Skip to content

Commit 7606da6

Browse files
committed
Extend from AuthCredentials and BaseUser superclasses
1 parent cc35c8f commit 7606da6

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

src/fastapi_oauth2/middleware.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
from fastapi.security.utils import get_authorization_scheme_param
1010
from jose.jwt import decode as jwt_decode
1111
from jose.jwt import encode as jwt_encode
12+
from starlette.authentication import AuthCredentials
1213
from starlette.authentication import AuthenticationBackend
14+
from starlette.authentication import BaseUser
1315
from starlette.middleware.authentication import AuthenticationMiddleware
1416
from starlette.requests import Request
1517
from starlette.types import ASGIApp
@@ -22,17 +24,14 @@
2224
from .core import OAuth2Core
2325

2426

25-
class Auth:
27+
class Auth(AuthCredentials):
2628
http: bool
2729
secret: str
2830
expires: int
2931
algorithm: str
3032
scopes: List[str]
3133
clients: Dict[str, OAuth2Core] = {}
3234

33-
def __init__(self, scopes: Optional[List[str]] = None) -> None:
34-
self.scopes = scopes or []
35-
3635
@classmethod
3736
def set_http(cls, http: bool) -> None:
3837
cls.http = http
@@ -67,13 +66,25 @@ def jwt_create(cls, token_data: dict) -> str:
6766
return cls.jwt_encode({**token_data, "exp": expire})
6867

6968

70-
class User(dict):
71-
is_authenticated: bool
72-
69+
class User(BaseUser, dict):
7370
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 = ""
7574
super().__init__(seq or {}, **kwargs)
7675

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+
7788

7889
class OAuth2Backend(AuthenticationBackend):
7990
def __init__(self, config: OAuth2Config) -> None:

0 commit comments

Comments
 (0)