Skip to content

Commit

Permalink
Pick github primary email first. Fixes #413
Browse files Browse the repository at this point in the history
  • Loading branch information
omab committed Oct 23, 2014
1 parent 01dbda3 commit 6550eae
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions social/backends/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,21 @@ def user_data(self, access_token, *args, **kwargs):
data = self._user_data(access_token)
if not data.get('email'):
try:
email = self._user_data(access_token, '/emails')[0]
except (HTTPError, IndexError, ValueError, TypeError):
email = ''

if isinstance(email, dict):
email = email.get('email', '')
data['email'] = email
emails = self._user_data(access_token, '/emails')
except (HTTPError, ValueError, TypeError):
emails = []

if emails:
email = emails[0]
primary_emails = [e for e in emails
if not isinstance(e, dict) or
e.get('primary')]

if primary_emails:
email = primary_emails[0]
if isinstance(email, dict):
email = email.get('email', '')
data['email'] = email
return data

def _user_data(self, access_token, path=None):
Expand Down Expand Up @@ -85,7 +93,6 @@ def member_url(self, user_data):
username=user_data.get('login'))



class GithubTeamOAuth2(GithubMemberOAuth2):
"""Github OAuth2 authentication backend for teams"""
name = 'github-team'
Expand Down

0 comments on commit 6550eae

Please sign in to comment.