55use Psr \Http \Message \ResponseInterface ;
66use Psr \Http \Message \ServerRequestInterface ;
77use Psr \Http \Server \RequestHandlerInterface ;
8- use Tqdev \PhpCrudApi \Controller \Responder ;
98use Tqdev \PhpCrudApi \Middleware \Base \Middleware ;
109use Tqdev \PhpCrudApi \Record \ErrorCode ;
1110use Tqdev \PhpCrudApi \RequestUtils ;
1211
1312class JwtAuthMiddleware extends Middleware
1413{
15- private function getVerifiedClaims (string $ token , int $ time , int $ leeway , int $ ttl , string $ secret , array $ requirements ): array
14+ private function getVerifiedClaims (string $ token , int $ time , int $ leeway , int $ ttl , array $ secrets , array $ requirements ): array
1615 {
1716 $ algorithms = array (
1817 'HS256 ' => 'sha256 ' ,
@@ -27,9 +26,14 @@ private function getVerifiedClaims(string $token, int $time, int $leeway, int $t
2726 return array ();
2827 }
2928 $ header = json_decode (base64_decode (strtr ($ token [0 ], '-_ ' , '+/ ' )), true );
30- if (!$ secret ) {
29+ $ kid = 0 ;
30+ if (isset ($ header ['kid ' ])) {
31+ $ kid = $ header ['kid ' ];
32+ }
33+ if (!isset ($ secrets [$ kid ])) {
3134 return array ();
3235 }
36+ $ secret = $ secrets [$ kid ];
3337 if ($ header ['typ ' ] != 'JWT ' ) {
3438 return array ();
3539 }
@@ -93,16 +97,16 @@ private function getClaims(string $token): array
9397 $ time = (int ) $ this ->getProperty ('time ' , time ());
9498 $ leeway = (int ) $ this ->getProperty ('leeway ' , '5 ' );
9599 $ ttl = (int ) $ this ->getProperty ('ttl ' , '30 ' );
96- $ secret = $ this ->getProperty ('secret ' , '' );
100+ $ secrets = $ this ->getMapProperty ('secrets ' , '' );
101+ if (!$ secrets ) {
102+ $ secrets = [$ this ->getProperty ('secret ' , '' )];
103+ }
97104 $ requirements = array (
98105 'alg ' => $ this ->getArrayProperty ('algorithms ' , '' ),
99106 'aud ' => $ this ->getArrayProperty ('audiences ' , '' ),
100107 'iss ' => $ this ->getArrayProperty ('issuers ' , '' ),
101108 );
102- if (!$ secret ) {
103- return array ();
104- }
105- return $ this ->getVerifiedClaims ($ token , $ time , $ leeway , $ ttl , $ secret , $ requirements );
109+ return $ this ->getVerifiedClaims ($ token , $ time , $ leeway , $ ttl , $ secrets , $ requirements );
106110 }
107111
108112 private function getAuthorizationToken (ServerRequestInterface $ request ): string
0 commit comments