From: Matías Aguirre Date: Mon, 23 May 2011 03:17:07 +0000 (-0300) Subject: Use OpenID identity_url field to identify UserSocialAuth instances. Closes gh-73 X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=475fc891424b3dc2ebc2e825bae94d8da8d4c611;p=django-social-auth.git Use OpenID identity_url field to identify UserSocialAuth instances. Closes gh-73 --- diff --git a/social_auth/backends/__init__.py b/social_auth/backends/__init__.py index 4eb1f15..d7b4a2d 100644 --- a/social_auth/backends/__init__.py +++ b/social_auth/backends/__init__.py @@ -103,9 +103,7 @@ class SocialAuthBackend(ModelBackend): uid = self.get_user_id(details, response) is_new = False try: - social_user = UserSocialAuth.objects.select_related('user')\ - .get(provider=self.name, - uid=uid) + social_user = self.get_social_auth_user(uid) except UserSocialAuth.DoesNotExist: user = kwargs.get('user') if user is None: # new user @@ -149,6 +147,7 @@ class SocialAuthBackend(ModelBackend): social_user.extra_data = extra_data social_user.save() + user.social_user = social_user return user def username(self, details): @@ -245,6 +244,15 @@ class SocialAuthBackend(ModelBackend): if changed: user.save() + def get_social_auth_user(self, uid): + """Return social auth user instance for given uid for current + backend. + + Riase DoesNotExist exception if no entry. + """ + return UserSocialAuth.objects.select_related('user')\ + .get(provider=self.name, uid=uid) + def get_user_id(self, details, response): """Must return a unique ID from values returned on details""" raise NotImplementedError('Implement in subclass') @@ -300,6 +308,15 @@ class OpenIDBackend(SocialAuthBackend): """Generic OpenID authentication backend""" name = 'openid' + def get_social_auth_user(self, uid): + """Return social auth user instance for given uid. OpenId uses + identity_url to identify the user in a unique way and that value + identifies the provider too. + + Riase DoesNotExist exception if no entry. + """ + return UserSocialAuth.objects.select_related('user').get(uid=uid) + def get_user_id(self, details, response): """Return user unique id provided by service""" return response.identity_url diff --git a/social_auth/views.py b/social_auth/views.py index 23fcb63..69678d8 100644 --- a/social_auth/views.py +++ b/social_auth/views.py @@ -4,7 +4,7 @@ from django.http import HttpResponseRedirect, HttpResponse, \ HttpResponseServerError from django.core.urlresolvers import reverse from django.db import transaction -from django.contrib.auth import login, REDIRECT_FIELD_NAME +from django.contrib.auth import login, REDIRECT_FIELD_NAME, load_backend from django.contrib.auth.decorators import login_required from social_auth.backends import get_backend @@ -49,9 +49,10 @@ def complete_process(request, backend): # Set session expiration date if present and not disabled by # setting. Use last social-auth instance for current provider, # users can associate several accounts with a same provider. - backend_name = backend.AUTH_BACKEND.name - social_user = user.social_auth.filter(provider=backend_name) \ - .order_by('-id')[0] + # + # user.social_user is the used UserSocialAuth instance defined + # in authenticate process + social_user = user.social_user if social_user.expiration_delta(): request.session.set_expiry(social_user.expiration_delta()) url = request.session.pop(REDIRECT_FIELD_NAME, '') or DEFAULT_REDIRECT