|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
You may not change or alter any portion of this comment or credits |
|
4
|
|
|
of supporting developers from this source code or any supporting source code |
|
5
|
|
|
which is considered copyrighted (c) material of the original comment or credit authors. |
|
6
|
|
|
|
|
7
|
|
|
This program is distributed in the hope that it will be useful, |
|
8
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
9
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Xmf; |
|
13
|
|
|
|
|
14
|
|
|
use Firebase\JWT\JWT; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Xmf\JsonWebToken |
|
18
|
|
|
* |
|
19
|
|
|
* Basic JWT support |
|
20
|
|
|
* |
|
21
|
|
|
* @category Xmf\JsonWebToken |
|
22
|
|
|
* @package Xmf |
|
23
|
|
|
* @author Richard Griffith <[email protected]> |
|
24
|
|
|
* @copyright 2016 XOOPS Project (http://xoops.org) |
|
25
|
|
|
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html) |
|
26
|
|
|
* @version Release: 1.0 |
|
27
|
|
|
* @link http://xoops.org |
|
28
|
|
|
*/ |
|
29
|
|
|
class JsonWebToken |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $key; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $algorithm = 'HS256'; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var array |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $claims = array(); |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* JsonWebToken constructor. |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $key key for signing/validating |
|
50
|
|
|
* @param string $algorithm algorithm to use for signing/validating |
|
51
|
|
|
*/ |
|
52
|
1 |
|
public function __construct($key = null, $algorithm = 'HS256') |
|
53
|
|
|
{ |
|
54
|
1 |
|
if (null === $key) { |
|
55
|
|
|
$keyFile = \XoopsBaseConfig::get('xoops-path') . '/data/syskey.php'; |
|
56
|
|
|
$key = include_once $keyFile; |
|
57
|
|
|
} |
|
58
|
1 |
|
$this->setKey($key); |
|
59
|
1 |
|
$this->setAlgorithm($algorithm); |
|
60
|
1 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Set the key |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $key key for signing/validating |
|
66
|
|
|
* |
|
67
|
|
|
* @return JsonWebToken |
|
68
|
|
|
*/ |
|
69
|
1 |
|
public function setKey($key) |
|
70
|
|
|
{ |
|
71
|
1 |
|
$this->key = $key; |
|
72
|
1 |
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $algorithm algorithm to use for signing/validating |
|
77
|
|
|
* |
|
78
|
|
|
* @return JsonWebToken |
|
79
|
|
|
* |
|
80
|
|
|
* @throws \DomainException |
|
81
|
|
|
*/ |
|
82
|
1 |
|
public function setAlgorithm($algorithm) |
|
83
|
|
|
{ |
|
84
|
1 |
|
if (array_key_exists($algorithm, JWT::$supported_algs)) { |
|
85
|
1 |
|
$this->algorithm = $algorithm; |
|
86
|
1 |
|
return $this; |
|
87
|
|
|
} |
|
88
|
1 |
|
throw new \DomainException('Algorithm not supported'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Decode a JWT string, validating signature and well defined registered claims, |
|
93
|
|
|
* and optionally validate against a list of supplied claims |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $jwtString string containing the JWT to decode |
|
96
|
|
|
* @param array $assertClaims associative array, claim => value, of claims to assert |
|
97
|
|
|
* |
|
98
|
|
|
* @return object|false |
|
99
|
|
|
*/ |
|
100
|
1 |
|
public function decode($jwtString, $assertClaims = array()) |
|
101
|
|
|
{ |
|
102
|
1 |
|
$allowedAlgorithms = array($this->algorithm); |
|
103
|
|
|
try { |
|
104
|
1 |
|
$values = JWT::decode($jwtString, $this->key, $allowedAlgorithms); |
|
105
|
1 |
|
} catch (\Exception $e) { |
|
106
|
1 |
|
trigger_error($e->getMessage(), E_USER_NOTICE); |
|
107
|
1 |
|
return false; |
|
108
|
|
|
} |
|
109
|
1 |
|
foreach ($assertClaims as $claim => $assert) { |
|
110
|
1 |
|
if (!property_exists($values, $claim)) { |
|
111
|
|
|
return false; |
|
112
|
1 |
|
} elseif ($values->$claim != $assert) { |
|
113
|
1 |
|
return false; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
1 |
|
return $values; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param array $payload associative array og claims, claim => value |
|
121
|
|
|
* @param int $expirationOffset seconds from now that token will expire. If not specified, |
|
122
|
|
|
* an "exp" claim will not be added or updated |
|
123
|
|
|
* |
|
124
|
|
|
* @return string encoded and signed jwt string |
|
125
|
|
|
* |
|
126
|
|
|
* @throws \DomainException; |
|
127
|
|
|
* @throws \InvalidArgumentException; |
|
128
|
|
|
* @throws \UnexpectedValueException; |
|
129
|
|
|
*/ |
|
130
|
1 |
|
public function create($payload, $expirationOffset = 0) |
|
131
|
|
|
{ |
|
132
|
1 |
|
if ((int) $expirationOffset > 0) { |
|
133
|
1 |
|
$payload['exp'] = time() + (int) $expirationOffset; |
|
134
|
|
|
} |
|
135
|
1 |
|
$value = JWT::encode($payload, $this->key, $this->algorithm); |
|
136
|
1 |
|
return $value; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|