Skip to content

Commit

Permalink
Add Oauth openid configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
thenav56 committed Dec 27, 2024
1 parent a81bae9 commit c0f81af
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
16 changes: 16 additions & 0 deletions main/oauth2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.contrib.auth.models import User
from oauth2_provider.oauth2_validators import OAuth2Validator


class CustomOAuth2Validator(OAuth2Validator):

def get_additional_claims(self, request):
user: User = request.user
user.get_full_name()
return {
"sub": user.email,
"email": user.email,
"name": user.get_full_name(),
"first_name": user.first_name,
"last_name": user.last_name,
}
26 changes: 26 additions & 0 deletions main/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@
JWT_PRIVATE_KEY=(str, None),
JWT_PUBLIC_KEY=(str, None),
JWT_EXPIRE_TIMESTAMP_DAYS=(int, 365),
# OIDC
OIDC_RSA_PRIVATE_KEY_BASE64_ENCODED=(str, None),
OIDC_RSA_PRIVATE_KEY=(str, None),
OIDC_RSA_PUBLIC_KEY_BASE64_ENCODED=(str, None),
OIDC_RSA_PUBLIC_KEY=(str, None),
# Country page
NS_CONTACT_USERNAME=(str, None),
NS_CONTACT_PASSWORD=(str, None),
Expand Down Expand Up @@ -204,6 +209,7 @@
# GO Apps
*GO_APPS,
# Utils Apps
"oauth2_provider",
"tinymce",
"admin_auto_filters",
"haystack",
Expand Down Expand Up @@ -706,6 +712,26 @@ def decode_base64(env_key, fallback_env_key):
AZURE_OPENAI_KEY = env("AZURE_OPENAI_KEY")
AZURE_OPENAI_DEPLOYMENT_NAME = env("AZURE_OPENAI_DEPLOYMENT_NAME")

# django-oauth-toolkit configs
OIDC_RSA_PRIVATE_KEY = decode_base64("OIDC_RSA_PRIVATE_KEY_BASE64_ENCODED", "OIDC_RSA_PRIVATE_KEY")
OIDC_RSA_PUBLIC_KEY = decode_base64("OIDC_RSA_PUBLIC_KEY_BASE64_ENCODED", "OIDC_RSA_PUBLIC_KEY")

OAUTH2_PROVIDER = {
"ACCESS_TOKEN_EXPIRE_SECONDS": 300, # NOTE: keep this high if this is used as OAuth instead of OIDC
"OIDC_ENABLED": True,
"OIDC_RSA_PRIVATE_KEY": OIDC_RSA_PRIVATE_KEY,
"PKCE_REQUIRED": True,
"SCOPES": {
"openid": "OpenID Connect scope",
"profile": "Profile scope",
"email": "Email scope",
},
"OAUTH2_VALIDATOR_CLASS": "main.oauth2.CustomOAuth2Validator",
"ALLOWED_REDIRECT_URI_SCHEMES": ["https"],
}
if GO_ENVIRONMENT == "development":
OAUTH2_PROVIDER["ALLOWED_REDIRECT_URI_SCHEMES"].append("http")

# Need to load this to overwrite modeltranslation module
import main.translation # noqa: F401 E402

Expand Down
2 changes: 2 additions & 0 deletions main/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
SpectacularRedocView,
SpectacularSwaggerView,
)
from oauth2_provider import urls as oauth2_urls

# DRF routes
from rest_framework import routers
Expand Down Expand Up @@ -169,6 +170,7 @@
admin.site.site_title = "IFRC Go admin"

urlpatterns = [
path("o/", include(oauth2_urls, namespace="oauth2_provider")),
# url(r"^api/v1/es_search/", EsPageSearch.as_view()),
url(r"^api/v1/search/", HayStackSearch.as_view()),
url(r"^api/v1/es_health/", EsPageHealth.as_view()),
Expand Down

0 comments on commit c0f81af

Please sign in to comment.