@@ -318,7 +318,11 @@ def do_local_jwk(self, filename):
318
318
Load a JWKS from a local file
319
319
320
320
:param filename: Name of the file from which the JWKS should be loaded
321
+ :return: True if load was successful or False if file hasn't been modified
321
322
"""
323
+ if not self ._local_update_required ():
324
+ return False
325
+
322
326
LOGGER .info ("Reading local JWKS from %s" , filename )
323
327
with open (filename ) as input_file :
324
328
_info = json .load (input_file )
@@ -328,6 +332,7 @@ def do_local_jwk(self, filename):
328
332
self .do_keys ([_info ])
329
333
self .last_local = time .time ()
330
334
self .time_out = self .last_local + self .cache_time
335
+ return True
331
336
332
337
def do_local_der (self , filename , keytype , keyusage = None , kid = "" ):
333
338
"""
@@ -336,7 +341,11 @@ def do_local_der(self, filename, keytype, keyusage=None, kid=""):
336
341
:param filename: Name of the file
337
342
:param keytype: Presently 'rsa' and 'ec' supported
338
343
:param keyusage: encryption ('enc') or signing ('sig') or both
344
+ :return: True if load was successful or False if file hasn't been modified
339
345
"""
346
+ if not self ._local_update_required ():
347
+ return False
348
+
340
349
LOGGER .info ("Reading local DER from %s" , filename )
341
350
key_args = {}
342
351
_kty = keytype .lower ()
@@ -359,12 +368,13 @@ def do_local_der(self, filename, keytype, keyusage=None, kid=""):
359
368
self .do_keys ([key_args ])
360
369
self .last_local = time .time ()
361
370
self .time_out = self .last_local + self .cache_time
371
+ return True
362
372
363
373
def do_remote (self ):
364
374
"""
365
375
Load a JWKS from a webpage.
366
376
367
- :return: True or False if load was successful
377
+ :return: True if load was successful or False if remote hasn't been modified
368
378
"""
369
379
# if self.verify_ssl is not None:
370
380
# self.httpc_params["verify"] = self.verify_ssl
@@ -390,7 +400,10 @@ def do_remote(self):
390
400
LOGGER .error (err )
391
401
raise UpdateFailed (REMOTE_FAILED .format (self .source , str (err )))
392
402
393
- if _http_resp .status_code == 200 : # New content
403
+ load_successful = _http_resp .status_code == 200
404
+ not_modified = _http_resp .status_code == 304
405
+
406
+ if load_successful :
394
407
self .time_out = time .time () + self .cache_time
395
408
396
409
self .imp_jwks = self ._parse_remote_response (_http_resp )
@@ -408,11 +421,9 @@ def do_remote(self):
408
421
if hasattr (_http_resp , "headers" ):
409
422
headers = getattr (_http_resp , "headers" )
410
423
self .last_remote = headers .get ("last-modified" ) or headers .get ("date" )
411
-
412
- elif _http_resp .status_code == 304 : # Not modified
424
+ elif not_modified :
413
425
LOGGER .debug ("%s not modified since %s" , self .source , self .last_remote )
414
426
self .time_out = time .time () + self .cache_time
415
-
416
427
else :
417
428
LOGGER .warning (
418
429
"HTTP status %d reading remote JWKS from %s" ,
@@ -424,7 +435,7 @@ def do_remote(self):
424
435
425
436
self .last_updated = time .time ()
426
437
self .ignore_errors_until = None
427
- return True
438
+ return load_successful
428
439
429
440
def _parse_remote_response (self , response ):
430
441
"""
@@ -449,23 +460,20 @@ def _parse_remote_response(self, response):
449
460
return None
450
461
451
462
def _uptodate (self ):
452
- res = False
453
463
if self .remote or self .local :
454
464
if time .time () > self .time_out :
455
- if self .local and not self ._local_update_required ():
456
- res = True
457
- elif self .update ():
458
- res = True
459
- return res
465
+ return self .update ()
466
+ return False
460
467
461
468
def update (self ):
462
469
"""
463
470
Reload the keys if necessary.
464
471
465
472
This is a forced update, will happen even if cache time has not elapsed.
466
473
Replaced keys will be marked as inactive and not removed.
474
+
475
+ :return: True if update was ok or False if we encountered an error during update.
467
476
"""
468
- res = True # An update was successful
469
477
if self .source :
470
478
_old_keys = self ._keys # just in case
471
479
@@ -475,24 +483,27 @@ def update(self):
475
483
try :
476
484
if self .local :
477
485
if self .fileformat in ["jwks" , "jwk" ]:
478
- self .do_local_jwk (self .source )
486
+ updated = self .do_local_jwk (self .source )
479
487
elif self .fileformat == "der" :
480
- self .do_local_der (self .source , self .keytype , self .keyusage )
488
+ updated = self .do_local_der (self .source , self .keytype , self .keyusage )
481
489
elif self .remote :
482
- res = self .do_remote ()
490
+ updated = self .do_remote ()
483
491
except Exception as err :
484
492
LOGGER .error ("Key bundle update failed: %s" , err )
485
493
self ._keys = _old_keys # restore
486
494
return False
487
495
488
- now = time .time ()
489
- for _key in _old_keys :
490
- if _key not in self ._keys :
491
- if not _key .inactive_since : # If already marked don't mess
492
- _key .inactive_since = now
493
- self ._keys .append (_key )
496
+ if updated :
497
+ now = time .time ()
498
+ for _key in _old_keys :
499
+ if _key not in self ._keys :
500
+ if not _key .inactive_since : # If already marked don't mess
501
+ _key .inactive_since = now
502
+ self ._keys .append (_key )
503
+ else :
504
+ self ._keys = _old_keys
494
505
495
- return res
506
+ return True
496
507
497
508
def get (self , typ = "" , only_active = True ):
498
509
"""
0 commit comments