Coverage for fastblocks/adapters/auth/_base.py: 0%
26 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-21 04:50 -0700
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-21 04:50 -0700
1import typing as t
2from contextvars import ContextVar
4from acb.config import AdapterBase, Settings
5from acb.depends import depends
6from pydantic import UUID4, EmailStr, SecretStr
7from starlette.authentication import UnauthenticatedUser
8from fastblocks.htmx import HtmxRequest
11class AuthBaseSettings(Settings):
12 token_id: str | None = None
14 @depends.inject
15 def __init__(self, config: t.Any = depends(), **data: t.Any) -> None:
16 super().__init__(**data)
17 self.token_id = self.token_id or getattr(config.app, "token_id", "_fb_")
20class CurrentUser(t.Protocol):
21 def has_role(self, _: str) -> str: ...
23 def set_role(self, _: str) -> str | bool | None: ...
25 @property
26 def identity(self) -> UUID4 | str | int: ...
28 @property
29 def display_name(self) -> str: ...
31 @property
32 def email(self) -> EmailStr | None: ...
34 def is_authenticated(
35 self,
36 request: HtmxRequest | None = None,
37 config: t.Any = None,
38 ) -> bool | int | str: ...
41class AuthProtocol(t.Protocol):
42 _current_user: ContextVar[t.Any]
44 @property
45 def current_user(self) -> t.Any: ...
47 async def authenticate(self, request: HtmxRequest) -> bool: ...
49 async def login(self, request: HtmxRequest) -> bool: ...
51 async def logout(self, request: HtmxRequest) -> bool: ...
54class AuthBase(AdapterBase):
55 _current_user: ContextVar[t.Any] = ContextVar(
56 "current_user",
57 default=UnauthenticatedUser(),
58 )
60 @property
61 def current_user(self) -> t.Any:
62 return self._current_user.get()
64 @property
65 def token_id(self) -> str:
66 return self.config.auth.token_id
68 @staticmethod
69 async def authenticate(request: HtmxRequest) -> bool: ...
71 def __init__(
72 self, secret_key: SecretStr, user_model: t.Any, **kwargs: t.Any
73 ) -> None:
74 super().__init__(**kwargs)
76 async def init(self) -> None: ...
78 async def login(self, request: HtmxRequest) -> bool: ...
80 async def logout(self, request: HtmxRequest) -> bool: ...