From: Matías Aguirre Date: Mon, 10 Jan 2011 23:07:54 +0000 (-0200) Subject: Moved auth backend access to auth module, small docstring changes X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=0c4adab399af37c57a73b5cc01af6cea5d4a13e8;p=django-social-auth.git Moved auth backend access to auth module, small docstring changes --- diff --git a/social_auth/auth.py b/social_auth/auth.py index 0826af0..b756e1b 100644 --- a/social_auth/auth.py +++ b/social_auth/auth.py @@ -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) diff --git a/social_auth/models.py b/social_auth/models.py index 001993e..6305c10 100644 --- a/social_auth/models.py +++ b/social_auth/models.py @@ -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) diff --git a/social_auth/signals.py b/social_auth/signals.py index ed5a8ae..b65abfa 100644 --- a/social_auth/signals.py +++ b/social_auth/signals.py @@ -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. diff --git a/social_auth/store.py b/social_auth/store.py index 1424289..ab7ebb4 100644 --- a/social_auth/store.py +++ b/social_auth/store.py @@ -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, diff --git a/social_auth/views.py b/social_auth/views.py index 1c1d8a9..248d6fa 100644 --- a/social_auth/views.py +++ b/social_auth/views.py @@ -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: