|
9 | 9 | from fastapi.security.utils import get_authorization_scheme_param
|
10 | 10 | from jose.jwt import decode as jwt_decode
|
11 | 11 | from jose.jwt import encode as jwt_encode
|
| 12 | +from starlette.authentication import AuthCredentials |
12 | 13 | from starlette.authentication import AuthenticationBackend
|
| 14 | +from starlette.authentication import BaseUser |
13 | 15 | from starlette.middleware.authentication import AuthenticationMiddleware
|
14 | 16 | from starlette.requests import Request
|
15 | 17 | from starlette.types import ASGIApp
|
|
22 | 24 | from .core import OAuth2Core
|
23 | 25 |
|
24 | 26 |
|
25 |
| -class Auth: |
| 27 | +class Auth(AuthCredentials): |
26 | 28 | http: bool
|
27 | 29 | secret: str
|
28 | 30 | expires: int
|
29 | 31 | algorithm: str
|
30 | 32 | scopes: List[str]
|
31 | 33 | clients: Dict[str, OAuth2Core] = {}
|
32 | 34 |
|
33 |
| - def __init__(self, scopes: Optional[List[str]] = None) -> None: |
34 |
| - self.scopes = scopes or [] |
35 |
| - |
36 | 35 | @classmethod
|
37 | 36 | def set_http(cls, http: bool) -> None:
|
38 | 37 | cls.http = http
|
@@ -67,13 +66,25 @@ def jwt_create(cls, token_data: dict) -> str:
|
67 | 66 | return cls.jwt_encode({**token_data, "exp": expire})
|
68 | 67 |
|
69 | 68 |
|
70 |
| -class User(dict): |
71 |
| - is_authenticated: bool |
72 |
| - |
| 69 | +class User(BaseUser, dict): |
73 | 70 | def __init__(self, seq: Optional[dict] = None, **kwargs) -> None:
|
74 |
| - self.is_authenticated = seq is not None |
| 71 | + self._is_authenticated = seq is not None |
| 72 | + self._display_name = "" |
| 73 | + self._identity = "" |
75 | 74 | super().__init__(seq or {}, **kwargs)
|
76 | 75 |
|
| 76 | + @property |
| 77 | + def is_authenticated(self) -> bool: |
| 78 | + return self._is_authenticated |
| 79 | + |
| 80 | + @property |
| 81 | + def display_name(self) -> str: |
| 82 | + return self._display_name |
| 83 | + |
| 84 | + @property |
| 85 | + def identity(self) -> str: |
| 86 | + return self._identity |
| 87 | + |
77 | 88 |
|
78 | 89 | class OAuth2Backend(AuthenticationBackend):
|
79 | 90 | def __init__(self, config: OAuth2Config) -> None:
|
|
0 commit comments