@@ -567,7 +567,7 @@ public function defer(
567567test ('decode() can be used with access tokens ' , function (): void {
568568 $ token = (new TokenGenerator ())->withHs256 ([
569569 'iss ' => 'https:// ' . $ this ->configuration ['domain ' ] . '/ '
570- ]);
570+ ], ' __test_client_secret__ ' , [], TokenGenerator:: TOKEN_ACCESS );
571571
572572 $ auth0 = new Auth0 ($ this ->configuration + [
573573 'tokenAlgorithm ' => 'HS256 ' ,
@@ -586,6 +586,52 @@ public function defer(
586586 expect ($ decoded ->getAudience ())->toContain ('__test_client_id__ ' );
587587});
588588
589+ test ('decode() rejects ID tokens when validating as access tokens ' , function (): void {
590+ $ idToken = (new TokenGenerator ())->withHs256 ([
591+ 'iss ' => 'https:// ' . $ this ->configuration ['domain ' ] . '/ ' ,
592+ 'nonce ' => '__test_nonce__ ' ,
593+ ], '__test_client_secret__ ' , [], TokenGenerator::TOKEN_ID );
594+
595+ $ auth0 = new Auth0 ($ this ->configuration + [
596+ 'tokenAlgorithm ' => 'HS256 ' ,
597+ ]);
598+
599+ $ auth0 ->decode ($ idToken ,
600+ null ,
601+ null ,
602+ null ,
603+ null ,
604+ null ,
605+ null ,
606+ Token::TYPE_ACCESS_TOKEN ,
607+ );
608+ })->throws (InvalidTokenException::class, InvalidTokenException::MSG_ID_TOKEN_USED_AS_ACCESS_TOKEN );
609+
610+ test ('decode() respects explicit audience for access tokens ' , function (): void {
611+ $ apiAudience = 'https://api.example.com ' ;
612+ $ token = (new TokenGenerator ())->withHs256 ([
613+ 'iss ' => 'https:// ' . $ this ->configuration ['domain ' ] . '/ ' ,
614+ 'aud ' => $ apiAudience
615+ ], '__test_client_secret__ ' , [], TokenGenerator::TOKEN_ACCESS );
616+
617+ $ auth0 = new Auth0 ($ this ->configuration + [
618+ 'tokenAlgorithm ' => 'HS256 ' ,
619+ 'audience ' => [$ apiAudience ],
620+ ]);
621+
622+ $ decoded = $ auth0 ->decode ($ token ,
623+ null ,
624+ null ,
625+ null ,
626+ null ,
627+ null ,
628+ null ,
629+ Token::TYPE_ACCESS_TOKEN ,
630+ );
631+
632+ expect ($ decoded ->getAudience ())->toEqual ([$ apiAudience ]);
633+ });
634+
589635test ('decode() can be used with logout tokens ' , function (): void {
590636 $ mockLogoutToken = TokenGenerator::create (TokenGenerator::TOKEN_LOGOUT , TokenGenerator::ALG_HS256 , [
591637 'iss ' => 'https:// ' . $ this ->configuration ['domain ' ] . '/ '
0 commit comments