Text Adapter

This adapter can be used to authenticate users based on credentials stored in an htpasswd file or from an array of usernames and passwords. Passwords can be stored as hashes using PHP’s password_hash() function, or by Apache’s APR1-MD5 algorithm. The APR1-MD5 check is made possible thanks to the APR1-MD5 package by Jeremy Ebler https://github.com/whitehat101/apr1-md5.

Creating Passwords

Apache htpasswd files can easily be created using Apache’s htpasswd command. For example, to create a password file and add a user, you would issue the following:

$ htpasswd -c /path/to/.htpasswd juser

To add another user to the same file, issue the following noting the omission of the -c switch:

$ htpasswd /path/to/.htpasswd otheruser

By default, Apache will hash the password using a custom md5 hashing algorithm. You can also use the bcrypt algorithm by using the -B option.

$ htpasswd -B /path/to/.htpasswd jdoe

The resulting password file may look something like this:

juser:$apr1$Y.cxiqTt$4SPe4SHcbX6yzrIMlYtJJ0
otheruser:$apr1$kn0MShZv$RCvYu/wezJ2nAA9mH6BNE0
jdoe:$2y$05$CpZSkygbkm5HHoYYF2lnx..qB4dGywQpIT5GsqWeO26mkPnNzzu8G

Now we can create the Text adapter passing the full path of the file to the constructor.

<?php
use Vespula\Auth\Session\Session;
use Vespula\Auth\Auth;
use Vespula\Auth\Adapter\Text;

$session = new Session();
$passwordfile = '/path/to/.htpasswd';
$adapter = new Text($passwordfile);

// This data is returned by the getUserdata() method
$adapter->setUserdata('juser', [
    'fullname'=>'Joe User',
    'email'=>'juser@vespula.com'
]);

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

You may also pass an array of users and passwords to the Text adapter constructor. Passwords may be hashed using Apache’s APR1-MD5 algorithm, Apache’s bcrypt algorithm, or PHP’s password_hash() function. For example:

<?php

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

$session = new Session();

$passwords = [
    'juser'=>'$apr1$Y.cxiqTt$4SPe4SHcbX6yzrIMlYtJJ0',
    'jsmith'=>'$2y$10$XNbpIsW6zI1QIR1tdCXr5.7DuE8Jpfd8yAdQ8czcjDbUrykCBDYXm'
];

$adapter = new Text($passwords);

// This data is returned by the getUserdata() method
$adapter->setUserdata('juser', [
    'fullname'=>'Joe User',
    'email'=>'juser@vespula.com'
]);

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

Loading User Data

You may want to load user data, such as email, full name, etc., into the auth object. This can be done in one of two ways.

Load data as an array

<?php

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

$session = new Session();

$passwords = [
    'juser'=>'$apr1$Y.cxiqTt$4SPe4SHcbX6yzrIMlYtJJ0',
    'jsmith'=>'$2y$10$XNbpIsW6zI1QIR1tdCXr5.7DuE8Jpfd8yAdQ8czcjDbUrykCBDYXm'
];

$adapter = new Text($passwords);

$userdata = [
    'juser'=>[
        'fullname'=>'Joe User',
        'email'=>'juser@example.com'
    ],
    'jsmith'=>[
        'fullname'=>'John Smith',
        'email'=>'jsmith@example.com'
    ],
];

$adapter->loadUserData($userdata);
$auth = new \Vespula\Auth\Auth($adapter, $session);

// Perform login etc...

// Now you can get userdata for the logged in (valid) user
$info = $auth->getUserdata();

// Or get a specific key
$email = $auth->getUserdata('email');

Set data for individual username

<?php

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

$session = new Session();

$passwords = [
    'juser'=>'$apr1$Y.cxiqTt$4SPe4SHcbX6yzrIMlYtJJ0',
    'jsmith'=>'$2y$10$XNbpIsW6zI1QIR1tdCXr5.7DuE8Jpfd8yAdQ8czcjDbUrykCBDYXm'
];

$adapter = new Text($passwords);

$adapter->setUserdata('juser', [
    'fullname'=>'Joe User',
    'email'=>'juser@example.com'
]);

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

// Now you can get userdata for the logged in (valid) user
$info = $auth->getUserdata();

// Or get a specific key
$email = $auth->getUserdata('email');