Skip to content

Commit d425647

Browse files
chappy84robertdimarco
authored andcommitted
Add in support for late static binding, so that developers can extend and override variables / methods if they so require (#88)
1 parent d6186e0 commit d425647

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

Diff for: src/JWT.php

+21-21
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class JWT
6666
*/
6767
public static function decode($jwt, $key, $allowed_algs = array())
6868
{
69-
$timestamp = is_null(self::$timestamp) ? time() : self::$timestamp;
69+
$timestamp = is_null(static::$timestamp) ? time() : static::$timestamp;
7070

7171
if (empty($key)) {
7272
throw new InvalidArgumentException('Key may not be empty');
@@ -79,18 +79,18 @@ public static function decode($jwt, $key, $allowed_algs = array())
7979
throw new UnexpectedValueException('Wrong number of segments');
8080
}
8181
list($headb64, $bodyb64, $cryptob64) = $tks;
82-
if (null === ($header = JWT::jsonDecode(JWT::urlsafeB64Decode($headb64)))) {
82+
if (null === ($header = static::jsonDecode(static::urlsafeB64Decode($headb64)))) {
8383
throw new UnexpectedValueException('Invalid header encoding');
8484
}
85-
if (null === $payload = JWT::jsonDecode(JWT::urlsafeB64Decode($bodyb64))) {
85+
if (null === $payload = static::jsonDecode(static::urlsafeB64Decode($bodyb64))) {
8686
throw new UnexpectedValueException('Invalid claims encoding');
8787
}
88-
$sig = JWT::urlsafeB64Decode($cryptob64);
88+
$sig = static::urlsafeB64Decode($cryptob64);
8989

9090
if (empty($header->alg)) {
9191
throw new UnexpectedValueException('Empty algorithm');
9292
}
93-
if (empty(self::$supported_algs[$header->alg])) {
93+
if (empty(static::$supported_algs[$header->alg])) {
9494
throw new UnexpectedValueException('Algorithm not supported');
9595
}
9696
if (!in_array($header->alg, $allowed_algs)) {
@@ -105,13 +105,13 @@ public static function decode($jwt, $key, $allowed_algs = array())
105105
}
106106

107107
// Check the signature
108-
if (!JWT::verify("$headb64.$bodyb64", $sig, $key, $header->alg)) {
108+
if (!static::verify("$headb64.$bodyb64", $sig, $key, $header->alg)) {
109109
throw new SignatureInvalidException('Signature verification failed');
110110
}
111111

112112
// Check if the nbf if it is defined. This is the time that the
113113
// token can actually be used. If it's not yet that time, abort.
114-
if (isset($payload->nbf) && $payload->nbf > ($timestamp + self::$leeway)) {
114+
if (isset($payload->nbf) && $payload->nbf > ($timestamp + static::$leeway)) {
115115
throw new BeforeValidException(
116116
'Cannot handle token prior to ' . date(DateTime::ISO8601, $payload->nbf)
117117
);
@@ -120,14 +120,14 @@ public static function decode($jwt, $key, $allowed_algs = array())
120120
// Check that this token has been created before 'now'. This prevents
121121
// using tokens that have been created for later use (and haven't
122122
// correctly used the nbf claim).
123-
if (isset($payload->iat) && $payload->iat > ($timestamp + self::$leeway)) {
123+
if (isset($payload->iat) && $payload->iat > ($timestamp + static::$leeway)) {
124124
throw new BeforeValidException(
125125
'Cannot handle token prior to ' . date(DateTime::ISO8601, $payload->iat)
126126
);
127127
}
128128

129129
// Check if this token has expired.
130-
if (isset($payload->exp) && ($timestamp - self::$leeway) >= $payload->exp) {
130+
if (isset($payload->exp) && ($timestamp - static::$leeway) >= $payload->exp) {
131131
throw new ExpiredException('Expired token');
132132
}
133133

@@ -160,12 +160,12 @@ public static function encode($payload, $key, $alg = 'HS256', $keyId = null, $he
160160
$header = array_merge($head, $header);
161161
}
162162
$segments = array();
163-
$segments[] = JWT::urlsafeB64Encode(JWT::jsonEncode($header));
164-
$segments[] = JWT::urlsafeB64Encode(JWT::jsonEncode($payload));
163+
$segments[] = static::urlsafeB64Encode(static::jsonEncode($header));
164+
$segments[] = static::urlsafeB64Encode(static::jsonEncode($payload));
165165
$signing_input = implode('.', $segments);
166166

167-
$signature = JWT::sign($signing_input, $key, $alg);
168-
$segments[] = JWT::urlsafeB64Encode($signature);
167+
$signature = static::sign($signing_input, $key, $alg);
168+
$segments[] = static::urlsafeB64Encode($signature);
169169

170170
return implode('.', $segments);
171171
}
@@ -184,10 +184,10 @@ public static function encode($payload, $key, $alg = 'HS256', $keyId = null, $he
184184
*/
185185
public static function sign($msg, $key, $alg = 'HS256')
186186
{
187-
if (empty(self::$supported_algs[$alg])) {
187+
if (empty(static::$supported_algs[$alg])) {
188188
throw new DomainException('Algorithm not supported');
189189
}
190-
list($function, $algorithm) = self::$supported_algs[$alg];
190+
list($function, $algorithm) = static::$supported_algs[$alg];
191191
switch($function) {
192192
case 'hash_hmac':
193193
return hash_hmac($algorithm, $msg, $key, true);
@@ -217,11 +217,11 @@ public static function sign($msg, $key, $alg = 'HS256')
217217
*/
218218
private static function verify($msg, $signature, $key, $alg)
219219
{
220-
if (empty(self::$supported_algs[$alg])) {
220+
if (empty(static::$supported_algs[$alg])) {
221221
throw new DomainException('Algorithm not supported');
222222
}
223223

224-
list($function, $algorithm) = self::$supported_algs[$alg];
224+
list($function, $algorithm) = static::$supported_algs[$alg];
225225
switch($function) {
226226
case 'openssl':
227227
$success = openssl_verify($msg, $signature, $key, $algorithm);
@@ -236,13 +236,13 @@ private static function verify($msg, $signature, $key, $alg)
236236
if (function_exists('hash_equals')) {
237237
return hash_equals($signature, $hash);
238238
}
239-
$len = min(self::safeStrlen($signature), self::safeStrlen($hash));
239+
$len = min(static::safeStrlen($signature), static::safeStrlen($hash));
240240

241241
$status = 0;
242242
for ($i = 0; $i < $len; $i++) {
243243
$status |= (ord($signature[$i]) ^ ord($hash[$i]));
244244
}
245-
$status |= (self::safeStrlen($signature) ^ self::safeStrlen($hash));
245+
$status |= (static::safeStrlen($signature) ^ static::safeStrlen($hash));
246246

247247
return ($status === 0);
248248
}
@@ -276,7 +276,7 @@ public static function jsonDecode($input)
276276
}
277277

278278
if (function_exists('json_last_error') && $errno = json_last_error()) {
279-
JWT::handleJsonError($errno);
279+
static::handleJsonError($errno);
280280
} elseif ($obj === null && $input !== 'null') {
281281
throw new DomainException('Null result with non-null input');
282282
}
@@ -296,7 +296,7 @@ public static function jsonEncode($input)
296296
{
297297
$json = json_encode($input);
298298
if (function_exists('json_last_error') && $errno = json_last_error()) {
299-
JWT::handleJsonError($errno);
299+
static::handleJsonError($errno);
300300
} elseif ($json === 'null' && $input !== null) {
301301
throw new DomainException('Null result with non-null input');
302302
}

0 commit comments

Comments
 (0)