Coverage for fastblocks/adapters/auth/_base.py: 0%
31 statements
« prev ^ index » next coverage.py v7.10.7, created at 2025-10-09 00:47 -0700
« prev ^ index » next coverage.py v7.10.7, created at 2025-10-09 00:47 -0700
1import typing as t
2from contextvars import ContextVar
4from acb.config import AdapterBase, Config, Settings
5from acb.depends import Inject, depends
6from pydantic import UUID4, EmailStr, SecretStr
7from starlette.authentication import UnauthenticatedUser
8from fastblocks.htmx import HtmxRequest
11class AuthBaseSettings(Settings): # type: ignore[misc]
12 token_id: str | None = None
14 @depends.inject # type: ignore[misc]
15 def __init__(self, config: Inject[Config], **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): # type: ignore[misc]
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 t.cast(str, self.config.auth.token_id)
68 @staticmethod
69 async def authenticate(request: HtmxRequest) -> bool:
70 raise NotImplementedError
72 def __init__(
73 self, secret_key: SecretStr, user_model: t.Any, **kwargs: t.Any
74 ) -> None:
75 super().__init__(**kwargs)
77 async def init(self) -> None:
78 raise NotImplementedError
80 async def login(self, request: HtmxRequest) -> bool:
81 raise NotImplementedError
83 async def logout(self, request: HtmxRequest) -> bool:
84 raise NotImplementedError