Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] uri bug and add custom username claim option #220

Merged
merged 3 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion django_descope/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.contrib.auth import logout
from django.contrib.auth.backends import BaseBackend
from django.http import HttpRequest
from .settings import USERNAME_CLAIM

from . import descope_client
from .models import DescopeUser
Expand Down Expand Up @@ -52,7 +53,13 @@ def authenticate(self, request: Union[HttpRequest, None], **kwargs):
# Contains sensitive information, so only log in DEBUG mode
logger.debug(validated_session)
if validated_session:
username = validated_session[SESSION_TOKEN_NAME]["sub"]
try:
username = validated_session[SESSION_TOKEN_NAME][USERNAME_CLAIM]
except KeyError:
if settings.DEBUG:
logger.debug("KeyError - USERNAME_CLAIM={USERNAME_CLAIM} does not exist on the token")
return None

user, _ = DescopeUser.objects.get_or_create(username=username)
user.sync(validated_session, refresh_token)
request.session[SESSION_COOKIE_NAME] = user.session_token["jwt"]
Expand Down
6 changes: 6 additions & 0 deletions django_descope/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@
# Role names to create in Descope that will map to User attributes
IS_STAFF_ROLE = getattr(settings, "DESCOPE_IS_STAFF_ROLE", "is_staff")
IS_SUPERUSER_ROLE = getattr(settings, "DESCOPE_IS_SUPERUSER_ROLE", "is_superuser")

# Ensure the claim used here is present in the JWT.
# Note: It is crucial to use a claim with a unique value for the username.
# Failure to do so may result in unintended user merges or account takeovers.
# For more information, refer to Descope's [NoAuth](https://www.descope.com/blog/post/noauth) blog post.
USERNAME_CLAIM = getattr(settings, "DESCOPE_USERNAME_CLAIM", "sub")
2 changes: 1 addition & 1 deletion django_descope/templatetags/descope.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def descope_flow(context, flow_id, success_redirect):
id = "descope-" + get_random_string(length=4)
store_jwt_url = reverse("django_descope:store_jwt")
flow = f"""
<descope-wc id="{id}" project-id="{PROJECT_ID}" flow-id="{flow_id}" redirect-url="{success_redirect}"
<descope-wc id="{id}" project-id="{PROJECT_ID}" flow-id="{flow_id}" redirect-url="{context.request.build_absolute_uri()}"
base-url="{os.environ.get('DESCOPE_BASE_URI', '')}"></descope-wc>
<script>
const descopeWcEle = document.getElementById('{id}');
Expand Down
Loading