Skip to content

Commit 3cc67dd

Browse files
Redirect unauthed users with 303 rather than 307 so POST requests are changed to GET
1 parent 7ea6d51 commit 3cc67dd

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

utils/auth.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from datetime import UTC, datetime, timedelta
1313
from typing import Optional
1414
from fastapi import Depends, Cookie, HTTPException, status
15+
from fastapi.responses import RedirectResponse
1516
from utils.db import get_session
1617
from utils.models import User, PasswordResetToken
1718

@@ -180,7 +181,8 @@ def validate_token_and_get_user(
180181
if decoded_token:
181182
user_email = decoded_token.get("sub")
182183
user = session.exec(select(User).where(
183-
User.email == user_email)).first()
184+
User.email == user_email
185+
)).first()
184186
if user:
185187
if token_type == "refresh":
186188
new_access_token = create_access_token(
@@ -215,6 +217,14 @@ def get_user_from_tokens(
215217
return None, None, None
216218

217219

220+
class AuthenticationError(HTTPException):
221+
def __init__(self):
222+
super().__init__(
223+
status_code=status.HTTP_303_SEE_OTHER,
224+
headers={"Location": "/login"}
225+
)
226+
227+
218228
def get_authenticated_user(
219229
tokens: tuple[Optional[str], Optional[str]
220230
] = Depends(oauth2_scheme_cookie),
@@ -228,11 +238,7 @@ def get_authenticated_user(
228238
raise NeedsNewTokens(user, new_access_token, new_refresh_token)
229239
return user
230240

231-
# If both tokens are invalid or missing, redirect to login
232-
raise HTTPException(
233-
status_code=status.HTTP_307_TEMPORARY_REDIRECT,
234-
headers={"Location": "/login"}
235-
)
241+
raise AuthenticationError()
236242

237243

238244
def get_optional_user(
@@ -275,7 +281,9 @@ def generate_password_reset_url(email: str, token: str) -> str:
275281

276282
def send_reset_email(email: str, session: Session):
277283
# Check for an existing unexpired token
278-
user = session.exec(select(User).where(User.email == email)).first()
284+
user = session.exec(select(User).where(
285+
User.email == email
286+
)).first()
279287
if user:
280288
existing_token = session.exec(
281289
select(PasswordResetToken)
@@ -316,18 +324,19 @@ def send_reset_email(email: str, session: Session):
316324

317325

318326
def get_user_from_reset_token(email: str, token: str, session: Session) -> tuple[Optional[User], Optional[PasswordResetToken]]:
319-
reset_token = session.exec(select(PasswordResetToken).where(
320-
PasswordResetToken.token == token,
321-
PasswordResetToken.expires_at > datetime.now(UTC),
322-
PasswordResetToken.used == False
323-
)).first()
327+
result = session.exec(
328+
select(User, PasswordResetToken)
329+
.where(
330+
User.email == email,
331+
PasswordResetToken.token == token,
332+
PasswordResetToken.expires_at > datetime.now(UTC),
333+
PasswordResetToken.used == False,
334+
PasswordResetToken.user_id == User.id
335+
)
336+
).first()
324337

325-
if not reset_token:
338+
if not result:
326339
return None, None
327340

328-
user = session.exec(select(User).where(
329-
User.email == email,
330-
User.id == reset_token.user_id
331-
)).first()
332-
341+
user, reset_token = result
333342
return user, reset_token

0 commit comments

Comments
 (0)