33from datetime import datetime , timedelta
44from typing import Any , Optional
55
6- from aiohttp import ClientSession
76from cryptography .hazmat .backends .openssl .backend import backend
87from cryptography .hazmat .primitives .asymmetric .types import PUBLIC_KEY_TYPES as KeyTypes
98from cryptography .x509 import load_der_x509_certificate
109from fastapi import HTTPException , status
10+ from httpx import AsyncClient
1111
1212log = logging .getLogger ('fastapi_azure_auth' )
1313
@@ -29,7 +29,6 @@ def __init__(
2929 self .authorization_endpoint : str
3030 self .signing_keys : dict [str , KeyTypes ]
3131 self .token_endpoint : str
32- self .end_session_endpoint : str
3332 self .issuer : str
3433
3534 async def load_config (self ) -> None :
@@ -57,7 +56,6 @@ async def load_config(self) -> None:
5756 log .info ('fastapi-azure-auth loaded settings from Azure AD.' )
5857 log .info ('authorization endpoint: %s' , self .authorization_endpoint )
5958 log .info ('token endpoint: %s' , self .token_endpoint )
60- log .info ('end session endpoint: %s' , self .end_session_endpoint )
6159 log .info ('issuer: %s' , self .issuer )
6260
6361 async def _load_openid_config (self ) -> None :
@@ -73,24 +71,21 @@ async def _load_openid_config(self) -> None:
7371 if self .app_id :
7472 config_url += f'?appid={ self .app_id } '
7573
76- log .info ('Trying to get OpenID Connect config from %s' , config_url )
77- async with ClientSession () as session :
78- # Fetch openid config
79- async with session .get (config_url , timeout = 10 ) as openid_response :
80- openid_response .raise_for_status ()
81- openid_cfg = await openid_response .json ()
82- jwks_uri = openid_cfg ['jwks_uri' ]
83- # Fetch keys
84- log .info ('Fetching jwks from %s' , jwks_uri )
85- async with session .get (jwks_uri , timeout = 10 ) as jwks_response :
86- jwks_response .raise_for_status ()
87- keys = await jwks_response .json ()
88- self ._load_keys (keys ['keys' ])
74+ async with AsyncClient (timeout = 10 ) as client :
75+ log .info ('Fetching OpenID Connect config from %s' , config_url )
76+ openid_response = await client .get (config_url )
77+ openid_response .raise_for_status ()
78+ openid_cfg = openid_response .json ()
8979
90- self .authorization_endpoint = openid_cfg ['authorization_endpoint' ]
91- self .token_endpoint = openid_cfg ['token_endpoint' ]
92- self .end_session_endpoint = openid_cfg ['end_session_endpoint' ]
93- self .issuer = openid_cfg ['issuer' ]
80+ self .authorization_endpoint = openid_cfg ['authorization_endpoint' ]
81+ self .token_endpoint = openid_cfg ['token_endpoint' ]
82+ self .issuer = openid_cfg ['issuer' ]
83+
84+ jwks_uri = openid_cfg ['jwks_uri' ]
85+ log .info ('Fetching jwks from %s' , jwks_uri )
86+ jwks_response = await client .get (jwks_uri )
87+ jwks_response .raise_for_status ()
88+ self ._load_keys (jwks_response .json ()['keys' ])
9489
9590 def _load_keys (self , keys : list [dict [str , Any ]]) -> None :
9691 """
0 commit comments