sync: 鍚屾灞€鍩熺綉鏈€鏂颁唬鐮?(source master @ b69568e4)

This commit is contained in:
gozeros committed 2026-10-21 14:51:31 +08:00
1 parent 4f7f07504a
commit 8bc8b0e082
1651 files changed
+278599 -11416

No files matched your search

@@ -1,12 +1,15 @@
"""JWT token creation and verification."""
from datetime import UTC, datetime, timedelta
from typing import Literal
import jwt
from pydantic import BaseModel
from pydantic import BaseModel, ValidationError
from app.gateway.auth.config import get_auth_config
from app.gateway.auth.errors import TokenError
AuthMethod = Literal["password", "passphrase", "sso"]
class TokenPayload(BaseModel):
@@ -16,15 +19,18 @@ class TokenPayload(BaseModel):
exp: datetime
iat: datetime | None = None
ver: int = 0 # token_version — must match User.token_version
auth_method: AuthMethod | None = None # None is historical/unverified identity-only login.
def create_access_token(user_id: str, expires_delta: timedelta | None = None, token_version: int = 0) -> str:
def create_access_token(user_id: str, expires_delta: timedelta | None = None, token_version: int = 0, *, auth_method: AuthMethod | None = None) -> str:
"""Create a JWT access token.
Args:
user_id: The user's UUID as string
expires_delta: Optional custom expiry, defaults to 7 days
token_version: User's current token_version for invalidation
auth_method: Explicit proof verified by the issuing login path. Never
defaults to a trusted method for old or identity-only callers.
Returns:
Encoded JWT string
@@ -34,6 +40,8 @@ def create_access_token(user_id: str, expires_delta: timedelta | None = None, to
now = datetime.now(UTC)
payload = {"sub": user_id, "exp": now + expiry, "iat": now, "ver": token_version}
if auth_method is not None:
payload["auth_method"] = auth_method
return jwt.encode(payload, config.jwt_secret, algorithm="HS256")
@@ -51,5 +59,5 @@ def decode_token(token: str) -> TokenPayload | TokenError:
return TokenError.EXPIRED
except jwt.InvalidSignatureError:
return TokenError.INVALID_SIGNATURE
except jwt.PyJWTError:
except (jwt.PyJWTError, ValidationError):
return TokenError.MALFORMED