mirror of
https://github.com/fastapi-users/fastapi-users.git
synced 2025-08-14 18:58:10 +08:00
Improve type hints (#1401)
* Add type parameters to `AuthenticationBackend` * add more type-hints
This commit is contained in:
@ -3,7 +3,7 @@ from typing import Optional
|
||||
|
||||
from beanie import PydanticObjectId
|
||||
from fastapi import Depends, Request
|
||||
from fastapi_users import BaseUserManager, FastAPIUsers
|
||||
from fastapi_users import BaseUserManager, FastAPIUsers, models
|
||||
from fastapi_users.authentication import (
|
||||
AuthenticationBackend,
|
||||
BearerTransport,
|
||||
@ -47,7 +47,7 @@ async def get_user_manager(user_db: BeanieUserDatabase = Depends(get_user_db)):
|
||||
bearer_transport = BearerTransport(tokenUrl="auth/jwt/login")
|
||||
|
||||
|
||||
def get_jwt_strategy() -> JWTStrategy:
|
||||
def get_jwt_strategy() -> JWTStrategy[models.UP, models.ID]:
|
||||
return JWTStrategy(secret=SECRET, lifetime_seconds=3600)
|
||||
|
||||
|
||||
|
@ -3,7 +3,7 @@ import uuid
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import Depends, Request
|
||||
from fastapi_users import BaseUserManager, FastAPIUsers, UUIDIDMixin
|
||||
from fastapi_users import BaseUserManager, FastAPIUsers, UUIDIDMixin, models
|
||||
from fastapi_users.authentication import (
|
||||
AuthenticationBackend,
|
||||
BearerTransport,
|
||||
@ -47,7 +47,7 @@ async def get_user_manager(user_db: SQLAlchemyUserDatabase = Depends(get_user_db
|
||||
bearer_transport = BearerTransport(tokenUrl="auth/jwt/login")
|
||||
|
||||
|
||||
def get_jwt_strategy() -> JWTStrategy:
|
||||
def get_jwt_strategy() -> JWTStrategy[models.UP, models.ID]:
|
||||
return JWTStrategy(secret=SECRET, lifetime_seconds=3600)
|
||||
|
||||
|
||||
|
@ -2,7 +2,7 @@ import uuid
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import Depends, Request
|
||||
from fastapi_users import BaseUserManager, FastAPIUsers, UUIDIDMixin
|
||||
from fastapi_users import BaseUserManager, FastAPIUsers, UUIDIDMixin, models
|
||||
from fastapi_users.authentication import (
|
||||
AuthenticationBackend,
|
||||
BearerTransport,
|
||||
@ -40,7 +40,7 @@ async def get_user_manager(user_db: SQLAlchemyUserDatabase = Depends(get_user_db
|
||||
bearer_transport = BearerTransport(tokenUrl="auth/jwt/login")
|
||||
|
||||
|
||||
def get_jwt_strategy() -> JWTStrategy:
|
||||
def get_jwt_strategy() -> JWTStrategy[models.UP, models.ID]:
|
||||
return JWTStrategy(secret=SECRET, lifetime_seconds=3600)
|
||||
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
import re
|
||||
from inspect import Parameter, Signature
|
||||
from typing import Callable, List, Optional, Sequence, Tuple, cast
|
||||
from typing import Any, Callable, Generic, List, Optional, Sequence, Tuple, cast
|
||||
|
||||
from fastapi import Depends, HTTPException, status
|
||||
from makefun import with_signature
|
||||
@ -31,10 +31,10 @@ class DuplicateBackendNamesError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
EnabledBackendsDependency = DependencyCallable[Sequence[AuthenticationBackend]]
|
||||
EnabledBackendsDependency = DependencyCallable[Sequence[AuthenticationBackend[models.UP, models.ID]]]
|
||||
|
||||
|
||||
class Authenticator:
|
||||
class Authenticator(Generic[models.UP, models.ID]):
|
||||
"""
|
||||
Provides dependency callables to retrieve authenticated user.
|
||||
|
||||
@ -46,11 +46,11 @@ class Authenticator:
|
||||
:param get_user_manager: User manager dependency callable.
|
||||
"""
|
||||
|
||||
backends: Sequence[AuthenticationBackend]
|
||||
backends: Sequence[AuthenticationBackend[models.UP, models.ID]]
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
backends: Sequence[AuthenticationBackend],
|
||||
backends: Sequence[AuthenticationBackend[models.UP, models.ID]],
|
||||
get_user_manager: UserManagerDependency[models.UP, models.ID],
|
||||
):
|
||||
self.backends = backends
|
||||
@ -62,7 +62,7 @@ class Authenticator:
|
||||
active: bool = False,
|
||||
verified: bool = False,
|
||||
superuser: bool = False,
|
||||
get_enabled_backends: Optional[EnabledBackendsDependency] = None,
|
||||
get_enabled_backends: Optional[EnabledBackendsDependency[models.UP, models.ID]] = None,
|
||||
):
|
||||
"""
|
||||
Return a dependency callable to retrieve currently authenticated user and token.
|
||||
@ -88,7 +88,7 @@ class Authenticator:
|
||||
signature = self._get_dependency_signature(get_enabled_backends)
|
||||
|
||||
@with_signature(signature)
|
||||
async def current_user_token_dependency(*args, **kwargs):
|
||||
async def current_user_token_dependency(*args: Any, **kwargs: Any):
|
||||
return await self._authenticate(
|
||||
*args,
|
||||
optional=optional,
|
||||
@ -106,7 +106,7 @@ class Authenticator:
|
||||
active: bool = False,
|
||||
verified: bool = False,
|
||||
superuser: bool = False,
|
||||
get_enabled_backends: Optional[EnabledBackendsDependency] = None,
|
||||
get_enabled_backends: Optional[EnabledBackendsDependency[models.UP, models.ID]] = None,
|
||||
):
|
||||
"""
|
||||
Return a dependency callable to retrieve currently authenticated user.
|
||||
@ -132,7 +132,7 @@ class Authenticator:
|
||||
signature = self._get_dependency_signature(get_enabled_backends)
|
||||
|
||||
@with_signature(signature)
|
||||
async def current_user_dependency(*args, **kwargs):
|
||||
async def current_user_dependency(*args: Any, **kwargs: Any):
|
||||
user, _ = await self._authenticate(
|
||||
*args,
|
||||
optional=optional,
|
||||
@ -157,7 +157,7 @@ class Authenticator:
|
||||
) -> Tuple[Optional[models.UP], Optional[str]]:
|
||||
user: Optional[models.UP] = None
|
||||
token: Optional[str] = None
|
||||
enabled_backends: Sequence[AuthenticationBackend] = kwargs.get(
|
||||
enabled_backends: Sequence[AuthenticationBackend[models.UP, models.ID]] = kwargs.get(
|
||||
"enabled_backends", self.backends
|
||||
)
|
||||
for backend in self.backends:
|
||||
|
@ -35,12 +35,12 @@ class FastAPIUsers(Generic[models.UP, models.ID]):
|
||||
with a specific set of parameters.
|
||||
"""
|
||||
|
||||
authenticator: Authenticator
|
||||
authenticator: Authenticator[models.UP, models.ID]
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
get_user_manager: UserManagerDependency[models.UP, models.ID],
|
||||
auth_backends: Sequence[AuthenticationBackend],
|
||||
auth_backends: Sequence[AuthenticationBackend[models.UP, models.ID]],
|
||||
):
|
||||
self.authenticator = Authenticator(auth_backends, get_user_manager)
|
||||
self.get_user_manager = get_user_manager
|
||||
@ -72,7 +72,7 @@ class FastAPIUsers(Generic[models.UP, models.ID]):
|
||||
return get_reset_password_router(self.get_user_manager)
|
||||
|
||||
def get_auth_router(
|
||||
self, backend: AuthenticationBackend, requires_verification: bool = False
|
||||
self, backend: AuthenticationBackend[models.UP, models.ID], requires_verification: bool = False
|
||||
) -> APIRouter:
|
||||
"""
|
||||
Return an auth router for a given authentication backend.
|
||||
@ -91,7 +91,7 @@ class FastAPIUsers(Generic[models.UP, models.ID]):
|
||||
def get_oauth_router(
|
||||
self,
|
||||
oauth_client: BaseOAuth2,
|
||||
backend: AuthenticationBackend,
|
||||
backend: AuthenticationBackend[models.UP, models.ID],
|
||||
state_secret: SecretType,
|
||||
redirect_url: Optional[str] = None,
|
||||
associate_by_email: bool = False,
|
||||
|
@ -11,9 +11,9 @@ from fastapi_users.router.common import ErrorCode, ErrorModel
|
||||
|
||||
|
||||
def get_auth_router(
|
||||
backend: AuthenticationBackend,
|
||||
backend: AuthenticationBackend[models.UP, models.ID],
|
||||
get_user_manager: UserManagerDependency[models.UP, models.ID],
|
||||
authenticator: Authenticator,
|
||||
authenticator: Authenticator[models.UP, models.ID],
|
||||
requires_verification: bool = False,
|
||||
) -> APIRouter:
|
||||
"""Generate a router with login/logout routes for an authentication backend."""
|
||||
|
@ -29,7 +29,7 @@ def generate_state_token(
|
||||
|
||||
def get_oauth_router(
|
||||
oauth_client: BaseOAuth2,
|
||||
backend: AuthenticationBackend,
|
||||
backend: AuthenticationBackend[models.UP, models.ID],
|
||||
get_user_manager: UserManagerDependency[models.UP, models.ID],
|
||||
state_secret: SecretType,
|
||||
redirect_url: Optional[str] = None,
|
||||
@ -156,7 +156,7 @@ def get_oauth_router(
|
||||
|
||||
def get_oauth_associate_router(
|
||||
oauth_client: BaseOAuth2,
|
||||
authenticator: Authenticator,
|
||||
authenticator: Authenticator[models.UP, models.ID],
|
||||
get_user_manager: UserManagerDependency[models.UP, models.ID],
|
||||
user_schema: Type[schemas.U],
|
||||
state_secret: SecretType,
|
||||
|
@ -12,7 +12,7 @@ def get_users_router(
|
||||
get_user_manager: UserManagerDependency[models.UP, models.ID],
|
||||
user_schema: Type[schemas.U],
|
||||
user_update_schema: Type[schemas.UU],
|
||||
authenticator: Authenticator,
|
||||
authenticator: Authenticator[models.UP, models.ID],
|
||||
requires_verification: bool = False,
|
||||
) -> APIRouter:
|
||||
"""Generate a router with the authentication routes."""
|
||||
|
Reference in New Issue
Block a user