]> git.parisson.com Git - django-social-auth.git/commitdiff
Merged krvss work. Small improvements over livejournal support
authorMatías Aguirre <matiasaguirre@gmail.com>
Fri, 14 Jan 2011 06:25:46 +0000 (04:25 -0200)
committerMatías Aguirre <matiasaguirre@gmail.com>
Fri, 14 Jan 2011 06:26:15 +0000 (04:26 -0200)
1  2 
README.rst
example/settings.py
example/templates/home.html
social_auth/auth.py
social_auth/backends.py
social_auth/conf.py

diff --cc README.rst
index 8256f0c7cab523ab19d0f894784be17d89f3e6ac,cc57ad7490d0a5a228324a4bacaed4ff3e22a77b..44b25f288a71a5c6383b53a1c8c278f7db1e0e9d
@@@ -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
Simple merge
Simple merge
index f533b77d5355d7d21594587b23711f9e93695fc2,132e1d11ddcdfe4f0e906160b3a5d9bc4ef9f637..c7b3ebe7b152e900ddc98171fa2c53a23480332f
@@@ -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
 -        """Returns LJ authentication URL"""
 -        if self.request.method != 'POST' or not self.request.POST.get(OPENID_LIVEJOURNAL_USER_FIELD):
++
+     def openid_url(self):
 -        return OPENID_LIVEJOURNAL_URL % self.request.POST[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]
++
  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,
  }
index 591030ac4f1ec6ae26b037b7f15686c275d54e5c,17da9566cfbddd5b64d46ed5ea42a99bf94acc19..867e72dcf86c3822a7e74546563ef295f8544a53
@@@ -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'
 -    """LJ OpenID authentication backend"""
 -    name = 'livejournal'
++
+ class LiveJournalBackend(OpenIDBackend):
++    """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
index 7a75d1a382a32af85e9c8ae2e4f1f58d0e478ebb,2fa337b82031b697ca78aca69c1fc7f46ecb328d..de6f42a80bc02c6ec3984ef04e5a12eb06818f37
@@@ -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_USER_FIELD = 'openid_lj_user'
+ OPENID_LIVEJOURNAL_URL = 'http://%s.livejournal.com'
++OPENID_LIVEJOURNAL_USER_FIELD = 'openid_lj_user'