diff --git a/simple_ado/auth/ado_azid_auth.py b/simple_ado/auth/ado_azid_auth.py index 7776b8f..cc57bc1 100644 --- a/simple_ado/auth/ado_azid_auth.py +++ b/simple_ado/auth/ado_azid_auth.py @@ -1,12 +1,20 @@ """Azure Identity authentication auth class.""" +import time + from azure.identity import DefaultAzureCredential +from azure.core.credentials import AccessToken from simple_ado.auth.ado_auth import ADOAuth class ADOAzIDAuth(ADOAuth): """Azure Identity auth.""" + access_token: AccessToken | None + + def __init__(self) -> None: + self.access_token = None + def get_authorization_header(self) -> str: """Get the header value. @@ -14,9 +22,9 @@ def get_authorization_header(self) -> str: # The get_token parameter specifies the Azure DevOps resource and requests a token with # default permissions for API access. - return ( - "Bearer " - + DefaultAzureCredential() - .get_token("499b84ac-1321-427f-aa17-267ca6975798/.default") - .token - ) + if self.access_token is None or self.access_token.expires_on <= time.time() + 60: + self.access_token = DefaultAzureCredential().get_token( + "499b84ac-1321-427f-aa17-267ca6975798/.default" + ) + + return "Bearer " + self.access_token.token