Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /mdfarhankc_fastapi-fullauth /fastapi_fullauth /flows /login.py
| import logging | |
| from typing import Any, Literal | |
| from fastapi_fullauth.adapters.base import AbstractUserAdapter | |
| from fastapi_fullauth.core.crypto import ahash_password, averify_password, password_needs_rehash | |
| from fastapi_fullauth.core.tokens import TokenEngine | |
| from fastapi_fullauth.exceptions import AccountLockedError, AuthenticationError | |
| from fastapi_fullauth.flows.tokens import ClaimsProvider, issue_token_pair | |
| from fastapi_fullauth.protection.lockout import LockoutManager | |
| from fastapi_fullauth.types import TokenPair, UserSchema | |
| logger = logging.getLogger("fastapi_fullauth.login") | |
| _DUMMY_HASHES: dict[str, str] = {} | |
| async def _get_dummy_hash(algorithm: Literal["argon2id", "bcrypt"] = "argon2id") -> str: | |
| """Lazily build a valid hash so timing-defense verifies take real time. | |
| Cached per algorithm: the dummy verify must use the same hasher the real | |
| users do, otherwise a bcrypt deployment still leaks user existence through | |
| the argon2-vs-bcrypt timing difference on the unknown-user path. | |
| """ | |
| if algorithm not in _DUMMY_HASHES: | |
| _DUMMY_HASHES[algorithm] = await ahash_password( | |
| "fastapi-fullauth-timing-defense", algorithm=algorithm | |
| ) | |
| return _DUMMY_HASHES[algorithm] | |
| async def login( | |
| adapter: AbstractUserAdapter, | |
| token_engine: TokenEngine, | |
| identifier: str, | |
| password: str, | |
| login_field: str = "email", | |
| lockout: LockoutManager | None = None, | |
| extra_claims: dict[str, Any] | None = None, | |
| extra_claims_provider: ClaimsProvider | None = None, | |
| user: UserSchema | None = None, | |
| hash_algorithm: Literal["argon2id", "bcrypt"] = "argon2id", | |
| prevent_timing_attacks: bool = False, | |
| user_agent: str | None = None, | |
| ip_address: str | None = None, | |
| ) -> TokenPair: | |
| if lockout and await lockout.is_locked(identifier): | |
| logger.warning("Login blocked; account locked: %s", identifier) | |
| raise AccountLockedError("Account is temporarily locked") | |
| if user is None: | |
| user = await adapter.get_user_by_field(login_field, identifier) | |
| hashed: str | None = None | |
| if user is not None: | |
| hashed = await adapter.get_hashed_password(user.id) | |
| if user is None or hashed is None: | |
| if prevent_timing_attacks: | |
| await averify_password(password, await _get_dummy_hash(hash_algorithm)) | |
| if lockout: | |
| await lockout.record_failure(identifier) | |
| logger.warning("Login failed; unknown user or no password: %s", identifier) | |
| raise AuthenticationError("Invalid credentials") | |
| if not await averify_password(password, hashed): | |
| if lockout: | |
| await lockout.record_failure(identifier) | |
| logger.warning("Login failed; invalid password: %s", identifier) | |
| raise AuthenticationError("Invalid credentials") | |
| if not user.is_active: | |
| logger.warning("Login failed; account deactivated: %s", identifier) | |
| raise AuthenticationError("User account is deactivated") | |
| if password_needs_rehash(hashed, algorithm=hash_algorithm): | |
| try: | |
| await adapter.set_password( | |
| user.id, await ahash_password(password, algorithm=hash_algorithm) | |
| ) | |
| except Exception: | |
| # A transient DB error here mustn't block a successful login. | |
| logger.exception("Password rehash failed for user_id=%s", user.id) | |
| if lockout: | |
| await lockout.clear(identifier) | |
| # Only now that authentication succeeded: the app's claims hook must not | |
| # run (or hit its own services) for failed or probing login attempts. | |
| if extra_claims_provider is not None: | |
| extra_claims = await extra_claims_provider(user) | |
| logger.info("Login successful: user_id=%s", user.id) | |
| return await issue_token_pair( | |
| adapter, | |
| token_engine, | |
| user, | |
| extra_claims=extra_claims, | |
| user_agent=user_agent, | |
| ip_address=ip_address, | |
| ) | |
Xet Storage Details
- Size:
- 3.92 kB
- Xet hash:
- 8633266156b07a54649b4ce63bd8b88a1729eec14f744d7ee5ac8f2e985f9de1
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.