Text Adapter
This adapter is used to authenticate users based on credentials stored in an htpasswd file
or from an array of usernames and passwords. Password hashes can be stored 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 -m /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 its APR1-MD5 algorithm hashing algorithm. You can also use the bcrypt algorithm by using the -B option. The bcrypt is generally preferable for new password files.
$ htpasswd -B /path/to/.htpasswd jdoe
The resulting password file may look something like this:
You can also create a password using PHP’s password_hash function.
$hash = password_hash('some-password', PASSWORD_DEFAULT);
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.
The password file must be outside the web document root and readable by the PHP process.
The adapter expects the full filesystem path, not a URL or relative web path.
<?php
use Vespula\Auth\Session\Session;
use Vespula\Auth\Auth;
use Vespula\Auth\Adapter\Text;
$session = new Session();
$session->start();
$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. The array will have usernames for keys and passwords for values.
For example:
<?php
use Vespula\Auth\Session\Session;
use Vespula\Auth\Auth;
use Vespula\Auth\Adapter\Text;
$session = new Session();
$session->start();
$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();
$session->start();
$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 Auth($adapter, $session);
// Perform login etc...
if ($auth->login($credentials)) {
// Successful login
// Now you can get userdata for the logged in (valid) user
$info = $auth->getUserdata();
// Or get a specific key
$email = $auth->getUserdata('email');
} else {
// Unsuccessful
// Throw an error or notify the user.
}
Setting Data for an Individual User
<?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');