Skip to content

Commit

Permalink
Rework github sso to use simlar kv implementation to GitLab
Browse files Browse the repository at this point in the history
Needed since we've dropped the github_username profile attribute.
  • Loading branch information
Neil Muller committed Oct 12, 2023
1 parent 6727de5 commit 7d09aee
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions wafer/registration/sso.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,22 +119,28 @@ def github_sso(code):
log.warning('Error extracting github email address: %s', e)
raise SSOError('Failed to obtain email address from GitHub')

profile_fields = {
'github_username': login,
}
# TODO: Extend this to also set the github profile url KV
profile_fields = {}
if 'blog' in gh:
profile_fields['blog'] = gh['blog']

try:
user = get_user_model().objects.get(userprofile__github_username=login)
except MultipleObjectsReturned:
log.warning('Multiple accounts have GitHub username %s', login)
raise SSOError('Multiple accounts have GitHub username %s' % login)
except ObjectDoesNotExist:
user = None
group = Group.objects.get_by_natural_key('Registration')
user = None

for kv in KeyValue.objects.filter(
group=group, key='github_sso_account_id', value=login,
userprofile__isnull=False):
if kv.userprofile_set.count() > 1:
message = 'Multiple accounts have the same GitHub username %s'
log.warning(message, login)
raise SSOError(message % login)
user = kv.userprofile_set.first().user
break

user = sso(user=user, desired_username=login, name=name, email=email,
profile_fields=profile_fields)
user.userprofile.kv.get_or_create(group=group, key='github_sso_account_id',
defaults={'value': login})
return user


Expand Down

0 comments on commit 7d09aee

Please sign in to comment.