2121
2222from .bloom import BloomFilter
2323from .cache import Cache
24- from .config import Config
25- from .config import CLIENT_INFO_EXPIRATION , CLIENT_INFORMATION_PATH , GET_ROLE_PATH , GRANT_PATH , JWKS_PATH , \
26- MAX_BACKOFF_TIME , REVOCATION_LIST_PATH , SCOPE_SEPARATOR , VERIFY_PATH
24+ from .config import DEFAULT_JWKS_REFRESH_INTERVAL , DEFAULT_REVOCATION_LIST_REFRESH_INTERVAL , Config
25+ from .config import CLIENT_INFO_EXPIRATION , CLIENT_INFORMATION_PATH , DEFAULT_TOKEN_REFRESH_RATE , GET_ROLE_PATH , \
26+ GRANT_PATH , JWKS_PATH , MAX_BACKOFF_TIME , REVOCATION_LIST_PATH , SCOPE_SEPARATOR , VERIFY_PATH
2727from .errors import ClientTokenGrantError , GetClientInformationError , GetJWKSError , GetRevocationListError , \
2828 GetRolePermissionError , HTTPClientError , InvalidTokenSignatureKeyError , NilClaimError , NoLocalValidationError , \
2929 RefreshAccessTokenError , StartLocalValidationError , TokenRevokedError , UserRevokedError , ValidateAccessTokenError , \
3030 ValidateAndParseClaimsError , ValidateAudienceError , ValidateJWTError , ValidatePermissionError , ValidateScopeError
3131from .models import ClientInformation , JWTClaims , Permission , RevocationList , Role , TokenResponse
3232from .log import logger
33+ from .task import Task
3334from .utils import parse_nanotimestamp
3435
3536
@@ -81,7 +82,9 @@ def __init__(self, config: Config,
8182 httpClient : HttpClient
8283 ) -> None :
8384 self ._lock = RLock ()
85+ self ._threads = {}
8486 self ._clientAccessToken = ""
87+ self ._tokenRefreshActive = False
8588 self ._localValidationActive = False
8689 self ._jwks = {}
8790 self ._revokedUsers = {}
@@ -390,7 +393,12 @@ def ClientTokenGrant(self) -> None:
390393 self ._clientAccessToken = token_response .AccessToken
391394 logger .info ("token grant success" )
392395
393- # TODO: Background refresh token
396+ if not self ._tokenRefreshActive :
397+ self ._tokenRefreshActive = True
398+ self ._threads ["refresh_token" ] = Task (
399+ token_response .ExpiresIn * DEFAULT_TOKEN_REFRESH_RATE ,
400+ self ._refresh_access_token
401+ )
394402
395403 except (json .JSONDecodeError , ValueError ) as e :
396404 raise ClientTokenGrantError ("unable to unmarshal response body" ) from e
@@ -410,9 +418,17 @@ def StartLocalValidation(self) -> None:
410418 try :
411419 self ._get_jwks ()
412420 self ._get_revocation_list ()
413- self ._localValidationActive = True
414421
415- # TODO: Background refresh JWKS and Revocation list
422+ if not self ._localValidationActive :
423+ self ._localValidationActive = True
424+ self ._threads ["refresh_jwks" ] = Task (
425+ DEFAULT_JWKS_REFRESH_INTERVAL ,
426+ self ._get_jwks
427+ )
428+ self ._threads ["refresh_revocation" ] = Task (
429+ DEFAULT_REVOCATION_LIST_REFRESH_INTERVAL ,
430+ self ._get_revocation_list
431+ )
416432
417433 except GetJWKSError as e :
418434 raise StartLocalValidationError ("unable to get JWKS" ) from e
@@ -641,7 +657,33 @@ def HealthCheck(self) -> bool:
641657 Returns:
642658 bool: health status
643659 """
644- return False
660+ with self ._lock :
661+ refresh_token = self ._threads .get ("refresh_token" )
662+ refresh_jwks = self ._threads .get ("refresh_jwks" )
663+ refresh_revocation = self ._threads .get ("refresh_revocation" )
664+
665+ if not refresh_token or not refresh_jwks or not refresh_revocation :
666+ logger .warning ("refresh token, jwks or revocation list background thread not started" )
667+ return False
668+
669+ if refresh_token .error is not None :
670+ logger .warning ("error refresh token" )
671+ logger .error (refresh_token .error )
672+ return False
673+
674+ if refresh_jwks .error is not None :
675+ logger .warning ("error refresh jwks" )
676+ logger .error (refresh_jwks .error )
677+ return False
678+
679+ if refresh_revocation .error is not None :
680+ logger .warning ("error refresh revocation list" )
681+ logger .error (refresh_revocation .error )
682+ return False
683+
684+ logger .info ("all OK" )
685+
686+ return True
645687
646688 def ValidateAudience (self , claims : Union [JWTClaims , None ]) -> None :
647689 """Validate audience of user access token
0 commit comments