@@ -63,32 +63,24 @@ class ClerkApiToken implements FPApiToken {
6363 }
6464 }
6565
66- // Dynamic JWKS discovery - extract issuer from JWT
67- let jwksUrl : string ;
66+ // Extract issuer from JWT and use it if present, otherwise fall back to config
67+ const [ , payloadB64 ] = token . split ( '.' ) ;
68+ if ( ! payloadB64 ) {
69+ throw new Error ( "Invalid JWT format - missing payload" ) ;
70+ }
71+
72+ const payload = JSON . parse ( atob ( payloadB64 ) ) ;
73+ const issuer = payload . iss ;
6874
69- if ( CLERK_PUB_JWT_URL ) {
70- // Use explicitly provided JWKS URL
75+ let jwksUrl : string ;
76+ if ( issuer && issuer . startsWith ( "https://" ) ) {
77+ // Use issuer from JWT (preferred)
78+ jwksUrl = `${ issuer } /.well-known/jwks.json` ;
79+ } else if ( CLERK_PUB_JWT_URL ) {
80+ // Fall back to configured URL
7181 jwksUrl = CLERK_PUB_JWT_URL ;
7282 } else {
73- // Auto-discover JWKS URL from JWT issuer
74- const [ , payloadB64 ] = token . split ( '.' ) ;
75- if ( ! payloadB64 ) {
76- throw new Error ( "Invalid JWT format - missing payload" ) ;
77- }
78-
79- const payload = JSON . parse ( atob ( payloadB64 ) ) ;
80- const issuer = payload . iss ;
81-
82- if ( ! issuer ) {
83- throw new Error ( "JWT missing issuer (iss) field - cannot auto-discover JWKS URL" ) ;
84- }
85-
86- if ( ! issuer . startsWith ( "https://" ) ) {
87- throw new Error ( `JWT issuer must use HTTPS: ${ issuer } ` ) ;
88- }
89-
90- jwksUrl = `${ issuer } /.well-known/jwks.json` ;
91- console . log ( '🔍 Auto-discovered JWKS URL:' , jwksUrl ) ;
83+ throw new Error ( "No valid JWKS URL: JWT missing issuer and CLERK_PUB_JWT_URL not set" ) ;
9284 }
9385
9486 // Validate URL format and security
0 commit comments