Commits

offline  committed b010258

email backend added

  • Participants
  • Parent commits 5ab638f

Comments (0)

Files changed (4)

File openauth/backends/email.py

+from __future__ import absolute_import
+
+from django import forms
+from openauth.backends import BaseBackend
+from django.utils.translation import ugettext as _
+
+from openauth import settings
+from openauth.fields import PasswordField
+
+
+class EmailBackend(BaseBackend):
+
+    email = forms.EmailField()
+    password = PasswordField()
+
+    def clean(self):
+        """
+        Validate cridentials and login the user in case of success.
+        If user is not activated and ACTIVATION_REQUIRED set to True
+        form will not validate.
+        """
+        super(EmailBackend, self).clean()
+        if self.is_valid():
+            email = self.cleaned_data['email'].lower()
+            password = self.cleaned_data['password']
+            user = get_object_or_None(User, email=email)
+            if user and user.check_password(password):
+                if settings.ACTIVATION_REQUIRED and not user.is_active:
+                    raise forms.ValidationError(_(u'Your account is not active. Please activate it.'))
+                user.backend = 'django.contrib.auth.backends.ModelBackend'
+                login_user(self.request, user)
+                raise Redirect(settings.LOGIN_REDIRECT_URL)
+            else:
+                raise forms.ValidationError(_(u'Incorrect authentication data'))
+
+
+

File openauth/backends/twitter.py

 from openauth.models import SocialID
 
 
-CONSUMER_KEY = "g7ajf7OPlUzhMM9JBPk3ZQ"
-CONSUMER_SECRET = "MCKHvJquCldbTjLG3rCwyVwWhsQkaX1X8JQy83dDHQ"
-REQUEST_TOKEN_URL = "http://twitter.com/oauth/request_token"
-ACCESS_TOKEN_URL = "http://twitter.com/oauth/access_token"
-AUTHORIZE_URL = "http://twitter.com/oauth/authorize"
-
-
 class TwitterBackend(BaseBackend):
 
+    CONSUMER_KEY = property(lambda self: getattr(settings, "%s_CONSUMER_KEY" % self.privider.upper()))
+    CONSUMER_SECRET = property(lambda self: getattr(settings, "%s_CONSUMER_SECRET" % self.privider.upper()))
+    REQUEST_TOKEN_URL = property(lambda self: getattr(settings, "%s_REQUEST_TOKEN_URL" % self.privider.upper()))
+    ACCESS_TOKEN_URL = property(lambda self: getattr(settings, "%s_ACCESS_TOKEN_URL" % self.privider.upper()))
+    AUTHORIZE_URL = property(lambda self: getattr(settings, "%s_AUTHORIZE_URL" % self.privider.upper()))
+
     def clean(self):
-        consumer = OAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET)
+        consumer = OAuthConsumer(self.CONSUMER_KEY, self.CONSUMER_SECRET)
         signature_method = OAuthSignatureMethod_HMAC_SHA1()
         callback = self.request.build_absolute_uri(reverse('openauth-social-complete', args=[self.provider]))
-        oauth_req = OAuthRequest.from_consumer_and_token(consumer, callback=callback, http_url=REQUEST_TOKEN_URL)
+        oauth_req = OAuthRequest.from_consumer_and_token(consumer, callback=callback, http_url=self.REQUEST_TOKEN_URL)
         oauth_req.sign_request(signature_method, consumer, None)
         response = urllib.urlopen(oauth_req.to_url()).read()
 
         token = OAuthToken.from_string(response) # instatiate token
 
-        oauth_req = OAuthRequest.from_consumer_and_token(consumer, token, http_url=AUTHORIZE_URL)
+        oauth_req = OAuthRequest.from_consumer_and_token(consumer, token, http_url=self.AUTHORIZE_URL)
         oauth_req.sign_request(signature_method, consumer, token)
         raise Redirect(oauth_req.to_url())
         
     def complete(self, request, response):
         signature_method = OAuthSignatureMethod_HMAC_SHA1()
         data = request.GET.copy()
-        consumer = OAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET)
+        consumer = OAuthConsumer(self.CONSUMER_KEY, self.CONSUMER_SECRET)
         oauth_token = data['oauth_token']
         oauth_verifier = data['oauth_verifier']
-        oauth_req = OAuthRequest.from_consumer_and_token(consumer, http_url=ACCESS_TOKEN_URL)
+        oauth_req = OAuthRequest.from_consumer_and_token(consumer, http_url=self.ACCESS_TOKEN_URL)
         oauth_req.set_parameter('oauth_token', oauth_token)
         oauth_req.set_parameter('oauth_verifier', oauth_verifier)
         oauth_req.sign_request(signature_method, consumer, None)

File openauth/settings.py

     'google': 'openauth.backends.google.GoogleBackend',                                            #
     'twitter': 'openauth.backends.twitter.TwitterBackend',                                         #
     'facebook': 'openauth.backends.facebook.FacebookBackend',                                      #
+    'email': 'openauth.backends.email.EmailBackend',
     #'oauth': 'openauth.backends.oauth.OAuthBackend',                                               #
     }                                                                                              #
 )                                                                                                  #

File openauth/views.py

         form = get_instance_from_path(path, data, request=request, provider=provider)
         next_url = request.GET.get("next") or settings.LOGIN_REDIRECT_URL
         request.session['next_url'] = next_url
-        form.is_valid()
+        if form.is_valid():
+            form.save()
     else:
         form = get_instance_from_path(settings.DEFAULT_LOGIN_FORM)