@@ -68,7 +68,7 @@ public static function normalize(string $email): string
6868 assert (defined ('MB_CASE_FOLD ' ) && function_exists ('idn_to_ascii ' ));
6969
7070 $ localEnd = strrpos ($ email , '@ ' );
71- if (false === $ localEnd ) {
71+ if (false === $ localEnd || $ localEnd + 1 === strlen ( $ email ) ) {
7272 return '' ;
7373 }
7474
@@ -101,7 +101,7 @@ public static function normalize(string $email): string
101101 *
102102 * @return string URL to redirect the browser to
103103 */
104- public function authenticate (string $ email , string $ state = null ): string
104+ public function authenticate (string $ email , ? string $ state = null ): string
105105 {
106106 $ authEndpoint = $ this ->fetchDiscovery ()->authorization_endpoint ?? null ;
107107 if (!is_string ($ authEndpoint )) {
@@ -161,19 +161,23 @@ public function verify(string $token): string
161161 }
162162
163163 // Find the matching public key, and verify the signature.
164- $ publicKey = null ;
164+ $ publicKey = '' ;
165165 foreach ($ keysDoc ->keys as $ key ) {
166166 if ($ key instanceof \stdClass
167- && isset ($ key ->alg ) && 'RS256 ' === $ key ->alg
168- && isset ($ key ->kid ) && $ key ->kid === $ kid
169- && isset ($ key ->n ) && isset ($ key ->e )) {
170- $ publicKey = self ::parseJwk ($ key );
167+ && isset ($ key ->alg ) && 'RS256 ' === $ key ->alg
168+ && isset ($ key ->kid ) && $ key ->kid === $ kid
169+ ) {
170+ try {
171+ $ publicKey = JWK ::toPem ($ key );
172+ } catch (\Exception ) {
173+ }
171174 break ;
172175 }
173176 }
174- if (null === $ publicKey ) {
177+ if ('' === $ publicKey ) {
175178 throw new \Exception ('Cannot find the public key used to sign the token ' );
176179 }
180+ $ publicKey = JwtSigner \Key \InMemory::plainText ($ publicKey );
177181
178182 // Validate the token claims.
179183 $ clock = \Lcobucci \Clock \SystemClock::fromUTC ();
@@ -228,28 +232,6 @@ private function fetchDiscovery(): \stdClass
228232 return $ this ->store ->fetchCached ('discovery ' , $ discoveryUrl );
229233 }
230234
231- /**
232- * Parse a JWK into a PEM public key.
233- */
234- private static function parseJwk (\stdClass $ jwk ): JwtSigner \Key
235- {
236- $ n = gmp_init (bin2hex (self ::decodeBase64Url ($ jwk ->n )), 16 );
237- $ e = gmp_init (bin2hex (self ::decodeBase64Url ($ jwk ->e )), 16 );
238-
239- $ seq = new \FG \ASN1 \Universal \Sequence ();
240- $ seq ->addChild (new \FG \ASN1 \Universal \Integer (gmp_strval ($ n )));
241- $ seq ->addChild (new \FG \ASN1 \Universal \Integer (gmp_strval ($ e )));
242- $ pkey = new \FG \X509 \PublicKey (bin2hex ($ seq ->getBinary ()));
243-
244- $ encoded = base64_encode ($ pkey ->getBinary ());
245-
246- return JwtSigner \Key \InMemory::plainText (
247- "-----BEGIN PUBLIC KEY----- \n" .
248- chunk_split ($ encoded , 64 , "\n" ).
249- "-----END PUBLIC KEY----- \n"
250- );
251- }
252-
253235 /**
254236 * Get the origin for a URL.
255237 */
@@ -281,14 +263,4 @@ private static function getOrigin(string $url): string
281263
282264 return $ res ;
283265 }
284-
285- private static function decodeBase64Url (string $ input ): string
286- {
287- $ output = base64_decode (strtr ($ input , '-_ ' , '+/ ' ), true );
288- if (false === $ output ) {
289- throw new \Exception ('Invalid base64 ' );
290- }
291-
292- return $ output ;
293- }
294266}
0 commit comments