11import asyncio
2- import httpx
32import time
3+ from typing import Optional
44
5- from cachetools import cached , TTLCache
5+ import httpx
6+ from cachetools import TTLCache , cached
67from fastapi import Header
78from httpx import AsyncClient , BasicAuth
89from opal_common .authentication .authenticator import Authenticator
1112from opal_common .authentication .signer import JWTSigner
1213from opal_common .authentication .verifier import JWTVerifier , Unauthorized
1314from opal_common .config import opal_common_config
14- from typing import Optional
1515
1616class _OAuth2Authenticator (Authenticator ):
1717 async def authenticate (self , headers ):
1818 if "Authorization" not in headers :
1919 token = await self .token ()
20- headers [' Authorization' ] = f"Bearer { token } "
20+ headers [" Authorization" ] = f"Bearer { token } "
2121
2222
2323class OAuth2ClientCredentialsAuthenticator (_OAuth2Authenticator ):
@@ -61,7 +61,7 @@ async def token(self):
6161
6262 async with AsyncClient () as client :
6363 response = await client .post (self ._token_url , auth = auth , data = data )
64- return (response .json ())[' access_token' ]
64+ return (response .json ())[" access_token" ]
6565
6666 def __call__ (self , authorization : Optional [str ] = Header (None )) -> {}:
6767 token = get_token_from_header (authorization )
@@ -79,10 +79,12 @@ def verify(self, token: str) -> {}:
7979 return claims
8080
8181 def _verify_opaque (self , token : str ) -> {}:
82- response = httpx .post (self ._introspect_url , data = {' token' : token })
82+ response = httpx .post (self ._introspect_url , data = {" token" : token })
8383
8484 if response .status_code != httpx .codes .OK :
85- raise Unauthorized (description = f"invalid status code { response .status_code } " )
85+ raise Unauthorized (
86+ description = f"invalid status code { response .status_code } "
87+ )
8688
8789 claims = response .json ()
8890 active = claims .get ("active" , False )
@@ -152,13 +154,15 @@ async def token(self):
152154 claims = self ._delegate .verify (token )
153155
154156 self ._token = token
155- self ._exp = claims [' exp' ]
157+ self ._exp = claims [" exp" ]
156158
157159 return self ._token
158160
159- @cached (cache = TTLCache (
160- maxsize = opal_common_config .OAUTH2_TOKEN_VERIFY_CACHE_MAXSIZE ,
161- ttl = opal_common_config .OAUTH2_TOKEN_VERIFY_CACHE_TTL
162- ))
161+ @cached (
162+ cache = TTLCache (
163+ maxsize = opal_common_config .OAUTH2_TOKEN_VERIFY_CACHE_MAXSIZE ,
164+ ttl = opal_common_config .OAUTH2_TOKEN_VERIFY_CACHE_TTL
165+ )
166+ )
163167 def __call__ (self , authorization : Optional [str ] = Header (None )) -> {}:
164168 return self ._delegate (authorization )
0 commit comments