mirror of
https://github.com/d3vyce/fastapi-toolsets.git
synced 2026-08-14 11:57:01 +00:00
wip2
This commit is contained in:
@@ -9,7 +9,7 @@ from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer, SecurityS
|
||||
|
||||
from fastapi_toolsets.exceptions import UnauthorizedError
|
||||
|
||||
from ..base import AuthSource
|
||||
from ..base import AuthSource, _call_validator
|
||||
|
||||
|
||||
class BearerTokenAuth(AuthSource):
|
||||
@@ -20,9 +20,9 @@ class BearerTokenAuth(AuthSource):
|
||||
where ``kwargs`` are the extra keyword arguments provided at instantiation.
|
||||
|
||||
Args:
|
||||
validator: Async callable that receives the credential and any extra
|
||||
keyword arguments, and returns the authenticated identity (e.g. a
|
||||
``User`` model). Should raise
|
||||
validator: Sync or async callable that receives the credential and any
|
||||
extra keyword arguments, and returns the authenticated identity
|
||||
(e.g. a ``User`` model). Should raise
|
||||
:class:`~fastapi_toolsets.exceptions.UnauthorizedError` on failure.
|
||||
prefix: Optional token prefix (e.g. ``"user_"``). If set, only tokens
|
||||
whose value starts with this prefix are matched. The prefix is
|
||||
@@ -63,14 +63,11 @@ class BearerTokenAuth(AuthSource):
|
||||
token = credentials.credentials
|
||||
if _prefix is not None and not token.startswith(_prefix):
|
||||
raise UnauthorizedError()
|
||||
return await _validator(token, **_kwargs)
|
||||
return await _call_validator(_validator, token, **_kwargs)
|
||||
|
||||
self._call_fn = _call
|
||||
self.__signature__ = inspect.signature(_call)
|
||||
|
||||
async def __call__(self, **kwargs: Any) -> Any:
|
||||
return await self._call_fn(**kwargs)
|
||||
|
||||
async def extract(self, request: Any) -> str | None:
|
||||
"""Extract the raw credential from the request without validating.
|
||||
|
||||
@@ -94,7 +91,7 @@ class BearerTokenAuth(AuthSource):
|
||||
Calls ``await validator(credential, **kwargs)`` where ``kwargs`` are
|
||||
the extra keyword arguments provided at instantiation.
|
||||
"""
|
||||
return await self._validator(credential, **self._kwargs)
|
||||
return await _call_validator(self._validator, credential, **self._kwargs)
|
||||
|
||||
def require(self, **kwargs: Any) -> "BearerTokenAuth":
|
||||
"""Return a new instance with additional (or overriding) validator kwargs."""
|
||||
|
||||
Reference in New Issue
Block a user