Skip to content

Commit

Permalink
OIDC & JWT (#174)
Browse files Browse the repository at this point in the history
* add oidc capabilities

* lint

* fix: temp key for build step

* fix(debug): prevent http failure in local env

* feat(oidc): jwt with properties

* lint

* chore(black)
  • Loading branch information
esinx committed Sep 1, 2024
1 parent ff1796e commit cd79316
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions backend/Platform/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
# OAuth2 Settings

OAUTH2_PROVIDER = {
"OAUTH2_VALIDATOR_CLASS": "accounts.oauth2_validator.LabsOAuth2Validator",
"SCOPES": {
"openid": "OpenID Connect scope",
"read": "Read scope",
Expand Down
2 changes: 1 addition & 1 deletion backend/Platform/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
urlpatterns = [
path("admin/", admin.site.urls),
path("announcements/", include("announcements.urls", namespace="announcements")),
path("accounts/", include("accounts.urls")),
path("accounts/", include("accounts.urls", namespace="oauth2_provider")),
path("options/", include("options.urls", namespace="options")),
path("identity/", include("identity.urls", namespace="identity")),
path("s/", include("shortener.urls", namespace="shortener")),
Expand Down
2 changes: 2 additions & 0 deletions backend/accounts/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class ShibbolethRemoteUserBackend(RemoteUserBackend):
"""

def get_email(self, pennid):
if settings.DEBUG:
return None
"""
Use Penn Directory API with OAuth2 to get the email of a user given their Penn ID.
This is necessary to ensure that we have the correct domain (@seas vs. @wharton, etc.)
Expand Down
4 changes: 4 additions & 0 deletions backend/accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ class User(AbstractUser):

VERIFICATION_EXPIRATION_MINUTES = 10

@property
def id(self):
return self.username

def get_preferred_name(self):
if self.preferred_name != "":
return self.preferred_name
Expand Down
23 changes: 23 additions & 0 deletions backend/accounts/oauth2_validator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from oauth2_provider.oauth2_validators import OAuth2Validator


class LabsOAuth2Validator(OAuth2Validator):
oidc_claim_scope = OAuth2Validator.oidc_claim_scope
oidc_claim_scope.update(
{
"name": "read",
"email": "read",
"pennid": "read",
"is_staff": "read",
"is_active": "read",
}
)

def get_additional_claims(self, request):
return {
"name": request.user.preferred_name or request.user.get_full_name(),
"email": request.user.email,
"pennid": request.user.pennid,
"is_staff": request.user.is_staff,
"is_active": request.user.is_active,
}
13 changes: 12 additions & 1 deletion backend/accounts/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
from django.conf import settings
from django.conf.urls.static import static
from django.urls import path
from oauth2_provider.views import AuthorizationView, TokenView
from oauth2_provider.views import (
AuthorizationView,
ConnectDiscoveryInfoView,
JwksInfoView,
TokenView,
)
from rest_framework import routers

from accounts.views import (
Expand Down Expand Up @@ -47,6 +52,12 @@
path("privacy/", PrivacySettingView.as_view(), name="privacy"),
path("privacy/<int:pk>/", PrivacySettingView.as_view(), name="privacy"),
path("user/<str:username>", FindUserView.as_view(), name="user"),
path(
".well-known/openid-configuration",
ConnectDiscoveryInfoView.as_view(),
name="oidc-connect-discovery-info",
),
path(".well-known/jwks.json", JwksInfoView.as_view(), name="oidc-jwks-info"),
]

urlpatterns += router.urls
Expand Down

0 comments on commit cd79316

Please sign in to comment.