From: Matías Aguirre Date: Fri, 14 Jan 2011 06:25:46 +0000 (-0200) Subject: Merged krvss work. Small improvements over livejournal support X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=55ec3cd1441edad9fc9efa1c719acda01f30e265;p=django-social-auth.git Merged krvss work. Small improvements over livejournal support --- 55ec3cd1441edad9fc9efa1c719acda01f30e265 diff --cc README.rst index 8256f0c,cc57ad7..44b25f2 --- a/README.rst +++ b/README.rst @@@ -27,8 -27,7 +27,9 @@@ credentials, some features are at the moment: * `Google OpenID`_ + * `Google OAuth`_ * `Yahoo OpenID`_ ++ * `LiveJournal OpenID`_ * OpenId_ like myOpenID_ * `Twitter OAuth`_ * `Facebook OAuth`_ @@@ -97,9 -95,8 +98,10 @@@ Configuratio 'social_auth.backends.TwitterBackend', 'social_auth.backends.FacebookBackend', 'social_auth.backends.OrkutBackend', + 'social_auth.backends.GoogleOAuthBackend', 'social_auth.backends.GoogleBackend', 'social_auth.backends.YahooBackend', ++ 'social_auth.backends.LiveJournalBackend', 'social_auth.backends.OpenIDBackend', 'django.contrib.auth.backends.ModelBackend', ) @@@ -202,6 -196,6 +204,13 @@@ for example, to store user gender, loca user.gender = response.get('gender') return True ++New data updating is made automatically but could be disabled and left only to ++signal handler if this setting value:: ++ ++ SOCIAL_AUTH_CHANGE_SIGNAL_ONLY = False ++ ++is set to True. ++ ------ OpenId @@@ -389,3 -336,3 +398,4 @@@ Base work is copyrighted by .. _caioariede: https://github.com/caioariede .. _krvss: https://github.com/krvss .. _jezdez: https://github.com/jezdez ++.. _LiveJournal OpenID: http://www.livejournal.com/support/faqbrowse.bml?faqid=283 diff --cc social_auth/auth.py index f533b77,132e1d1..c7b3ebe --- a/social_auth/auth.py +++ b/social_auth/auth.py @@@ -16,10 -15,10 +16,11 @@@ from django.contrib.auth import authent from .store import DjangoOpenIDStore from .backends import TwitterBackend, OrkutBackend, FacebookBackend, \ - OpenIDBackend, GoogleBackend, YahooBackend, LiveJournalBackend + OpenIDBackend, GoogleBackend, YahooBackend, \ - GoogleOAuthBackend ++ GoogleOAuthBackend, LiveJournalBackend from .conf import AX_ATTRS, SREG_ATTR, OPENID_ID_FIELD, SESSION_NAME, \ OPENID_GOOGLE_URL, OPENID_YAHOO_URL, TWITTER_SERVER, \ + OPENID_LIVEJOURNAL_URL, OPENID_LIVEJOURNAL_USER_FIELD, \ TWITTER_REQUEST_TOKEN_URL, TWITTER_ACCESS_TOKEN_URL, \ TWITTER_AUTHORIZATION_URL, TWITTER_CHECK_AUTH, \ FACEBOOK_CHECK_AUTH, FACEBOOK_AUTHORIZATION_URL, \ @@@ -167,7 -166,19 +168,24 @@@ class YahooAuth(OpenIdAuth) """Return Yahoo OpenID service url""" return OPENID_YAHOO_URL + + class LiveJournalAuth(OpenIdAuth): + """LiveJournal OpenID authentication""" + AUTH_BACKEND = LiveJournalBackend ++ + def uses_redirect(self): + """LiveJournal uses redirect""" + return True - ++ + def openid_url(self): - """Returns LJ authentication URL""" - if self.request.method != 'POST' or not self.request.POST.get(OPENID_LIVEJOURNAL_USER_FIELD): ++ """Returns LiveJournal authentication URL""" ++ if self.request.method != 'POST' or \ ++ not self.request.POST.get(OPENID_LIVEJOURNAL_USER_FIELD): + raise ValueError, 'Missing LiveJournal user identifier' - return OPENID_LIVEJOURNAL_URL % self.request.POST[OPENID_LIVEJOURNAL_USER_FIELD] ++ return OPENID_LIVEJOURNAL_URL % \ ++ self.request.POST[OPENID_LIVEJOURNAL_USER_FIELD] ++ + class BaseOAuth(BaseAuth): """OAuth base class""" def __init__(self, request, redirect): @@@ -444,8 -397,8 +462,9 @@@ BACKENDS = 'twitter': TwitterAuth, 'facebook': FacebookAuth, 'google': GoogleAuth, + 'google-oauth': GoogleOAuth, 'yahoo': YahooAuth, + 'livejournal': LiveJournalAuth, 'orkut': OrkutAuth, 'openid': OpenIdAuth, } diff --cc social_auth/backends.py index 591030a,17da956..867e72d --- a/social_auth/backends.py +++ b/social_auth/backends.py @@@ -1,6 -1,6 +1,7 @@@ """ Authentication backeds for django.contrib.auth AUTHENTICATION_BACKENDS setting """ ++import urlparse from os import urandom from openid.extensions import ax, sreg @@@ -108,23 -108,31 +109,33 @@@ class SocialAuthBackend(ModelBackend) """Update user details with (maybe) new data. Username is not changed if associating a new credential.""" changed = False - for name, value in details.iteritems(): - # not update username if user already exists - if not new_user and name == USERNAME: - continue - if value and value != getattr(user, name, value): - setattr(user, name, value) - changed = True - + ++ # check if values update should be left to signals handlers only + if not getattr(settings, 'SOCIAL_AUTH_CHANGE_SIGNAL_ONLY', False): + for name, value in details.iteritems(): + # not update username if user already exists + if not new_user and name == USERNAME: + continue + if value and value != getattr(user, name, value): + setattr(user, name, value) + changed = True + # Fire a pre-update signal sending current backend instance, # user instance (created or retrieved from database), service # response and processed details, signal handlers must return # True or False to signal that something has changed - updated = filter(None, pre_update.send(sender=self, user=user, + updated = filter(None, pre_update.send(sender=self.__class__, + user=user, response=response, details=details)) - if changed or len(updated) > 0: + # Looking for at least one update + has_update = False + for result in updated: + if result[1]: + has_update = True + break + + if changed or has_update: user.save() def get_user_id(self, details, response): @@@ -268,3 -258,7 +279,16 @@@ class GoogleBackend(OpenIDBackend) class YahooBackend(OpenIDBackend): """Yahoo OpenID authentication backend""" name = 'yahoo' + ++ + class LiveJournalBackend(OpenIDBackend): - """LJ OpenID authentication backend""" - name = 'livejournal' ++ """LiveJournal OpenID authentication backend""" ++ name = 'livejournal' ++ ++ def get_user_details(self, response): ++ """Generate username from identity url""" ++ values = super(LiveJournalBackend, self).get_user_details(response) ++ if not values.get(USERNAME): ++ values[USERNAME] = urlparse.urlsplit(response.identity_url)\ ++ .netloc.split('.', 1)[0] ++ return values diff --cc social_auth/conf.py index 7a75d1a,2fa337b..de6f42a --- a/social_auth/conf.py +++ b/social_auth/conf.py @@@ -48,5 -43,7 +48,7 @@@ AX_ATTRS = AX_SCHEMA_ATTRS + OLD_AX_ATT SREG_ATTR = ['email', 'fullname', 'nickname'] OPENID_ID_FIELD = 'openid_identifier' SESSION_NAME = 'openid' - OPENID_GOOGLE_URL = 'https://www.google.com/accounts/o8/id' + OPENID_GOOGLE_URL = 'https://www.google.com/accounts/o8/id' OPENID_YAHOO_URL = 'http://yahoo.com' + OPENID_LIVEJOURNAL_URL = 'http://%s.livejournal.com' -OPENID_LIVEJOURNAL_USER_FIELD = 'openid_lj_user' ++OPENID_LIVEJOURNAL_USER_FIELD = 'openid_lj_user'