secretkey : a secret password string
hash_algo : hashing algorithm (e.g sha256, md5)
algo : any of the options - [HS256|HS384|HS512|RS256]
type : any of the options - [JWS|JWT]
payload : a data array having predefined keysets iss nbf and exp expected to be hashed
token : currently or previously generated token
$token : previously generated token
$jws = new Jwstoken;
set method is used to set or modify the default type and algorithm
to be used for generating the jwstokens. The default type is JWS and the
default algorithm is HS256. This method can be skipped if the default set parameters
are used.
$jws->set(type, algo);
$jws->set(); // sets default algorithm - ['JWS', 'HS256'] $jws->set('JWS', 'HS384'); // using JWS with HS384 algorithm $jws->set('JWT', 'HS384'); // using JWT with HS384 algorithm
algo() method is used to overide only the default algorithm
set for creating jwstokens. The algorithm supplied must be amongst the valid algorithms.
$jws->algo(hash_algo);
$jws->algo('HS512');
payload() method is used to set a payload for jwstokens.
$jws->payload($payload); // supplies data to be hashed.
$payload = [
'data'=>'mydata', // some extra data supplied
'nbf'=>time() + 60, // time when token becomes active (60secs)
'exp'=>time() + 120, // time when token becomes expired (2minutes after created)
'iss' => 'user', // user who issued token
];
$jws->payload($payload);
jwstoken class to perform verifications on the token generated. The
payload data should be set using the following special array indices. iss - issued by nbf - not before exp - expire time
The values defined above are keys that should be used when supplying some special data.
iss defines the user who issued a token.
nbf defines the time when a generated token should become active in seconds
exp defines the time when a generated token should expire in seconds.
Example: The following payload data ['iss' =>'user', 'nbf'=> 60, 'exp'=>120] tells the
jwstoken to issue a token from "user" that becomes active only after 1 minute (60secs) it was generated
and valid for 2 minutes (120secs). It should be noted that this token will only have a total of
1 minute activeness because 1 minute is used out of the accessible 2 minutes to pend the token. It is
also possible to set tokens that do not expire by not defining the expire time. The
nbf can also be avoided by not defining it.
$jws->expires($time);
where:
$time: expire time in seconds.
$jws->expires(120); // sets expire time to 2 minutes
$jws->sign(secretkey, hash_algos);
$jws = new JWSToken; $jws->payload($payload); //check payload for $payload supplied $jws->sign('password123'); //sign and generate a payload with sha256 $jws->sign('password123', 'md5'); //sign and generate a payload with md5
token sets or fetches a generated token. When a token is generated
using the sign() method, the token() method returns the current
hash string of the generated token. It can also be used to supply a token meant for decryption.
$jws->token(); // return a generated token $jws->token($token); // set a previously generated token
$jws->payload($payload); // check payload for the $payload used here. $jws->sign('secret_key'); // lock and sign payload with a secret key var_dump( $jws->token() ); // output generated token
$jws->isValid($secretkey, $hash_algo); // returns bool of true if token is valid
$jws->payload($payload); // check payload for the $payload used here $jws->sign('secret', 'sha256'); $token = $jws->token(); // generate a token. var_dump( $jws->isValid($token) ); // returns: true
// $token as some generated token var_dump( $jws->token($token)->isValid('password', 'md5') ); // note: hash algo (i.e md5) must match algo used for generating token
decrypt method decrypts a generated token, returning back the
supplied payload data.
$jws->decrypt($token, $secretkey, $hash_algo);
Note: if $token is not a valid token or cannot be decrypted, the method returns an empty data.
$jws = new JWSToken;
$jws->sign($payload, 'pass123', 'md5'); // check payload for the $payload used here.
$token = $jws->token();
$decrypt = $jws->decrypt($token, 'pass123', 'md5');
if($decrypt) {
var_dump($decrypt);
} else {
var_dump($jws->error);
}
$jws = new JWSToken;
$jws->set('JWS', 'md5');
//$token as some generated token
if( $jws->token($token)->isValid('pass', 'md5') ) {
var_dump( $jws->decrypt() ); // returns payload data or null
} else {
var_dump( $jws->error );
}
jwstoken class supports that when the method isValid() is used,
then decrypt() method can be used immediately after, providing a shorter way of decrypting
tokens.
$jws->expired(secretkey, hash_algo); // returns true or false // Note: when a testing has not been done, it returns an empty string.
// hash_algo used for generating token should be supplied if( $jws->token($token)->expired('pass', 'md5') ) { // run code }
$jws->pending(secretkey, hash_algo); // returns true or false
// hash_algo used for generating token should be supplied if( $jws->token($token)->expired('pass', 'md5') ) { // ... run code }
if( !$jws->token($token)->isValid('pass', 'md5') ) {
var_dump( $jws->error() );
}