@@ -70,6 +70,10 @@ async def __call__(self, request: Request):
7070 oauth2_scheme = OAuth2PasswordBearer (tokenUrl = "token" )
7171else :
7272 oauth2_scheme = CookieScheme (cookie_key = security_config .cookie_key )
73+ if security_config .instrument_auth_type == "token" :
74+ instrument_oauth2_scheme = OAuth2PasswordBearer (tokenUrl = "token" )
75+ else :
76+ instrument_oauth2_scheme = lambda * args , ** kwargs : None
7377pwd_context = CryptContext (schemes = ["bcrypt" ], deprecated = "auto" )
7478
7579instrument_server_tokens : Dict [float , dict ] = {}
@@ -170,7 +174,35 @@ async def validate_token(token: Annotated[str, Depends(oauth2_scheme)]):
170174 )
171175 async with aiohttp .ClientSession (cookies = cookies ) as session :
172176 async with session .get (
173- f"{ auth_url } { url_path_for ('auth.router' , 'simple_token_validation' )} " ,
177+ auth_url ,
178+ headers = headers ,
179+ ) as response :
180+ success = response .status == 200
181+ validation_outcome = await response .json ()
182+ if not (success and validation_outcome .get ("valid" )):
183+ raise JWTError
184+ except JWTError :
185+ raise HTTPException (
186+ status_code = status .HTTP_401_UNAUTHORIZED ,
187+ detail = "Could not validate credentials" ,
188+ headers = {"WWW-Authenticate" : "Bearer" },
189+ )
190+ return None
191+
192+
193+ async def validate_instrument_token (
194+ token : Annotated [str , Depends (instrument_oauth2_scheme )]
195+ ):
196+ try :
197+ if security_config .instrument_auth_url :
198+ async with aiohttp .ClientSession () as session :
199+ headers = (
200+ {}
201+ if not security_config .instrument_auth_type
202+ else {"Authorization" : f"Bearer { token } " }
203+ )
204+ async with session .get (
205+ security_config .instrument_auth_url ,
174206 headers = headers ,
175207 ) as response :
176208 success = response .status == 200
0 commit comments