Skip to content

Commit

Permalink
Merge pull request #242 from johngian/pr-234
Browse files Browse the repository at this point in the history
Refactor username generation to an overridable method
  • Loading branch information
johngian committed May 28, 2018
2 parents ad13928 + 8b780fb commit 67ab0a6
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions mozilla_django_oidc/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,27 @@ def verify_claims(self, claims):

def create_user(self, claims):
"""Return object for a newly created user account."""
# bluntly stolen from django-browserid
# https://github.com/mozilla/django-browserid/blob/master/django_browserid/auth.py

username_algo = import_from_settings('OIDC_USERNAME_ALGO', None)
email = claims.get('email')
if not email:
return None

username = self.get_username(claims)

return self.UserModel.objects.create_user(username, email)

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 = import_from_settings('OIDC_USERNAME_ALGO', None)

if username_algo:
if isinstance(username_algo, six.string_types):
username_algo = import_string(username_algo)
username = username_algo(email)
else:
username = default_username_algo(email)
return username_algo(claims.get('email'))

return self.UserModel.objects.create_user(username, email)
return default_username_algo(claims.get('email'))

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

0 comments on commit 67ab0a6

Please sign in to comment.