]> git.parisson.com Git - django-social-auth.git/commitdiff
Moved auth backend access to auth module, small docstring changes
authorMatías Aguirre <matiasaguirre@gmail.com>
Mon, 10 Jan 2011 23:07:54 +0000 (21:07 -0200)
committerMatías Aguirre <matiasaguirre@gmail.com>
Mon, 10 Jan 2011 23:07:54 +0000 (21:07 -0200)
social_auth/auth.py
social_auth/models.py
social_auth/signals.py
social_auth/store.py
social_auth/views.py

index 0826af0469537138964624b7bfd1f43892eb2fdf..b756e1be77805b00b5961f553b2d25a316f781dc 100644 (file)
@@ -377,3 +377,18 @@ class FacebookAuth(BaseOAuth):
             return simplejson.load(urllib.urlopen(url))
         except simplejson.JSONDecodeError:
             return None
+
+
+# Authentication backends
+BACKENDS = {
+    'twitter': TwitterAuth,
+    'facebook': FacebookAuth,
+    'google': GoogleAuth,
+    'yahoo': YahooAuth,
+    'orkut': OrkutAuth,
+    'openid': OpenIdAuth,
+}
+
+def get_backend(name, *args, **kwargs):
+    """Return auth backend instance *if* it's registered, None in other case"""
+    return BACKENDS.get(name, lambda *args, **kwargs: None)(*args, **kwargs)
index 001993e751e0eaee5a1691777aded4baceb33c1b..6305c10c55fde41e891e2c774730d374f54a475e 100644 (file)
@@ -53,6 +53,10 @@ class Nonce(models.Model):
     timestamp = models.IntegerField()
     salt = models.CharField(max_length=40)
 
+    def __unicode__(self):
+        """Unicode representation"""
+        return self.server_url
+
 
 class Association(models.Model):
     """OpenId account association"""
@@ -62,3 +66,7 @@ class Association(models.Model):
     issued = models.IntegerField()
     lifetime = models.IntegerField()
     assoc_type = models.CharField(max_length=64)
+
+    def __unicode__(self):
+        """Unicode representation"""
+        return '%s %s' % (self.handle, self.issued)
index ed5a8ae0a77ff4ebe757fb9479b092be7e50d55b..b65abfa9a115fac388b42e41e388714cee756e20 100644 (file)
@@ -2,10 +2,9 @@
 from django.dispatch import Signal
 
 # Pre save signal
-#   This signal is sent when user instance values is about to be
-#   updated with new values from services provided. This way custom
-#   actions can be attached and values updated if needed before the
-#   saving time.
+#   This signal is sent when user instance is about to be updated with
+#   new values from services provided. This way custom actions can be
+#   attached and values updated if needed before the saving time.
 #
 #   Handlers must return True if any value was updated/changed,
 #   otherwise must return any non True value.
index 142428934842a70f4a242f760468a47ccb528ea1..ab7ebb44ddbd7f212a5a7cb60ed024c7c29f30a0 100644 (file)
@@ -50,12 +50,12 @@ class DjangoOpenIDStore(OpenIDStore):
         if expired:  # clear expired associations
             Association.objects.filter(pk__in=expired).delete()
 
-        if associations:
+        if associations: # return most recet association
             associations.sort(key=lambda x: x.issued, reverse=True)
             return associations[0]
 
     def useNonce(self, server_url, timestamp, salt):
-        """Generate one use number and return if it was created"""
+        """Generate one use number and return *if* it was created"""
         if abs(timestamp - time.time()) > SKEW:
             return False
         return Nonce.objects.get_or_create(server_url=server_url,
index 1c1d8a9eec06f437f70381b4372c348282929022..248d6fa1d4a3007384fcbe87f1e51b35b1ca9df8 100644 (file)
@@ -6,19 +6,7 @@ from django.core.urlresolvers import reverse
 from django.contrib.auth import login, REDIRECT_FIELD_NAME
 from django.contrib.auth.decorators import login_required
 
-from .auth import TwitterAuth, FacebookAuth, OpenIdAuth, GoogleAuth, \
-                  YahooAuth, OrkutAuth
-
-
-# Authentication backends
-BACKENDS = {
-    'twitter': TwitterAuth,
-    'facebook': FacebookAuth,
-    'google': GoogleAuth,
-    'yahoo': YahooAuth,
-    'openid': OpenIdAuth,
-    'orkut': OrkutAuth,
-}
+from .auth import get_backend
 
 
 def auth(request, backend):
@@ -31,9 +19,9 @@ def auth(request, backend):
 
 def complete(request, backend):
     """Authentication complete process"""
-    if backend not in BACKENDS:
+    backend = get_backend(backend, request, request.path)
+    if not backend:
         return HttpResponseServerError('Incorrect authentication service')
-    backend = BACKENDS[backend](request, request.path)
     user = backend.auth_complete()
     if user and getattr(user, 'is_active', True):
         login(request, user)
@@ -56,9 +44,9 @@ def associate(request, backend):
 @login_required
 def associate_complete(request, backend):
     """Authentication complete process"""
-    if backend not in BACKENDS:
+    backend = get_backend(backend, request, request.path)
+    if not backend:
         return HttpResponseServerError('Incorrect authentication service')
-    backend = BACKENDS[backend](request, request.path)
     backend.auth_complete(user=request.user)
     url = request.session.pop(REDIRECT_FIELD_NAME, '') or \
           getattr(settings, 'LOGIN_REDIRECT_URL', '')
@@ -67,12 +55,12 @@ def associate_complete(request, backend):
 
 def auth_process(request, backend, complete_url_name, default_final_url):
     """Authenticate using social backend"""
-    if backend not in BACKENDS:
+    redirect = reverse(complete_url_name, args=(backend,))
+    backend = get_backend(backend, request, redirect)
+    if not backend:
         return HttpResponseServerError('Incorrect authentication service')
     request.session[REDIRECT_FIELD_NAME] = request.GET.get(REDIRECT_FIELD_NAME,
                                                            default_final_url)
-    redirect = reverse(complete_url_name, args=(backend,))
-    backend = BACKENDS[backend](request, redirect)
     if backend.uses_redirect:
         return HttpResponseRedirect(backend.auth_url())
     else: