Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add in support for late static binding #88

Merged
merged 1 commit into from
Jun 28, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions src/JWT.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class JWT
*/
public static function decode($jwt, $key, $allowed_algs = array())
{
$timestamp = is_null(self::$timestamp) ? time() : self::$timestamp;
$timestamp = is_null(static::$timestamp) ? time() : static::$timestamp;

if (empty($key)) {
throw new InvalidArgumentException('Key may not be empty');
Expand All @@ -79,18 +79,18 @@ public static function decode($jwt, $key, $allowed_algs = array())
throw new UnexpectedValueException('Wrong number of segments');
}
list($headb64, $bodyb64, $cryptob64) = $tks;
if (null === ($header = JWT::jsonDecode(JWT::urlsafeB64Decode($headb64)))) {
if (null === ($header = static::jsonDecode(static::urlsafeB64Decode($headb64)))) {
throw new UnexpectedValueException('Invalid header encoding');
}
if (null === $payload = JWT::jsonDecode(JWT::urlsafeB64Decode($bodyb64))) {
if (null === $payload = static::jsonDecode(static::urlsafeB64Decode($bodyb64))) {
throw new UnexpectedValueException('Invalid claims encoding');
}
$sig = JWT::urlsafeB64Decode($cryptob64);
$sig = static::urlsafeB64Decode($cryptob64);

if (empty($header->alg)) {
throw new UnexpectedValueException('Empty algorithm');
}
if (empty(self::$supported_algs[$header->alg])) {
if (empty(static::$supported_algs[$header->alg])) {
throw new UnexpectedValueException('Algorithm not supported');
}
if (!in_array($header->alg, $allowed_algs)) {
Expand All @@ -105,13 +105,13 @@ public static function decode($jwt, $key, $allowed_algs = array())
}

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

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

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

Expand Down Expand Up @@ -160,12 +160,12 @@ public static function encode($payload, $key, $alg = 'HS256', $keyId = null, $he
$header = array_merge($head, $header);
}
$segments = array();
$segments[] = JWT::urlsafeB64Encode(JWT::jsonEncode($header));
$segments[] = JWT::urlsafeB64Encode(JWT::jsonEncode($payload));
$segments[] = static::urlsafeB64Encode(static::jsonEncode($header));
$segments[] = static::urlsafeB64Encode(static::jsonEncode($payload));
$signing_input = implode('.', $segments);

$signature = JWT::sign($signing_input, $key, $alg);
$segments[] = JWT::urlsafeB64Encode($signature);
$signature = static::sign($signing_input, $key, $alg);
$segments[] = static::urlsafeB64Encode($signature);

return implode('.', $segments);
}
Expand All @@ -184,10 +184,10 @@ public static function encode($payload, $key, $alg = 'HS256', $keyId = null, $he
*/
public static function sign($msg, $key, $alg = 'HS256')
{
if (empty(self::$supported_algs[$alg])) {
if (empty(static::$supported_algs[$alg])) {
throw new DomainException('Algorithm not supported');
}
list($function, $algorithm) = self::$supported_algs[$alg];
list($function, $algorithm) = static::$supported_algs[$alg];
switch($function) {
case 'hash_hmac':
return hash_hmac($algorithm, $msg, $key, true);
Expand Down Expand Up @@ -217,11 +217,11 @@ public static function sign($msg, $key, $alg = 'HS256')
*/
private static function verify($msg, $signature, $key, $alg)
{
if (empty(self::$supported_algs[$alg])) {
if (empty(static::$supported_algs[$alg])) {
throw new DomainException('Algorithm not supported');
}

list($function, $algorithm) = self::$supported_algs[$alg];
list($function, $algorithm) = static::$supported_algs[$alg];
switch($function) {
case 'openssl':
$success = openssl_verify($msg, $signature, $key, $algorithm);
Expand All @@ -236,13 +236,13 @@ private static function verify($msg, $signature, $key, $alg)
if (function_exists('hash_equals')) {
return hash_equals($signature, $hash);
}
$len = min(self::safeStrlen($signature), self::safeStrlen($hash));
$len = min(static::safeStrlen($signature), static::safeStrlen($hash));

$status = 0;
for ($i = 0; $i < $len; $i++) {
$status |= (ord($signature[$i]) ^ ord($hash[$i]));
}
$status |= (self::safeStrlen($signature) ^ self::safeStrlen($hash));
$status |= (static::safeStrlen($signature) ^ static::safeStrlen($hash));

return ($status === 0);
}
Expand Down Expand Up @@ -276,7 +276,7 @@ public static function jsonDecode($input)
}

if (function_exists('json_last_error') && $errno = json_last_error()) {
JWT::handleJsonError($errno);
static::handleJsonError($errno);
} elseif ($obj === null && $input !== 'null') {
throw new DomainException('Null result with non-null input');
}
Expand All @@ -296,7 +296,7 @@ public static function jsonEncode($input)
{
$json = json_encode($input);
if (function_exists('json_last_error') && $errno = json_last_error()) {
JWT::handleJsonError($errno);
static::handleJsonError($errno);
} elseif ($json === 'null' && $input !== null) {
throw new DomainException('Null result with non-null input');
}
Expand Down