This commit is contained in:
dni ⚡
2024-10-17 10:37:33 +02:00
parent a58a698918
commit f6d87e3c36
2 changed files with 21 additions and 18 deletions
+17 -16
View File
@@ -271,32 +271,33 @@ async def reset_password(data: ResetUserPassword) -> JSONResponse:
raise HTTPException( raise HTTPException(
HTTPStatus.UNAUTHORIZED, "Auth by 'Username and Password' not allowed." HTTPStatus.UNAUTHORIZED, "Auth by 'Username and Password' not allowed."
) )
if not data.reset_key[:10].startswith("reset_key_"):
assert data.reset_key[:10] == "reset_key_", "This is not a reset key." raise HTTPException(HTTPStatus.BAD_REQUEST, "This is not a reset key.")
reset_data_json = decrypt_internal_message( reset_data_json = decrypt_internal_message(
base64.b64decode(data.reset_key[10:]).decode() base64.b64decode(data.reset_key[10:]).decode()
) )
assert reset_data_json, "Cannot process reset key." if not reset_data_json:
raise HTTPException(HTTPStatus.BAD_REQUEST, "Cannot process reset key.")
action, user_id, request_time = json.loads(reset_data_json) action, user_id, request_time = json.loads(reset_data_json)
assert action == "reset", "Expected reset action." if not action:
assert user_id is not None, "Missing user ID." raise HTTPException(HTTPStatus.BAD_REQUEST, "Missing action.")
assert request_time is not None, "Missing reset time." if not user_id:
raise HTTPException(HTTPStatus.BAD_REQUEST, "Missing user ID.")
if not request_time:
raise HTTPException(HTTPStatus.BAD_REQUEST, "Missing reset time.")
user = await get_account(user_id) _validate_auth_timeout(request_time)
assert user, "User not found."
update_pwd = UpdateUserPassword( account = await get_account(user_id)
user_id=user.id, if not account:
username=user.username or "", raise HTTPException(HTTPStatus.NOT_FOUND, "User not found.")
password=data.password,
password_repeat=data.password_repeat,
)
user = await update_user_password(update_pwd, request_time)
account.hash_password(data.password)
await update_account(account)
return _auth_success_response( return _auth_success_response(
username=user.username, user_id=user_id, email=user.email username=account.username, user_id=user_id, email=account.email
) )
+4 -2
View File
@@ -1,6 +1,6 @@
import json import json
import re import re
from datetime import UTC, datetime, timedelta from datetime import datetime, timedelta, timezone
from pathlib import Path from pathlib import Path
from typing import Any, Optional, Type from typing import Any, Optional, Type
@@ -184,7 +184,9 @@ def is_valid_username(username: str) -> bool:
def create_access_token(data: dict): def create_access_token(data: dict):
expire = datetime.now(UTC) + timedelta(minutes=settings.auth_token_expire_minutes) expire = datetime.now(timezone.utc) + timedelta(
minutes=settings.auth_token_expire_minutes
)
to_encode = data.copy() to_encode = data.copy()
to_encode.update({"exp": expire}) to_encode.update({"exp": expire})
return jwt.encode(to_encode, settings.auth_secret_key, "HS256") return jwt.encode(to_encode, settings.auth_secret_key, "HS256")