Skip to content

Commit 2f7f91f

Browse files
fix: apply ruff formating
1 parent 6f596a0 commit 2f7f91f

File tree

3 files changed

+31
-59
lines changed

3 files changed

+31
-59
lines changed

backend/src/auth/auth0_api.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from src.dependencies.auth0 import (
99
get_auth0_service,
1010
get_current_user,
11-
get_current_user_claims
11+
get_current_user_claims,
1212
)
1313
from src.services.auth0 import Auth0Service
1414
from src.users.auth0 import get_or_create_user_from_auth0
@@ -20,8 +20,7 @@
2020

2121
@router.get("/login")
2222
async def login(
23-
request: Request,
24-
auth_service: Annotated[Auth0Service, Depends(get_auth0_service)]
23+
request: Request, auth_service: Annotated[Auth0Service, Depends(get_auth0_service)]
2524
) -> RedirectResponse:
2625
return await auth_service.login(request)
2726

@@ -30,7 +29,7 @@ async def login(
3029
async def callback(
3130
request: Request,
3231
session: Annotated[Session, Depends(get_db)],
33-
auth_service: Annotated[Auth0Service, Depends(get_auth0_service)]
32+
auth_service: Annotated[Auth0Service, Depends(get_auth0_service)],
3433
) -> RedirectResponse:
3534
try:
3635
# Exchange auth code for tokens
@@ -58,28 +57,25 @@ async def callback(
5857

5958
@router.get("/logout")
6059
async def logout(
61-
auth_service: Annotated[Auth0Service, Depends(get_auth0_service)]
60+
auth_service: Annotated[Auth0Service, Depends(get_auth0_service)],
6261
) -> RedirectResponse:
6362
return auth_service.logout()
6463

6564

6665
@router.get("/me", response_model=UserPublic)
6766
async def read_users_me(
68-
current_user: Annotated[User, Depends(get_current_user)]
67+
current_user: Annotated[User, Depends(get_current_user)],
6968
) -> User:
7069
return current_user
7170

7271

7372
@router.get("/validate")
7473
async def validate_token(
75-
claims: Annotated[dict[str, Any], Depends(get_current_user_claims)]
74+
claims: Annotated[dict[str, Any], Depends(get_current_user_claims)],
7675
) -> dict[str, Any]:
7776
return claims
7877

7978

8079
@router.get("/error")
8180
async def auth_error(message: str = "Authentication error"):
82-
raise HTTPException(
83-
status_code=401,
84-
detail=message
85-
)
81+
raise HTTPException(status_code=401, detail=message)

backend/src/dependencies/auth0.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def get_auth0_service() -> Auth0Service:
2727

2828

2929
async def get_token_from_header(
30-
credentials: Annotated[HTTPAuthorizationCredentials, Depends(security)]
30+
credentials: Annotated[HTTPAuthorizationCredentials, Depends(security)],
3131
) -> str:
3232
"""
3333
Extract and return the JWT token from Authorization header.
@@ -37,7 +37,7 @@ async def get_token_from_header(
3737

3838
async def get_current_user_claims(
3939
token: Annotated[str, Depends(get_token_from_header)],
40-
auth_service: Annotated[Auth0Service, Depends(get_auth0_service)]
40+
auth_service: Annotated[Auth0Service, Depends(get_auth0_service)],
4141
) -> dict[str, Any]:
4242
"""
4343
Validate token and return the user claims.
@@ -51,12 +51,12 @@ async def get_current_user_info(
5151
request: Request,
5252
token: Annotated[str, Depends(get_token_from_header)],
5353
claims: Annotated[dict[str, Any], Depends(get_current_user_claims)],
54-
auth_service: Annotated[Auth0Service, Depends(get_auth0_service)]
54+
auth_service: Annotated[Auth0Service, Depends(get_auth0_service)],
5555
) -> UserInfo:
5656
"""
5757
Get user information from Auth0.
5858
"""
59-
# Access token should be available in the request's session
59+
# Access token should be available in the request's session
6060
# after the callback flow
6161
access_token = request.session.get("access_token")
6262
if not access_token:
@@ -69,7 +69,7 @@ async def get_current_user_info(
6969
async def get_current_user(
7070
request: Request,
7171
session: Annotated[Session, Depends(get_db)],
72-
user_info: Annotated[UserInfo, Depends(get_current_user_info)]
72+
user_info: Annotated[UserInfo, Depends(get_current_user_info)],
7373
) -> User:
7474
"""
7575
Get the current user from the database.
@@ -104,6 +104,5 @@ async def get_current_user(
104104

105105
except Exception as e:
106106
raise HTTPException(
107-
status_code=401,
108-
detail=f"User integration failed: {str(e)}"
107+
status_code=401, detail=f"User integration failed: {str(e)}"
109108
)

backend/src/services/auth0.py

Lines changed: 18 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)