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

Custom username algo plus claims #492 #493

Closed
Changes from 1 commit
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
Next Next commit
added claims support to custom username algo
  • Loading branch information
EduardRosert committed Jun 1, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit c01143945f6a2a49996c2d66202f6e0a38c92f3d
15 changes: 12 additions & 3 deletions mozilla_django_oidc/auth.py
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
import json
import logging

import inspect
import requests
from django.contrib.auth import get_user_model
from django.contrib.auth.backends import ModelBackend
@@ -20,10 +21,11 @@
LOGGER = logging.getLogger(__name__)


def default_username_algo(email):
def default_username_algo(email, claims):
"""Generate username for the Django user.

:arg str/unicode email: the email address to use to generate a username
:arg dic claims: the claims from your OIDC provider, currently unused

:returns: str/unicode

@@ -100,14 +102,21 @@ def get_username(self, claims):
"""Generate username based on claims."""
# bluntly stolen from django-browserid
# https://github.com/mozilla/django-browserid/blob/master/django_browserid/auth.py

username_algo = self.get_settings("OIDC_USERNAME_ALGO", None)

if username_algo:
if isinstance(username_algo, str):
username_algo = import_string(username_algo)
return username_algo(claims.get("email"))
if len(inspect.getfullargspec(username_algo).args) == 1:
# this is for backwards compatibility only
return username_algo(claims.get("email"))
else:
# also pass the claims to the custom user name algo
return username_algo(claims.get("email"), claims)


return default_username_algo(claims.get("email"))
return default_username_algo(claims.get("email"), claims)

def update_user(self, user, claims):
"""Update existing user with new claims, if necessary save, and return user"""