Overview

A simple, flexible authentication class that is easy to set up and understand. It has adapters for Text, SQL, and LDAP.

Source is at https://bitbucket.org/vespula/vespula.auth

Version 5.1.0 features a fairly significant refactoring and updated code.

Installation

Installation is easiest via composer.

composer require vespula/auth

Example Usage

<?php

use Vespula\Auth\Session\Session;
use Vespula\Auth\Auth;
use Vespula\Auth\Adapter\Text;

// Create a session object before authentication.
$session = new Session();

// Optionally specify the maximum idle time of the session
$session->setMaxIdle(3600);

// You must `start` the session.
$session->start();

$adapter = new Text(...);

$auth = new Auth($adapter, $session);

Check for valid credentials

Credentials can be retrieved from POST data using PHP’s filter_input(). The FILTER_UNSAFE_RAW filter leaves the values unchanged; this is intentional. Passwords should not be sanitized, trimmed, or otherwise modified before authentication.

<?php

$username = filter_input(INPUT_POST, 'username', FILTER_UNSAFE_RAW, FILTER_REQUIRE_SCALAR);
$password = filter_input(INPUT_POST, 'password', FILTER_UNSAFE_RAW, FILTER_REQUIRE_SCALAR);

// Validate the username and password.
if (! is_string($username) || $username === '') {
    throw new InvalidArgumentException('A valid username is required.');
}

if (! is_string($password) || $password === '') {
    throw new InvalidArgumentException('A password is required.');
}

$credentials = [
    'username' => $username,
    'password' => $password
];

if ($auth->login($credentials)) {
    // Authentication successful
    // Note that escaping the user data is the developer's responsibility.
    $username = htmlspecialchars($auth->getUsername(), ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');

    echo 'Wecome, ' . $username;

    // Retrieve all userdata
    $userdata = $auth->getUserdata();

    // Or retrieve a specific value directly
    $fullname = $auth->getUserdata('fullname');
} else {
    // Authentication failed.
    // An incorrect username or password is a normal authentication failure,
    // not necessarily an adapter error. getError() may return null.

    $error = $adapter->getError();

    echo "Please try again";
}

Perform logout

<?php

$auth->logout();

Check Authentication State

<?php

// Check whether the session has exceeded its idle timeout.
// An idle session is automatically cleared.
if ($auth->isIdle()) {
    // Session has expired due to inactivity.
}

// Has the person been sitting idle too long?
if ($auth->isIdle()) {
    // Sitting around for too long
    // User is automatically logged out and status set to ANON
}

Access to user data

<?php
$username = $auth->getUsername();

$userdata = $auth->getUserdata(); // varies by adapter

// get a specific key from the $userdata array (assuming it exists in the array)
$email = $auth->getUserdata('email');

Autologin

A user can be automatically logged in without the authentication step. This can be useful when implementing a “remember me” feature. In these situations, the user must be validated before simply automatically logging them in.

$this->auth->authLogin($username);

This will attempt to look up the userdata and re-establish the session.