@@ -47,30 +47,43 @@ export class RemoteJWKSCollector implements KeyCollector {
4747    this . agent  =  this . resolveAgent ( ) ; 
4848  } 
4949
50-   async  getKeys ( ) : Promise < KeyResult >  { 
50+   private   async  getJwksData ( ) : Promise < any >  { 
5151    const  abortController  =  new  AbortController ( ) ; 
5252    const  timeout  =  setTimeout ( ( )  =>  { 
5353      abortController . abort ( ) ; 
5454    } ,  30_000 ) ; 
5555
56-     const  res  =  await  fetch ( this . url ,  { 
57-       method : 'GET' , 
58-       headers : { 
59-         Accept : 'application/json' 
60-       } , 
61-       signal : abortController . signal , 
62-       agent : this . agent 
63-     } ) ; 
64- 
65-     if  ( ! res . ok )  { 
66-       throw  new  AuthorizationError ( ErrorCode . PSYNC_S2204 ,  `JWKS request failed with ${ res . statusText }  ,  { 
67-         configurationDetails : `JWKS URL: ${ this . url }  
56+     try  { 
57+       const  res  =  await  fetch ( this . url ,  { 
58+         method : 'GET' , 
59+         headers : { 
60+           Accept : 'application/json' 
61+         } , 
62+         signal : abortController . signal , 
63+         agent : this . agent 
6864      } ) ; 
69-     } 
7065
71-     const  data  =  ( await  res . json ( ) )  as  any ; 
66+       if  ( ! res . ok )  { 
67+         throw  new  AuthorizationError ( ErrorCode . PSYNC_S2204 ,  `JWKS request failed with ${ res . statusText }  ,  { 
68+           configurationDetails : `JWKS URL: ${ this . url }  
69+         } ) ; 
70+       } 
7271
73-     clearTimeout ( timeout ) ; 
72+       return  ( await  res . json ( ) )  as  any ; 
73+     }  catch  ( e )  { 
74+       throw  new  AuthorizationError ( ErrorCode . PSYNC_S2204 ,  `JWKS request failed` ,  { 
75+         configurationDetails : `JWKS URL: ${ this . url }  , 
76+         // This covers most cases of FetchError 
77+         // `cause: e` could lose the error message 
78+         cause : {  message : e . message ,  code : e . code  } 
79+       } ) ; 
80+     }  finally  { 
81+       clearTimeout ( timeout ) ; 
82+     } 
83+   } 
84+ 
85+   async  getKeys ( ) : Promise < KeyResult >  { 
86+     const  data  =  await  this . getJwksData ( ) ; 
7487
7588    // https://github.com/panva/jose/blob/358e864a0cccf1e0f9928a959f91f18f3f06a7de/src/jwks/local.ts#L36 
7689    if  ( 
@@ -81,7 +94,7 @@ export class RemoteJWKSCollector implements KeyCollector {
8194      return  { 
8295        keys : [ ] , 
8396        errors : [ 
84-           new  AuthorizationError ( ErrorCode . PSYNC_S2204 ,  `JWKS request failed with  ${ res . statusText }  ,  { 
97+           new  AuthorizationError ( ErrorCode . PSYNC_S2204 ,  `Invalid  JWKS response ` ,  { 
8598            configurationDetails : `JWKS URL: ${ this . url } ${ JSON . stringify ( data ,  null ,  2 ) }  
8699          } ) 
87100        ] 
0 commit comments