Skip to content

Alpha/task15 - Done Upgrading - Change Password #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions app/api/deps.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from collections.abc import Generator
from typing import Annotated, Any

from fastapi import Depends, HTTPException
from fastapi import Depends, HTTPException, status
from fastapi.security import OAuth2PasswordBearer
from jose import JWTError, jwt
from pymongo.database import Database
Expand All @@ -25,7 +25,7 @@ def get_user(
token: str = Depends(oauth2_scheme),
) -> UserRead:
credentials_exception = HTTPException(
status_code=401,
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)
Expand All @@ -41,13 +41,13 @@ def get_user(
user = crud.get_user_by_username(username=username)
if user is None:
raise HTTPException(
status_code=403,
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Authenticaton error",
)

if user.role is UserRole.inactive:
raise HTTPException(
status_code=403,
status_code=status.HTTP_401_UNAUTHORIZED,
detail="User account is inactive. Please contact support.",
)

Expand Down
3 changes: 3 additions & 0 deletions app/api/endpoints/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ def change_password(
current_user: Annotated[UserRead, Depends(get_user)],
request: ChangePasswordRequest,
) -> ApiGeneralResponse:
if not AuthService.verify_password(password=request.old_password, hashed=current_user.hashed_password):
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Incorrect old password")

user_update = UserUpdate.from_user_read(
user_read=current_user,
password=request.new_password,
Expand Down
2 changes: 1 addition & 1 deletion app/core/config/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


class Config(BaseSettings):
ACCESS_TOKEN_EXPIRE_MINUTS: int = 60 * 24 * 30
ACCESS_TOKEN_EXPIRE_MINUTS: int = 60 * 24 * 7 # 7 days
JWT_ALGORITHM: str = "HS256"
JWT_SECRET_KEY: str = config_env.JWT_SECRET_KEY

Expand Down
1 change: 1 addition & 0 deletions app/schemas/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@


class ChangePasswordRequest(BaseModel):
old_password: str
new_password: str