Skip to content

Instantly share code, notes, and snippets.

@olivx
Forked from cansadadeserfeliz/pipeline.py
Last active January 25, 2019 17:14
Show Gist options
  • Save olivx/e251b4658e034fe02b90730db204c689 to your computer and use it in GitHub Desktop.
Save olivx/e251b4658e034fe02b90730db204c689 to your computer and use it in GitHub Desktop.
Django: python-social-auth configuration and pipeline
def update_user_social_data(strategy, *args, **kwargs):
"""Set the name and avatar for a user only if is new.
"""
print 'update_user_social_data ::', strategy
if not kwargs['is_new']:
return
full_name = ''
backend = kwargs['backend']
user = kwargs['user']
if (
isinstance(backend, GoogleOAuth2)
or isinstance(backend, FacebookOAuth2)
):
full_name = kwargs['response'].get('name')
elif (
isinstance(backend, LinkedinOAuth)
or isinstance(backend, TwitterOAuth)
):
if kwargs.get('details'):
full_name = kwargs['details'].get('fullname')
user.full_name = full_name
if isinstance(backend, GoogleOAuth2):
if response.get('image') and response['image'].get('url'):
url = response['image'].get('url')
ext = url.split('.')[-1]
user.avatar.save(
'{0}.{1}'.format('avatar', ext),
ContentFile(urllib2.urlopen(url).read()),
save=False
)
elif isinstance(backend, FacebookOAuth2):
fbuid = kwargs['response']['id']
image_name = 'fb_avatar_%s.jpg' % fbuid
image_url = 'http://graph.facebook.com/%s/picture?type=large' % fbuid
image_stream = urlopen(image_url)
user.avatar.save(
image_name,
ContentFile(image_stream.read()),
)
elif isinstance(backend, TwitterOAuth):
if kwargs['response'].get('profile_image_url'):
image_name = 'tw_avatar_%s.jpg' % full_name
image_url = kwargs['response'].get['profile_image_url']
image_stream = urlopen(image_url)
user.avatar.save(
image_name,
ContentFile(image_stream.read()),
)
elif isinstance(backend, LinkedinOAuth):
if kwargs['response'].get('pictureUrl'):
image_name = 'linked_avatar_%s.jpg' % full_name
image_url = kwargs['response'].get['pictureUrl']
image_stream = urlopen(image_url)
user.avatar.save(
image_name,
ContentFile(image_stream.read()),
)
user.save()
# python-social-auth
AUTHENTICATION_BACKENDS = (
'social.backends.facebook.FacebookOAuth2',
'social.backends.twitter.TwitterOAuth',
'social.backends.linkedin.LinkedinOAuth',
'social.backends.google.GoogleOAuth2',
'django.contrib.auth.backends.ModelBackend',
)
SOCIAL_AUTH_PIPELINE = (
'social.pipeline.social_auth.social_details',
'social.pipeline.social_auth.social_uid',
'social.pipeline.social_auth.auth_allowed',
'social.pipeline.social_auth.social_user',
'social.pipeline.mail.mail_validation',
'social.pipeline.social_auth.associate_by_email',
'social.pipeline.user.get_username',
'social.pipeline.user.create_user',
'social.pipeline.social_auth.associate_user',
'social.pipeline.social_auth.load_extra_data',
'social.pipeline.user.user_details',
'myapp.pipeline.update_user_social_data',
)
SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/'
SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']
SOCIAL_AUTH_STRATEGY = 'social.strategies.django_strategy.DjangoStrategy'
SOCIAL_AUTH_STORAGE = 'social.apps.django_app.default.models.DjangoStorage'
SOCIAL_AUTH_FACEBOOK_KEY = ''
SOCIAL_AUTH_FACEBOOK_SECRET = ''
SOCIAL_AUTH_TWITTER_KEY = ''
SOCIAL_AUTH_TWITTER_SECRET = ''
SOCIAL_AUTH_LINKEDIN_KEY = ''
SOCIAL_AUTH_LINKEDIN_SECRET = ''
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = ''
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = ''
SOCIAL_AUTH_LINKEDIN_SCOPE = ['r_basicprofile', 'r_emailaddress']
# These fields be requested from linkedin.
SOCIAL_AUTH_LINKEDIN_FIELD_SELECTORS = [
'email-address',
'picture-url',
]
SOCIAL_AUTH_LINKEDIN_EXTRA_DATA = [
('id', 'id'),
('firstName', 'first_name'),
('lastName', 'last_name'),
('emailAddress', 'email_address'),
('public-profile-url', 'public_profile_url'),
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment