]> git.openstreetmap.org Git - osqa.git/blobdiff - forum_modules/oauthauth/consumer.py
Add an OSM authentication provider
[osqa.git] / forum_modules / oauthauth / consumer.py
index bf16a5415c6e12faec769baf184ca3dd62d2afa1..ee9bf97fb576a6e10837421d5f67f76ed08ed263 100644 (file)
@@ -3,9 +3,12 @@ import urllib2
 import httplib
 import time
 
+from forum.settings import APP_URL
 from forum.authentication.base import AuthenticationConsumer, InvalidAuthentication
 from django.utils.translation import ugettext as _
+from django.core.urlresolvers import reverse
 
+from settings import TWITTER_AUTO_CALLBACK_REDIRECT
 from lib import oauth2
 
 class OAuthAbstractAuthConsumer(AuthenticationConsumer):
@@ -37,7 +40,7 @@ class OAuthAbstractAuthConsumer(AuthenticationConsumer):
         if token.key != request.GET.get('oauth_token', 'no-token'):
             raise InvalidAuthentication(_("Something went wrong! Auth tokens do not match"))
 
-        access_token = self.fetch_access_token(token)
+        access_token = self.fetch_access_token(token, request.GET.get('oauth_verifier', '')) 
 
         return access_token.to_string()
 
@@ -46,7 +49,16 @@ class OAuthAbstractAuthConsumer(AuthenticationConsumer):
         return {}
         
     def fetch_request_token(self):
-        oauth_request = oauth2.Request.from_consumer_and_token(self.consumer, http_url=self.request_token_url)
+        parameters = {}
+        # If the installation is configured to automatically redirect to the Twitter provider done page -- do it.
+        if bool(TWITTER_AUTO_CALLBACK_REDIRECT):
+            callback_url = '%s%s' % (APP_URL, reverse('auth_provider_done', prefix='/', kwargs={'provider' : 'twitter'}))
+            # Pass
+            parameters.update({
+                'oauth_callback' : callback_url,
+            })
+
+        oauth_request = oauth2.Request.from_consumer_and_token(self.consumer, http_url=self.request_token_url, parameters=parameters)
         oauth_request.sign_request(self.signature_method, self.consumer, None)
         params = oauth_request
         data = urllib.urlencode(params)
@@ -62,8 +74,9 @@ class OAuthAbstractAuthConsumer(AuthenticationConsumer):
         full_url='%s?%s'%(self.authorization_url, data)
         return full_url
 
-    def fetch_access_token(self, token):
+    def fetch_access_token(self, token, oauth_verifier): 
         oauth_request = oauth2.Request.from_consumer_and_token(self.consumer, token=token, http_url=self.access_token_url)
+        oauth_request['oauth_verifier'] = oauth_verifier 
         oauth_request.sign_request(self.signature_method, self.consumer, token)
         params = oauth_request
         data = urllib.urlencode(params)
@@ -81,7 +94,7 @@ class OAuthAbstractAuthConsumer(AuthenticationConsumer):
 
         url = oauth_request.to_url()
         connection = httplib.HTTPSConnection(self.server_url)
-        connection.request(oauth_request.http_method, url)
+        connection.request("GET", url)
 
         return connection.getresponse().read()