@@ -121,15 +121,17 @@ impl AuthenticationExtension for Jwt {
121121
122122impl Jwt {
123123 fn decoder ( & mut self ) -> Result < Decoder < ' _ > , ErrorResponse > {
124+ // If we don't have the JWKS yet or poll interval expired, we try to load the JWKS.
124125 if self
125126 . jwks
126127 . as_ref ( )
127128 . is_none_or ( |( _, ts) | ts. elapsed ( ) > self . config . poll_interval )
128129 {
129130 let ts = Instant :: now ( ) ;
130- let ( jwks , bytes) = self
131+ let ( on_cache_miss , bytes) = self
131132 . jwks_cache
132- . get_or_insert ( self . config . url . as_str ( ) , || {
133+ . try_get_or_insert ( self . config . url . as_str ( ) , || {
134+ log:: debug!( "Fetching JWKS from {}" , self . config. url) ;
133135 let request = HttpRequest :: get ( self . config . url . clone ( ) ) . build ( ) ;
134136 let response = http:: execute ( request) ?;
135137 let bytes = response. into_bytes ( ) ;
@@ -140,7 +142,9 @@ impl Jwt {
140142 log:: error!( "Failed to retrieve JWKS: {err}" ) ;
141143 ErrorResponse :: internal_server_error ( )
142144 } ) ?;
143- let jwks: Jwks = match jwks {
145+ // Either it's a cache hit and we need to parse the bytes or it's a cache miss and we
146+ // parsed the JWKS already.
147+ let jwks: Jwks = match on_cache_miss {
144148 Some ( jwks) => jwks,
145149 _ => serde_json:: from_slice ( & bytes) . map_err ( |err| {
146150 log:: error!( "Failed to parse JWKS: {err}" ) ;
0 commit comments