Guardian
, (*1)
Simple and flexible authentication framework., (*2)
Install
Via Composer, (*3)
``` bash
$ composer require indigophp/guardian, (*4)
## Usage
This library provides an easy way to authenticate any entity with OR without persisting and calling it "login".
A simple login example:
``` php
use Indigo\Guardian\Identifier\InMemory;
use Indigo\Guardian\Authenticator\UserPassword;
use Indigo\Guardian\Hasher\Plaintext;
use Indigo\Guardian\SessionAuth;
use Indigo\Guardian\Session\Native;
$identifier = new InMemory([
1 => [
'username' => 'john.doe',
'password' => 'secret',
'name' => 'John Doe',
],
]);
$authenticator = new UserPassword(new Plaintext);
$session = new Native;
$auth = new SessionAuth($identifier, $authenticator, $session);
// returns true to indicate success
$auth->login([
'username' => 'john.doe',
'password' => 'secret',
]);
Later, when login succeeds, check for the current login:, (*5)
``` php
// returns true/false
$auth->check();, (*6)
// returns the current caller
$caller = $auth->getCurrentCaller();, (*7)
And logout at the end:
``` php
// returns true/false
$auth->logout();
API Authentication
Since Guardian is an authentication library, you can easily use it to authenticate API requests without persistence. To achieve this, see the following simple authentication service:, (*8)
``` php
use Indigo\Guardian\Identifier\InMemory;
use Indigo\Guardian\Authenticator\UserPassword;
use Indigo\Guardian\Hasher\Plaintext;
use Indigo\Guardian\RequestAuth;, (*9)
$identifier = new InMemory([
1 => [
'username' => 'john.doe',
'password' => 'secret',
'name' => 'John Doe',
],
]);, (*10)
$authenticator = new UserPassword(new Plaintext);, (*11)
$auth = new RequestAuth($identifier, $authenticator);, (*12)
$subject = [
'username' => 'john.doe',
'password' => 'secret',
];, (*13)
// returns true to indicate success
$auth->authenticate($subject);, (*14)
// returns the caller object if identify succeeds
$caller = $auth->authenticateAndReturn($subject);, (*15)
## Testing
``` bash
$ phpspec run
Contributing
Please see CONTRIBUTING for details., (*16)
Credits
License
The MIT License (MIT). Please see License File for more information., (*17)