Skip to content

Commit ca20dac

Browse files
committed
Implement Auth and User response types
1 parent 0f94a13 commit ca20dac

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

src/fastapi_oauth2/middleware.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
from typing import List
12
from typing import Optional
23
from typing import Tuple
34
from typing import Union
45

56
from fastapi.security.utils import get_authorization_scheme_param
6-
from starlette.authentication import AuthCredentials
77
from starlette.authentication import AuthenticationBackend
88
from starlette.middleware.authentication import AuthenticationMiddleware
99
from starlette.requests import Request
@@ -16,17 +16,32 @@
1616
from .utils import jwt_decode
1717

1818

19+
class Auth:
20+
scopes: List[str]
21+
22+
def __init__(self, scopes: Optional[List[str]] = None) -> None:
23+
self.scopes = scopes or []
24+
25+
26+
class User(dict):
27+
is_authenticated: bool
28+
29+
def __init__(self, seq: Optional[dict] = None, **kwargs) -> None:
30+
self.is_authenticated = seq is not None
31+
super().__init__(seq or {}, **kwargs)
32+
33+
1934
class OAuth2Backend(AuthenticationBackend):
20-
async def authenticate(self, request: Request) -> Optional[Tuple["AuthCredentials", Optional[dict]]]:
35+
async def authenticate(self, request: Request) -> Optional[Tuple["Auth", "User"]]:
2136
authorization = request.cookies.get("Authorization")
2237
scheme, param = get_authorization_scheme_param(authorization)
2338

2439
if not scheme or not param:
25-
return AuthCredentials(), None
40+
return Auth(), User()
2641

27-
access_token = jwt_decode(param)
28-
scope = access_token.pop("scope")
29-
return AuthCredentials(scope), access_token
42+
user = jwt_decode(param)
43+
scopes = user.pop("scope")
44+
return Auth(scopes), User(user)
3045

3146

3247
class OAuth2Middleware:

templates/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<body style="margin: 0; display: flex; flex-direction: column; background: #1e1e20; color: #dfdfd6; font-family: sans-serif;">
1111
<header style="display: flex; background: #161618; height: 70px; width: 100%;">
1212
<div style="display: flex; align-items: center; margin: auto 50px auto auto;">
13-
{% if request.user %}
13+
{% if request.user.is_authenticated %}
1414
<a href="/oauth2/logout" style="text-decoration: none; color: #dfdfd6; margin-right: 20px;">Sign out</a>
1515
<img style="height: 50px; width: 50px;" src="{{ request.user.avatar_url }}" alt="Pic">
1616
{% else %}
@@ -26,7 +26,7 @@
2626
</header>
2727
<section
2828
style="display: flex; flex-direction: column; align-items: center; justify-content: center; height: calc(100vh - 70px);">
29-
{% if request.user %}
29+
{% if request.user.is_authenticated %}
3030
<h1>Hi, {{ request.user.name }}</h1>
3131
<p>This is what your JWT contains currently</p>
3232
<pre>{{ json.dumps(request.user, indent=4) }}</pre>

0 commit comments

Comments
 (0)