Commits

Sarvi Shanmugham  committed bc74646

Added support for separate account creation using social auth.
Instead of automatically creating accouts. Now you can create accounts with
a separate view and URL. The regular login URL will not automatically create
accounts. The old behaviour is left alone using the settings flag.

Added support for separate settings for redirect URLs for userena account
creattions.
Some cleanup and New templates.

  • Participants
  • Parent commits 18697cc

Comments (0)

Files changed (3)

File social_auth/backends/__init__.py

             created=False
             updated=False
             if user is None:  # new user
-                if not getattr(settings, 'SOCIAL_AUTH_CREATE_USERS', True):
+                if not kwargs['autocreate']:
                     return None
                 username = self.username(details)
                 email = details.get('email')
     """
     AUTH_BACKEND = None
 
-    def __init__(self, request, redirect):
+    def __init__(self, request, redirect, autocreate=True):
         self.request = request
         self.data = request.POST if request.method == 'POST' else request.GET
         self.redirect = redirect
+        self.autocreate = autocreate
 
     def auth_url(self):
         """Must return redirect URL to auth provider"""
         if not response:
             raise ValueError('This is an OpenID relying party endpoint')
         elif response.status == SUCCESS:
-            kwargs.update({'response': response, self.AUTH_BACKEND.name: True})
+            kwargs.update({'response': response, 
+                           self.AUTH_BACKEND.name: True,
+                           'autocreate':self.autocreate})
             return authenticate(*args, **kwargs)
         elif response.status == FAILURE:
             raise ValueError('OpenID authentication failed: %s' % \
 
 class BaseOAuth(BaseAuth):
     """OAuth base class"""
-    def __init__(self, request, redirect):
+    def __init__(self, request, redirect,autocreate=True):
         """Init method"""
-        super(BaseOAuth, self).__init__(request, redirect)
+        super(BaseOAuth, self).__init__(request, redirect,autocreate)
         self.redirect_uri = self.request.build_absolute_uri(self.redirect)
 
 
         if data is not None:
             data['access_token'] = access_token.to_string()
 
-        kwargs.update({'response': data, self.AUTH_BACKEND.name: True})
+        kwargs.update({'response': data, 
+                       self.AUTH_BACKEND.name: True,
+                       'autocreate':self.autocreate})
         return authenticate(*args, **kwargs)
 
     def unauthorized_token(self):

File social_auth/urls.py

 """URLs module"""
 from django.conf.urls.defaults import patterns, url
 
-from social_auth.views import auth, complete, associate, associate_complete, \
-                              disconnect
+from social_auth.views import auth, complete, create, create_complete, \
+                              associate, associate_complete, disconnect
 
 
 urlpatterns = patterns('',
+    url(r'^create/(?P<backend>[^/]+)/$', create, name='create_begin'),
+    url(r'^create/complete/(?P<backend>[^/]+)/$', create_complete,
+        name='create_complete'),
     url(r'^login/(?P<backend>[^/]+)/$', auth, name='begin'),
     url(r'^complete/(?P<backend>[^/]+)/$', complete, name='complete'),
     url(r'^associate/(?P<backend>[^/]+)/$', associate, name='associate_begin'),

File social_auth/views.py

 
 from social_auth.backends import get_backend
 
-
-def auth(request, backend):
-    """Start authentication process"""
-    complete_url = getattr(settings, 'SOCIAL_AUTH_COMPLETE_URL_NAME',
-                           'complete')
-    redirect = getattr(settings, 'LOGIN_REDIRECT_URL', '')
+def create(request, backend):
+    """Authentication starting process"""
+    complete_url = getattr(settings, 'SOCIAL_AUTH_CREATE_URL_NAME',
+                           'create_complete')
+    redirect = getattr(settings, 'SOCIAL_AUTH_CREATE_REDIRECT_URL', '')
     return auth_process(request, backend, complete_url, redirect)
 
 
-def complete(request, backend):
+def create_complete(request, backend):
     """Authentication complete process"""
-    backend = get_backend(backend, request, request.path)
+    backend = get_backend(backend, request, request.path, autocreate=True)
     if not backend:
         return HttpResponseServerError('Incorrect authentication service')
 
     return HttpResponseRedirect(url)
 
 
+def auth(request, backend):
+    """Start authentication process"""
+    complete_url = getattr(settings, 'SOCIAL_AUTH_COMPLETE_URL_NAME',
+                           'complete')
+    redirect = getattr(settings, 'LOGIN_REDIRECT_URL', '')
+    return auth_process(request, backend, complete_url, redirect)
+
+
+def complete(request, backend):
+    """Authentication complete process"""
+    backend = get_backend(backend, request, request.path, 
+                          autocreate=getattr(settings, 'SOCIAL_AUTH_CREATE_USERS', True))
+    if not backend:
+        return HttpResponseServerError('Incorrect authentication service')
+
+    try:
+        user = backend.auth_complete()
+    except ValueError, e: # some Authentication error ocurred
+        user = None
+        error_key = getattr(settings, 'SOCIAL_AUTH_ERROR_KEY', None)
+        if error_key: # store error in session
+            request.session[error_key] = str(e)
+
+    if user and getattr(user, 'is_active', True):
+        login(request, user)
+        url = request.session.pop(REDIRECT_FIELD_NAME, '') or \
+              getattr(settings, 'LOGIN_REDIRECT_URL', '')
+    else:
+        url = getattr(settings, 'LOGIN_ERROR_URL', settings.LOGIN_URL)
+    return HttpResponseRedirect(url)
+
+
+
 @login_required
 def associate(request, backend):
     """Authentication starting process"""
     complete_url = getattr(settings, 'SOCIAL_AUTH_ASSOCIATE_URL_NAME',
                            'associate_complete')
-    redirect = getattr(settings, 'LOGIN_REDIRECT_URL', '')
+    redirect = getattr(settings, 'SOCIAL_AUTH_ASSOCIATE_REDIRECT_URL', '')
     return auth_process(request, backend, complete_url, redirect)
 
 
 @login_required
 def associate_complete(request, backend):
     """Authentication complete process"""
-    backend = get_backend(backend, request, request.path)
+    backend = get_backend(backend, request, request.path, 
+                          autocreate=False)
     if not backend:
         return HttpResponseServerError('Incorrect authentication service')
     backend.auth_complete(user=request.user)