diff --git a/README.md b/README.md index 3eb7e96e..83ab34e7 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ $token = array( ); $jwt = JWT::encode($token, $key); -$decoded = JWT::decode($jwt, $key); +$decoded = JWT::decode($jwt, $key, 'HS256'); print_r($decoded); ?> diff --git a/lib/JWT/Authentication/JWT.php b/lib/JWT/Authentication/JWT.php index a5023b0d..c79f8733 100644 --- a/lib/JWT/Authentication/JWT.php +++ b/lib/JWT/Authentication/JWT.php @@ -32,7 +32,7 @@ class JWT * @uses jsonDecode * @uses urlsafeB64Decode */ - public static function decode($jwt, $key = null, $verify = true) + public static function decode($jwt, $key = null, $algo = null, $verify = true) { $tks = explode('.', $jwt); if (count($tks) != 3) { @@ -50,6 +50,9 @@ public static function decode($jwt, $key = null, $verify = true) if (empty($header->alg)) { throw new \DomainException('Empty algorithm'); } + if($algo && $header->alg !== $algo){ + throw new \DomainException('Invalid algorithm'); + } if ($sig != JWT::sign("$headb64.$bodyb64", $key, $header->alg)) { throw new \UnexpectedValueException('Signature verification failed'); }