@@ -30,26 +30,23 @@ class Auth0Service:
3030 def __init__ (self ):
3131 self .oauth = OAuth ()
3232 self .oauth .register (
33- ' auth0' ,
33+ " auth0" ,
3434 client_id = settings .AUTH0_CLIENT_ID ,
3535 client_secret = settings .AUTH0_CLIENT_SECRET ,
3636 server_metadata_url = (
37- f'https://{ settings .AUTH0_DOMAIN } /'
38- '.well-known/openid-configuration'
37+ f"https://{ settings .AUTH0_DOMAIN } /.well-known/openid-configuration"
3938 ),
4039 client_kwargs = {
41- ' scope' : ' openid profile email' ,
42- ' audience' : settings .AUTH0_AUDIENCE ,
40+ " scope" : " openid profile email" ,
41+ " audience" : settings .AUTH0_AUDIENCE ,
4342 },
4443 )
4544 self ._jwks = None
4645
4746 async def _get_jwks (self ) -> dict [str , Any ]:
4847 if self ._jwks is None :
4948 try :
50- jwks_url = (
51- f"https://{ settings .AUTH0_DOMAIN } /.well-known/jwks.json"
52- )
49+ jwks_url = f"https://{ settings .AUTH0_DOMAIN } /.well-known/jwks.json"
5350 async with httpx .AsyncClient () as client :
5451 response = await client .get (jwks_url )
5552 response .raise_for_status ()
@@ -58,8 +55,7 @@ async def _get_jwks(self) -> dict[str, Any]:
5855 except Exception as e :
5956 logger .error (f"Failed to fetch JWKS: { str (e )} " )
6057 raise HTTPException (
61- status_code = 500 ,
62- detail = "Authentication configuration error"
58+ status_code = 500 , detail = "Authentication configuration error"
6359 )
6460 return self ._jwks
6561
@@ -73,8 +69,7 @@ async def login(self, request: Request) -> RedirectResponse:
7369 except Exception as e :
7470 logger .error (f"Failed to initiate login: { str (e )} " )
7571 raise HTTPException (
76- status_code = 500 ,
77- detail = "Failed to initiate login. Please try again."
72+ status_code = 500 , detail = "Failed to initiate login. Please try again."
7873 )
7974
8075 async def callback (self , request : Request ) -> TokenResponse :
@@ -85,8 +80,7 @@ async def callback(self, request: Request) -> TokenResponse:
8580 except Exception as e :
8681 logger .error (f"Failed to exchange code for token: { str (e )} " )
8782 raise HTTPException (
88- status_code = 401 ,
89- detail = "Authentication failed. Please try again."
83+ status_code = 401 , detail = "Authentication failed. Please try again."
9084 )
9185
9286 async def validate_token (self , token : str ) -> dict [str , Any ]:
@@ -100,52 +94,36 @@ async def validate_token(self, token: str) -> dict[str, Any]:
10094 jwks ,
10195 claims_options = {
10296 "iss" : {"essential" : True , "value" : settings .AUTH0_ISSUER },
103- "aud" : {
104- "essential" : True ,
105- "value" : settings .AUTH0_AUDIENCE
106- },
97+ "aud" : {"essential" : True , "value" : settings .AUTH0_AUDIENCE },
10798 "exp" : {"essential" : True },
108- }
99+ },
109100 )
110101 jwt .validate_claims (
111102 claims ,
112103 {
113104 "iss" : {"essential" : True , "value" : settings .AUTH0_ISSUER },
114- "aud" : {
115- "essential" : True ,
116- "value" : settings .AUTH0_AUDIENCE
117- },
105+ "aud" : {"essential" : True , "value" : settings .AUTH0_AUDIENCE },
118106 "exp" : {"essential" : True },
119- }
107+ },
120108 )
121109
122110 # Additional validation
123- if not claims .get (' sub' ):
111+ if not claims .get (" sub" ):
124112 logger .warning ("Token validation failed: missing subject" )
125113 raise HTTPException (
126- status_code = 401 ,
127- detail = "Invalid token: missing subject"
114+ status_code = 401 , detail = "Invalid token: missing subject"
128115 )
129116
130117 return claims
131118 except jwt .ExpiredTokenError :
132119 logger .warning ("Token has expired" )
133- raise HTTPException (
134- status_code = 401 ,
135- detail = "Token has expired"
136- )
120+ raise HTTPException (status_code = 401 , detail = "Token has expired" )
137121 except jwt .InvalidTokenError as e :
138122 logger .warning (f"Invalid token: { str (e )} " )
139- raise HTTPException (
140- status_code = 401 ,
141- detail = "Invalid token"
142- )
123+ raise HTTPException (status_code = 401 , detail = "Invalid token" )
143124 except Exception as e :
144125 logger .error (f"Token validation failed: { str (e )} " )
145- raise HTTPException (
146- status_code = 401 ,
147- detail = "Invalid token"
148- )
126+ raise HTTPException (status_code = 401 , detail = "Invalid token" )
149127
150128 async def get_user_info (self , access_token : str ) -> UserInfo :
151129 try :
@@ -156,8 +134,7 @@ async def get_user_info(self, access_token: str) -> UserInfo:
156134 except Exception as e :
157135 logger .error (f"Failed to get user info: { str (e )} " )
158136 raise HTTPException (
159- status_code = 401 ,
160- detail = "Failed to retrieve user information"
137+ status_code = 401 , detail = "Failed to retrieve user information"
161138 )
162139
163140 def logout (self ) -> RedirectResponse :
0 commit comments