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

1import typing as t 

2from contextvars import ContextVar 

3 

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 

9 

10 

11class AuthBaseSettings(Settings): 

12 token_id: str | None = None 

13 

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_") 

18 

19 

20class CurrentUser(t.Protocol): 

21 def has_role(self, _: str) -> str: ... 

22 

23 def set_role(self, _: str) -> str | bool | None: ... 

24 

25 @property 

26 def identity(self) -> UUID4 | str | int: ... 

27 

28 @property 

29 def display_name(self) -> str: ... 

30 

31 @property 

32 def email(self) -> EmailStr | None: ... 

33 

34 def is_authenticated( 

35 self, 

36 request: HtmxRequest | None = None, 

37 config: t.Any = None, 

38 ) -> bool | int | str: ... 

39 

40 

41class AuthProtocol(t.Protocol): 

42 _current_user: ContextVar[t.Any] 

43 

44 @property 

45 def current_user(self) -> t.Any: ... 

46 

47 async def authenticate(self, request: HtmxRequest) -> bool: ... 

48 

49 async def login(self, request: HtmxRequest) -> bool: ... 

50 

51 async def logout(self, request: HtmxRequest) -> bool: ... 

52 

53 

54class AuthBase(AdapterBase): 

55 _current_user: ContextVar[t.Any] = ContextVar( 

56 "current_user", 

57 default=UnauthenticatedUser(), 

58 ) 

59 

60 @property 

61 def current_user(self) -> t.Any: 

62 return self._current_user.get() 

63 

64 @property 

65 def token_id(self) -> str: 

66 return self.config.auth.token_id 

67 

68 @staticmethod 

69 async def authenticate(request: HtmxRequest) -> bool: ... 

70 

71 def __init__( 

72 self, secret_key: SecretStr, user_model: t.Any, **kwargs: t.Any 

73 ) -> None: 

74 super().__init__(**kwargs) 

75 

76 async def init(self) -> None: ... 

77 

78 async def login(self, request: HtmxRequest) -> bool: ... 

79 

80 async def logout(self, request: HtmxRequest) -> bool: ...