ds/jwt

This package is abandoned and no longer maintained. The author suggests using the lcobucci/jwt package instead.

json web tokens

v1.0.0.0 2017-11-27 16:23 UTC

This package is not auto-updated.

Last update: 2018-06-13 16:24:44 UTC


README

#Json Web Tokens

Json Web Tokens


//Signatures
$HsSignatute =  new \Ds\Jwt\Signature\HsSignature('my-secret')
$RsSignature =  new \Ds\Jwt\Signature\RsSignature(
                   file_get_contents(__DIR__ . '/key.pub'),
                   file_get_contents(__DIR__ . '/key.priv')
                );



$jwt =  new JWT(new Parser());
$jwt = $jwt->withSignature('HS', $HsSignatute);
$jwt = $jwt->withSignature('RS', $RsSignature);


//Define Claims.
$claims = [
  'jti' => 'my-id',
  'aud' => 'foo.bar',
  'nbf' => time()
];

try{
    $newToken = $jwt->createFromArray('HS256', $claims)->getToken()
}catch(Exception $e){
    //algo not found.
}



//Import rules into validator.
$newJwtRules = $validator->withRules($claims);

try{
    $token->import($newJwt, $newJwtRules)->verify();
}catch (\Exception $e){
    //token not valid.
}

$HS256Token = $token->withId('myId')
                     ->withIssuer('www.domain.com')
                     ->withAudience('foo.bar')
                     ->withExpires(time())
                     ->withIssuedAt(time())
                     ->withNotBefore(time()-600)
                     ->withSubject(['subject'])
                     ->withClaim('custom', 'my claim data')
                     ->withClaim('custom2', ['my claim data'])
                     ->useSignature('RS256')
                     ->create();

$validator = new \Ds\Jwt\Validator();

$validator->setId('myId')
            ->setIssuer('www.domain.com')
            ->setAudience('foo.bar')
            ->setExpires(time())
            ->setIssuedAt(time())
            ->setNotBefore(time()-600)
            ->setSubject(['subject'])
            ->setClaim('custom','my claim data')
            ->setClaim('custom2', ['my claim data']);

try{
    $result = $token->import($HS256Token, $validator)->verify(); //finds correct signature
    var_dump($result);
}catch (\Exception $e){
    echo $e->getMessage();
}

//