docs: record auth hash alignment and validation results
This commit is contained in:
1 parent
22887fef03
commit
f7819904b9
51 files changed
+1792
-542
No files matched your search
@@ -56,6 +56,7 @@ from __future__ import annotations
|
||||
|
||||
import abc
|
||||
import os
|
||||
from http.cookies import CookieError, SimpleCookie
|
||||
from typing import Any, Callable, Optional
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Request, Response
|
||||
@@ -72,6 +73,7 @@ from .authclient import (
|
||||
forwarded_client_ip,
|
||||
)
|
||||
from .clientip import client_address, parse_trusted_proxies, trusted_proxies_from_env
|
||||
from .coreconfig import MIN_INTERNAL_SECRET_LENGTH, internal_secret_matches
|
||||
from .ratelimit import AttemptLimiter
|
||||
|
||||
__all__ = ["ProxyStore", "build_auth_router"]
|
||||
@@ -104,7 +106,10 @@ ERR_FEATURE_REQUIRES_AUTHGUARD = "This feature requires AUTHGUARD_URL to be conf
|
||||
# rather than a generic "internal error" indistinguishable from any other bug.
|
||||
ERR_AUTHGUARD_UNAVAILABLE = "Unable to reach the authentication service. Please try again shortly."
|
||||
ERR_AUTHGUARD_NOT_CONFIGURED = "AUTHGUARD_URL is not configured, so sessions cannot be verified."
|
||||
ERR_SECRET_NOT_SET = "SECRET environment variable is not set."
|
||||
ERR_SECRET_NOT_SET = (
|
||||
"SECRET environment variable is unset, shorter than "
|
||||
f"{MIN_INTERNAL_SECRET_LENGTH} characters, or an example placeholder."
|
||||
)
|
||||
ERR_ADMIN_REQUIRED = "Administrator access required."
|
||||
|
||||
# _LOCAL_PROFILE is the fixed placeholder identity every handler reports when
|
||||
@@ -175,6 +180,8 @@ class UpdateMeRequest(BaseModel):
|
||||
email: str = ""
|
||||
username: str = ""
|
||||
password: str = ""
|
||||
currentPassword: str = ""
|
||||
totpToken: str = ""
|
||||
|
||||
|
||||
class UpdateAccessRequest(BaseModel):
|
||||
@@ -333,8 +340,30 @@ def build_auth_router(
|
||||
)
|
||||
|
||||
def _forward_cookies(response: Response, cookies: list[str]) -> None:
|
||||
for cookie in cookies:
|
||||
response.headers.append("set-cookie", cookie)
|
||||
for header in cookies:
|
||||
parsed = SimpleCookie()
|
||||
try:
|
||||
parsed.load(header)
|
||||
except CookieError:
|
||||
continue
|
||||
for morsel in parsed.values():
|
||||
morsel["httponly"] = True
|
||||
morsel["samesite"] = "lax"
|
||||
if secure_cookie:
|
||||
morsel["secure"] = True
|
||||
response.headers.append("set-cookie", morsel.OutputString())
|
||||
|
||||
def _temporary_session_token(cookies: list[str]) -> str:
|
||||
for header in cookies:
|
||||
parsed = SimpleCookie()
|
||||
try:
|
||||
parsed.load(header)
|
||||
except CookieError:
|
||||
continue
|
||||
morsel = parsed.get(cookie_name)
|
||||
if morsel is not None and morsel.value:
|
||||
return morsel.value
|
||||
return ""
|
||||
|
||||
async def _current_profile(
|
||||
request: Request, client: AuthClient
|
||||
@@ -610,14 +639,42 @@ def build_auth_router(
|
||||
email = _normalize_email(req.email)
|
||||
username = _normalize_username(req.username)
|
||||
password = None
|
||||
temporary_token = ""
|
||||
if req.password:
|
||||
_validate_password(req.password)
|
||||
if not req.currentPassword:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="Current password is required to change the password.",
|
||||
)
|
||||
if current.totp_enabled and not req.totpToken.strip():
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="A current authenticator code is required to change the password.",
|
||||
)
|
||||
try:
|
||||
reauthenticated = await client.login(
|
||||
current.username,
|
||||
req.currentPassword,
|
||||
req.totpToken,
|
||||
)
|
||||
except AuthClientError as exc:
|
||||
_raise_authguard_error(exc, "Current credentials could not be verified.")
|
||||
raise # unreachable
|
||||
temporary_token = _temporary_session_token(reauthenticated.cookies)
|
||||
password = req.password
|
||||
|
||||
try:
|
||||
await client.update_managed_user(
|
||||
token, current.id, username=username, email=email, password=password
|
||||
)
|
||||
try:
|
||||
await client.update_managed_user(
|
||||
token, current.id, username=username, email=email, password=password
|
||||
)
|
||||
finally:
|
||||
if password is not None and temporary_token:
|
||||
try:
|
||||
await client.logout(temporary_token)
|
||||
except AuthClientError:
|
||||
pass
|
||||
except AuthClientError as exc:
|
||||
_raise_authguard_error(exc, "Profile could not be updated.")
|
||||
raise # unreachable
|
||||
@@ -651,9 +708,8 @@ def build_auth_router(
|
||||
request: Request,
|
||||
store: ProxyStore = Depends(get_store), # noqa: B008
|
||||
) -> dict[str, bool]:
|
||||
if (
|
||||
not configured_internal_secret
|
||||
or request.headers.get("X-Internal-Auth") != configured_internal_secret
|
||||
if not internal_secret_matches(
|
||||
request.headers.get("X-Internal-Auth"), configured_internal_secret
|
||||
):
|
||||
raise HTTPException(status_code=401, detail="internal service authentication required")
|
||||
await store.set_login_required(req.loginRequired)
|
||||
|
||||
Reference in new issue
Block a user