1 | <?php |
||
27 | class JsonWebToken |
||
28 | { |
||
29 | /** |
||
30 | * @var KeyAbstract |
||
31 | */ |
||
32 | protected $key; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $algorithm = 'HS256'; |
||
38 | |||
39 | /** |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $claims = array(); |
||
43 | |||
44 | /** |
||
45 | * JsonWebToken constructor. |
||
46 | * |
||
47 | * @param KeyAbstract $key key for signing/validating |
||
48 | * @param string $algorithm algorithm to use for signing/validating |
||
49 | */ |
||
50 | 1 | public function __construct(KeyAbstract $key, $algorithm = 'HS256') |
|
55 | |||
56 | /** |
||
57 | * @param string $algorithm algorithm to use for signing/validating |
||
58 | * |
||
59 | * @return JsonWebToken |
||
60 | * |
||
61 | * @throws \DomainException |
||
62 | */ |
||
63 | 1 | public function setAlgorithm($algorithm) |
|
71 | |||
72 | /** |
||
73 | * Decode a JWT string, validating signature and well defined registered claims, |
||
74 | * and optionally validate against a list of supplied claims |
||
75 | * |
||
76 | * @param string $jwtString string containing the JWT to decode |
||
77 | * @param array|\Traversable $assertClaims associative array, claim => value, of claims to assert |
||
78 | * |
||
79 | * @return object|false |
||
80 | */ |
||
81 | 1 | public function decode($jwtString, $assertClaims = array()) |
|
82 | { |
||
83 | 1 | $allowedAlgorithms = array($this->algorithm); |
|
84 | try { |
||
85 | 1 | $values = JWT::decode($jwtString, $this->key->getVerifying(), $allowedAlgorithms); |
|
86 | 1 | } catch (\Exception $e) { |
|
87 | 1 | trigger_error($e->getMessage(), E_USER_NOTICE); |
|
88 | return false; |
||
89 | } |
||
90 | 1 | foreach ($assertClaims as $claim => $assert) { |
|
91 | 1 | if (!property_exists($values, $claim)) { |
|
92 | 1 | return false; |
|
93 | 1 | } elseif ($values->$claim != $assert) { |
|
94 | 1 | return false; |
|
95 | } |
||
96 | } |
||
97 | 1 | return $values; |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * Create a signed token string for a payload |
||
102 | * |
||
103 | * @param array|\ArrayObject $payload traversable set of claims, claim => value |
||
104 | * @param int $expirationOffset seconds from now that token will expire. If not specified, |
||
105 | * an "exp" claim will not be added or updated |
||
106 | * |
||
107 | * @return string encoded and signed jwt string |
||
108 | * |
||
109 | * @throws \DomainException; |
||
110 | * @throws \InvalidArgumentException; |
||
111 | * @throws \UnexpectedValueException; |
||
112 | */ |
||
113 | 1 | public function create($payload, $expirationOffset = 0) |
|
121 | } |
||
122 |