2017 © Pedro Peláez
 

library authentication

Authentication

image

jdz/authentication

Authentication

  • Tuesday, May 29, 2018
  • by jdz
  • Repository
  • 1 Watchers
  • 0 Stars
  • 5 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

authentication

Simple authentication, (*1)

Usage

Exemple for a database authentication, (*2)

1/ Create a DatabaseConnector, (*3)

use JDZ\Authentication\Connector\DatabaseConnector;

/**
 * Database connector for authentication
 */
class DboAuthenticator extends DatabaseConnector
{
  /**
   * Get the user hashed password from the username
   * 
   * @param     array       $credentials  Key/value pairs holding the user credentials
   * @return    string      The user hashed password
   */
  protected function getHashedPassword(array $credentials)
  {
    $dbo = Dbo();

    $query = $dbo->getQuery(true);

    $query->select($this->tbl_pass_column);
    $query->from($this->tbl_name);
    $query->where($this->tbl_username_column.'='.$dbo->q($credentials['username']));
    $dbo->setQuery($query);
    $result = $dbo->loadResult();
    return (string)$result;
  }
}

2/ Authenticate, (*4)

use JDZ\Authentication\Authentication;
use JDZ\Authentication\AuthenticationException;

function authenticate($credentials)
{
  $authenticationConnector = new DboAuthenticator([
    'tbl_name' => 'users',
    'tbl_username_column' => 'email',
    'tbl_pass_column' => 'password',
  ]);

  $auth = new Authentication();
  $auth->addConnector($authenticationConnector);

  $authResponse = $auth->authenticate($credentials, $options);

  if ( $authResponse->status !== Authentication::SUCCESS ){
    if ( isset($options['silent']) && $options['silent'] ){
    return false;
    }

    switch($authResponse->status){
    case Authentication::EMPTY_USER:
      $message = 'Missing username in credentials';
      break;

    case Authentication::EMPTY_PASS:
      $message = 'Missing password in credentials';
      break;

    case Authentication::BAD_PASS:
      $message = 'Invalid password';
      break;

    case Authentication::BAD_CREDENTIALS:
      $message = 'Bad credentials';
      break;
    }

    throw new AuthenticationException($message);
  }
}

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

try {
  authenticate($credentials);
}
catch(AuthenticationException $e){
  die($e->getMessage();
}

The Versions

29/05 2018

dev-master

9999999-dev https://github.com/joffreydemetz/authentication/

Authentication

  Sources   Download

MIT

The Requires

  • php ^7.1.3

 

authentication

29/05 2018

2.0.2

2.0.2.0 https://github.com/joffreydemetz/authentication/

Authentication

  Sources   Download

MIT

The Requires

  • php ^7.1.3

 

authentication

13/05 2018

2.0.0

2.0.0.0 https://github.com/joffreydemetz/authentication/

Authentication

  Sources   Download

MIT

The Requires

  • php ^7.1.3

 

authentication

22/01 2018

1.2.0

1.2.0.0 http://joffreydemetz.com/git/authentication/

Authentication

  Sources   Download

MIT

The Requires

  • php >=5.6

 

authentication

03/12 2017

1.1.0

1.1.0.0 http://joffreydemetz.com/git/authentication/

Authentication

  Sources   Download

MIT

The Requires

  • php >=5.6

 

authentication