backend.auth
Authentication and authorisation utilities.
Provides JWT-based access and refresh token handling, password hashing (bcrypt with legacy SHA-1 fallback), user lookup and all FastAPI dependency helpers required by the API routers.
- Constants:
ACCESS_TOKEN_EXPIRE_MINUTES (int): Lifetime of an access token in minutes (15). REFRESH_TOKEN_EXPIRE_DAYS (int): Lifetime of a refresh token in days (30). RESET_PASSWORD_TOKEN_EXPIRE_MINUTES (int): Lifetime of a password-reset token (15). ALGORITHM (str): JWT signing algorithm (
HS256).
- backend.auth.get_token_from_cookie_or_header(request)[Quellcode]
Extract a Bearer token from the
Authorizationheader or theaccess_tokencookie.The header is checked first; the cookie serves as a backwards- compatible fallback.
- Parameter:
request (Request) – The incoming FastAPI request object.
- Rückgabe:
The raw JWT string, or
Noneif no token was found.- Rückgabetyp:
str | None
- backend.auth.hash_pw(plain_pw)[Quellcode]
Hash a plain-text password using bcrypt.
- backend.auth.verify_password(plain, hashed)[Quellcode]
Verify a plain-text password against a stored hash.
Supports bcrypt hashes (
$2b$,$2a$,$2y$prefix) and, untilLEGACY_HASH_DEADLINE, LDAP-style SHA-1 hashes ({SHA}prefix).
- backend.auth.authenticate_user(db, username, password)[Quellcode]
Look up a user by username and verify their password.
If the stored hash uses a legacy format it is automatically upgraded to bcrypt on successful login.
- Parameter:
- Rückgabe:
The authenticated user, or
Noneif the credentials are invalid.- Rückgabetyp:
models.User | None
- backend.auth.create_access_token(data)[Quellcode]
Create a signed JWT access token.
The token expires after
ACCESS_TOKEN_EXPIRE_MINUTESminutes.
- backend.auth.create_refresh_token(user_id, db)[Quellcode]
Create a refresh token, persist its hash in the database and return the raw token string.
- backend.auth.verify_refresh_token(token, db)[Quellcode]
Validate a refresh token and return the associated user.
- Parameter:
token (str) – The raw refresh token string.
db (Session) – Active database session.
- Rückgabe:
The user associated with the token.
- Rückgabetyp:
- Verursacht:
HTTPException 401 – If the token is invalid, revoked or expired.
- backend.auth.revoke_refresh_token(token, db)[Quellcode]
Mark a refresh token as revoked in the database.
- Parameter:
token (str) – The raw refresh token string.
db (Session) – Active database session.
- backend.auth.blacklist_access_token(token, db)[Quellcode]
Add an access token to the blacklist so it cannot be reused after logout.
Only tokens that have not yet expired are stored; already-expired tokens are silently ignored.
- Parameter:
token (str) – The raw JWT access token string.
db (Session) – Active database session.
- backend.auth.create_password_reset_token(user_name)[Quellcode]
Create a short-lived JWT token scoped to password reset.
- backend.auth.get_db()[Quellcode]
FastAPI dependency that yields a database session.
- Yields:
Session – An active SQLAlchemy database session.
- backend.auth.get_current_user(request, db=Depends(dependency=<function get_db>, use_cache=True, scope=None))[Quellcode]
FastAPI dependency that validates the access token and returns the current user’s payload.
Checks both the token blacklist and the JWT signature/expiry.
- Parameter:
request (Request) – The incoming request (token read from header or cookie).
db (Session) – Active database session.
- Rückgabe:
A dictionary with keys
user_nameanduser_group.- Rückgabetyp:
- Verursacht:
HTTPException 401 – If the token is missing, invalid or blacklisted.
- async backend.auth.get_current_user_dep(request, db=Depends(dependency=<function get_db>, use_cache=True, scope=None))[Quellcode]
Async wrapper for router-level dependencies
- Parameter:
request (Request) – The incoming request.
db (Session) – Active database session.
- Rückgabe:
A dictionary with keys
user_nameanduser_group.- Rückgabetyp:
- backend.auth.check_user_role(token, expected_role)[Quellcode]
Verify that a JWT token carries a specific role.
- backend.auth.verify_password_reset_token(token=Depends(dependency=<fastapi.security.oauth2.OAuth2PasswordBearer object>, use_cache=True, scope=None), db=Depends(dependency=<function get_db>, use_cache=True, scope=None))[Quellcode]
Validate a password-reset token and ensure it has not been used before.