- Notifications
You must be signed in to change notification settings - Fork0
🪅 Turn everything into a unique encrypted JWT
License
attla/token
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
composer require attla/token
useAttla\Token\FactoryasToken;useAttla\Token\FacadeasTokenFacade;// create token on PHP projects$token = Token::create();// on laravel projects$token = TokenFacade::create();// or with global alias on laravel projects$token = \Token::create();// set a payload$token->body('token value..');// get the token value$tokenEncoded =$token->get();
$token = Token::create()->secret('your secret phrase');// changing the secret on exist instance$token->secret('your secret phrase');// secret aliases$token->phrase('your secret phrase');$token->passphrase('your secret phrase');// Set token body type when it can be converted (array, stdClass, object)$token->associative();// set token payload as associative array$token->asObject();// set payload as stdClass object// defines that it will always generate the same result$token->same();
By default thesecret
key is empty, but on laravel projects the default asenv('APP_KEY')
orconfig('app.key')
When token body asstring
,integer
,float
,bool
, andnull
, it cant be converted to associative or object equivalent
Set theexpiration
time in seconds after which the JWT MUST NOT be accepted for processing:
useCarbon\Carbon;$time =strtotime('+1 hour');$token->exp($time);$token->expiration((new \DateTime())->setTimeStamp($time));$token->expiresAt(Carbon::createFromTimestamp($time));
Set the time at which the JWT was issued (iat
):
useCarbon\Carbon;$time =strtotime('-1 day');$token->iat($time);$token->issuedAt((new \DateTime())->setTimeStamp($time));$token->issuedBefore(Carbon::createFromTimestamp($time));
Set the time before (nbf
) which the JWT MUST NOT be accepted for processing
useCarbon\Carbon;$time =strtotime('+30 day');$token->nbf($time);$token->notBefore((new \DateTime())->setTimeStamp($time));$token->canOnlyBeUsedAfter(Carbon::createFromTimestamp($time));
Set theaudience
that the JWT is intended for:
$token->aud('https://example.com');$token->audience('https://example.com','https://example.app');$token->permittedFor(['https://example.net','https://example.org']);
Set the principalsubject
of the JWT:
$token->relatedTo('exampl@e.com');$token->sub('exampl@e.com');
Set the principal that issued (iss
) the JWT:
$token->issuedBy('https://example.com');$token->iss('https://example.net');
Set the unique identifier (jti
) for the JWT:
$jti =hash('sha256',uniqid(mt_rand(),true));$token->jti($jti);$token->identifiedBy($jti);
Lock the token bybrowser
user agent:
// current browser$token->bwr();$token->broser();// setup a user agent by string$token->browser('Mozilla/5.0 (U; Linux x86_64; en-US) Gecko/20100101 Firefox/50.9');
Lock the token byip
address:
// current request ip address$token->ip();// setup a ip address by string$token->ip('1.1.1.1');$token->ip('1.1.1.1','2001:db8:0:0:0:0:2:1');$token->ip(['1.1.1.1','8.8.8.8']);
Lock the token by geographic coordinates (loc
):
// setup a location by coordinate string$token->loc('-44.05964,77.10679,5');
// set a custom claim "uid"$token->withClaim('uid',1);$token->with('uid',1);// alias// on parse validate using:$token->with('uid',1);
All claim values as inserted on token header, to be retrieved on body use:
// insert the payload as array or object$token->payload(['uid' =>1]);// on parse validate use:$token->with('uid',1);
Verifying if a value is present on token:
$hasUid =$token->has('uid');// isset(uid)$hasUidWithValue =$token->has('uid',1);// isset(uid) && uid === 1
$tokenValue = Token::parse($tokenEncoded) ->associative() ->get();
// Creating$token = Token::create() ->secret('your secret phrase')// secret key ->iss($_SERVER['HTTP_HOST'])// Set 'issuer' claim ->aud('e.com',$_SERVER['HTTP_HOST'])// Set 'audience' claim ->sub('7urkg6uDkMISjZBuFGdeySokAIrSuWAB')// Set 'subject' claim ->iat(time())// Set 'issued' date in seconds ->exp(7200)// Set 'expiration' in seconds (2 hours) ->bwr()// Lock the token by user agent of browser ->ip()// Lock the token with IP (v6 or v4) ->payload([// Set the token payload'name' =>'Acme LLC','email' =>'acme@e.com', ]);// Get the token$tokenEncoded =$token->get();echo$tokenEncoded .PHP_EOL;$tokenParse = Token::parse($tokenEncoded) ->iss($_SERVER['HTTP_HOST'])// Set the issuer claim for validate ->validAt(time() -3600)// Rewrites the current date for 'exp', 'iat', 'nbf' validations ->associative();if ($tokenParse->isValid()) {echo'Subject:'.$tokenParse->sub() .PHP_EOL;echo'Audience:'.implode(',',$tokenParse->audience()) .PHP_EOL;echo$tokenParse->get() .PHP_EOL;}else {echo"Token as invalid!" .PHP_EOL;}
This package is licensed under theMIT license ©Zunq.
About
🪅 Turn everything into a unique encrypted JWT
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.