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
social_user.extra_data = extra_data
social_user.save()
+ user.social_user = social_user
return user
def username(self, details):
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')
"""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
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
# 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