Spaces:
Sleeping
Sleeping
Download adsp/app/api/routes/auth.py from thanhnv2323/humanize-text-python: direct link, hf CLI and curl.
- Browser
- Download file 2.34 kB
-
https://huggingface.co/spaces/thanhnv2323/humanize-text-python/resolve/main/adsp/app/api/routes/auth.py
- Command line
-
hf download hf://spaces/thanhnv2323/humanize-text-python/adsp/app/api/routes/auth.py
-
curl -L -o auth.py https://huggingface.co/spaces/thanhnv2323/humanize-text-python/resolve/main/adsp/app/api/routes/auth.py
2.34 kB
| """Authentication API routes.""" | |
| from __future__ import annotations | |
| from typing import Any, Callable | |
| from pydantic import BaseModel, ConfigDict, Field | |
| class AuthRegisterRequest(BaseModel): | |
| model_config = ConfigDict(json_schema_extra={"example": {"user": "demo-user", "token": "demo-token-123"}}) | |
| user: str = Field(description="Unique user identifier used for API calls.", examples=["demo-user"]) | |
| token: str = Field(description="Token to register for the user.", examples=["demo-token-123"]) | |
| class AuthValidateRequest(BaseModel): | |
| model_config = ConfigDict(json_schema_extra={"example": {"user": "demo-user", "token": "demo-token-123"}}) | |
| user: str = Field(description="User identifier to validate.", examples=["demo-user"]) | |
| token: str = Field(description="Token to validate for the user.", examples=["demo-token-123"]) | |
| class AuthRegisterResponse(BaseModel): | |
| model_config = ConfigDict(json_schema_extra={"example": {"status": "ok"}}) | |
| status: str = Field(description="Registration status.", examples=["ok"]) | |
| class AuthValidateResponse(BaseModel): | |
| model_config = ConfigDict(json_schema_extra={"example": {"authorized": True}}) | |
| authorized: bool = Field(description="Whether the supplied user/token pair is authorized.") | |
| def register_auth_routes( | |
| app: Any, | |
| *, | |
| get_services: Callable[..., Any], | |
| ) -> None: | |
| from fastapi import Depends | |
| def register_auth(payload: AuthRegisterRequest, services: Any = Depends(get_services)) -> AuthRegisterResponse: | |
| services.auth.register(payload.user, payload.token) | |
| return AuthRegisterResponse(status="ok") | |
| def validate_auth( | |
| payload: AuthValidateRequest, | |
| services: Any = Depends(get_services), | |
| ) -> AuthValidateResponse: | |
| return AuthValidateResponse(authorized=services.auth.is_authorized(payload.user, payload.token)) | |