Skip to content

Commit f936785

Browse files
committed
Feat: don't allow changing to the same password
1 parent 39eb4fc commit f936785

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

app/core/users/endpoints_users.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,12 +643,29 @@ async def reset_password(
643643
),
644644
)
645645

646+
user = await cruds_users.get_user_by_id(db=db, user_id=recover_request.user_id)
647+
if user.should_change_password:
648+
# we control whether we check if the new password is different
649+
if security.verify_password(
650+
reset_password_request.new_password,
651+
user.password_hash,
652+
):
653+
raise HTTPException(
654+
status_code=403,
655+
detail="The new password should not be identical to the current password",
656+
)
657+
646658
new_password_hash = security.get_password_hash(reset_password_request.new_password)
647659
await cruds_users.update_user_password_by_id(
648660
db=db,
649661
user_id=recover_request.user_id,
650662
new_password_hash=new_password_hash,
651663
)
664+
await cruds_users.update_should_user_change_password_by_id(
665+
db=db,
666+
user_id=recover_request.user_id,
667+
should_change_password=False,
668+
)
652669

653670
# As the user has reset its password, all other recovery requests can be deleted from the table
654671
await cruds_users.delete_recover_request_by_email(
@@ -825,12 +842,28 @@ async def change_password(
825842
if user is None:
826843
raise HTTPException(status_code=403, detail="The old password is invalid")
827844

845+
if user.should_change_password:
846+
# we control whether we check if the new password is different
847+
if security.verify_password(
848+
change_password_request.new_password,
849+
user.password_hash,
850+
):
851+
raise HTTPException(
852+
status_code=403,
853+
detail="The new password should not be identical to the current password",
854+
)
855+
828856
new_password_hash = security.get_password_hash(change_password_request.new_password)
829857
await cruds_users.update_user_password_by_id(
830858
db=db,
831859
user_id=user.id,
832860
new_password_hash=new_password_hash,
833861
)
862+
await cruds_users.update_should_user_change_password_by_id(
863+
db=db,
864+
user_id=user.id,
865+
should_change_password=False,
866+
)
834867

835868
# Revoke existing auth refresh tokens
836869
# to force the user to reauthenticate on all services and devices

0 commit comments

Comments
 (0)