-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: implement OAuth/OIDC sign on with Authlib (#414)
- Loading branch information
Showing
15 changed files
with
112 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
""" | ||
The oauth application: Implements OAuth-based authentication | ||
""" | ||
from django.apps import AppConfig | ||
|
||
|
||
class OAuthAppConfig(AppConfig): | ||
name = "benefits.oauth" | ||
label = "oauth" | ||
verbose_name = "Benefits OAuth" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from django.urls import path | ||
|
||
from . import views | ||
|
||
|
||
app_name = "oauth" | ||
urlpatterns = [ | ||
# /oauth | ||
path("login", views.login, name="login"), | ||
path("authorize", views.authorize, name="authorize"), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from django.shortcuts import redirect | ||
from django.urls import reverse | ||
|
||
from authlib.integrations.django_client import OAuth | ||
|
||
from benefits.core import session | ||
from benefits.settings import OAUTH_CLIENT_NAME | ||
|
||
|
||
if OAUTH_CLIENT_NAME: | ||
_oauth = OAuth() | ||
_oauth.register(OAUTH_CLIENT_NAME) | ||
oauth_client = _oauth.create_client(OAUTH_CLIENT_NAME) | ||
|
||
|
||
ROUTE_AUTH = "oauth:authorize" | ||
ROUTE_START = "eligibility:start" | ||
ROUTE_CONFIRM = "eligibility:confirm" | ||
|
||
|
||
def login(request): | ||
if not oauth_client: | ||
raise Exception("No OAuth client") | ||
|
||
route = reverse(ROUTE_AUTH) | ||
redirect_uri = request.build_absolute_uri(route) | ||
|
||
return oauth_client.authorize_redirect(request, redirect_uri) | ||
|
||
|
||
def authorize(request): | ||
if not oauth_client: | ||
raise Exception("No OAuth client") | ||
|
||
token = oauth_client.authorize_access_token(request) | ||
|
||
if token is None: | ||
return redirect(ROUTE_START) | ||
else: | ||
# we are intentionally not storing anything about the user, including their token | ||
session.update(request, auth=True) | ||
return redirect(ROUTE_CONFIRM) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
Authlib==1.0.1 | ||
cryptography==36.0.2 | ||
Django==3.2.12 | ||
django-csp==3.7 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters