juanchosl / tokenizer
A small collection of encoder/decoder Token for authentication
Requires
- php: ^7.1 || ^8.0
- ext-hash: *
- ext-json: *
- ext-mbstring: *
- ext-openssl: *
- juanchosl/exceptions: ^1.0
Requires (Dev)
- phpstan/phpstan: 1.10.*
- phpunit/phpunit: 9.6.*
This package is auto-updated.
Last update: 2024-09-19 23:30:46 UTC
README
Description
A small collection of encoder/decoder Token for authentication
This is a test project in order to check how works the composer installation directly from GitHub
Install
composer require juanchosl/tokenizer
How use it
Entities
The system use a CredentialInterface as DTO, a simple object with username and password
All credentials needs to be pushed into a CredentialsInterface collection, a sequence of available usersto compare. We provide a simple Collection in order to push a few users, but is importante that you create your own implementation in order to search usrs into database, as usual
In a login request, you needs to ensure the username+password validation before create a token
Use a concret Tokenizer
For create a token
$options = [ JwtToken::OPTION_ISSUER => $_ENV['CYPHER_KEY'], JwtToken::OPTION_AUDIENCE => 'Restricted area' ]; $tokenizer = new JuanchoSL\Tokenizer\Repositories\JwtToken($options); $token = $tokenizer->encode(new Credential($username, $password));
For decode a token
$user_data = $tokenizer->decode($token);
For a check a token with a previously retrieved user
$tokenizer->check($db_data, $token):
Use the provided service
For create a token
$options = [ JwtToken::OPTION_ISSUER => $_ENV['CYPHER_KEY'], JwtToken::OPTION_AUDIENCE => 'Restricted area' ]; $tokenizer = new JuanchoSL\Tokenizer\Repositories\JwtToken($options); $service = new Authentication($tokenizer, new Credentials); $service->authenticateByCredential($credential); return $service->generateToken($credential);
For decode a token
$options = [ JwtToken::OPTION_ISSUER => $_ENV['CYPHER_KEY'], JwtToken::OPTION_AUDIENCE => 'Restricted area' ]; $tokenizer = new JuanchoSL\Tokenizer\Repositories\JwtToken($options); $service = new Authentication($tokenizer, new Credentials); return $service->->authenticateByToken($token);