]> git.openstreetmap.org Git - osqa.git/blob - forum_modules/oauthauth/consumer.py
Make page title more easily skinnable.
[osqa.git] / forum_modules / oauthauth / consumer.py
1 import urllib
2 import urllib2
3 import httplib
4 import time
5
6 from forum.authentication.base import AuthenticationConsumer, InvalidAuthentication
7 from django.utils.translation import ugettext as _
8
9 from lib import oauth
10
11 class OAuthAbstractAuthConsumer(AuthenticationConsumer):
12
13     def __init__(self, consumer_key, consumer_secret, server_url, request_token_url, access_token_url, authorization_url):
14         self.consumer_secret = consumer_secret
15         self.consumer_key = consumer_key
16
17         self.consumer = oauth.OAuthConsumer(consumer_key, consumer_secret)
18         self.signature_method = oauth.OAuthSignatureMethod_HMAC_SHA1()
19
20         self.server_url = server_url
21         self.request_token_url = request_token_url
22         self.access_token_url = access_token_url
23         self.authorization_url = authorization_url
24
25     def prepare_authentication_request(self, request, redirect_to):
26         request_token = self.fetch_request_token()
27         request.session['unauthed_token'] = request_token.to_string()
28         return self.authorize_token_url(request_token)
29
30     def process_authentication_request(self, request):
31         unauthed_token = request.session.get('unauthed_token', None)
32         if not unauthed_token:
33              raise InvalidAuthentication(_('Error, the oauth token is not on the server'))
34
35         token = oauth.OAuthToken.from_string(unauthed_token)
36
37         if token.key != request.GET.get('oauth_token', 'no-token'):
38             raise InvalidAuthentication(_("Something went wrong! Auth tokens do not match"))
39
40         access_token = self.fetch_access_token(token)
41
42         return access_token.to_string()
43
44     def get_user_data(self, key):
45         #token = oauth.OAuthToken.from_string(access_token)
46         return {}
47         
48     def fetch_request_token(self):
49         oauth_request = oauth.OAuthRequest.from_consumer_and_token(self.consumer, http_url=self.request_token_url)
50         oauth_request.sign_request(self.signature_method, self.consumer, None)
51         params = oauth_request.parameters
52         data = urllib.urlencode(params)
53         full_url='%s?%s'%(self.request_token_url, data)
54         response = urllib2.urlopen(full_url)
55         return oauth.OAuthToken.from_string(response.read())
56
57     def authorize_token_url(self, token, callback_url=None):
58         oauth_request = oauth.OAuthRequest.from_token_and_callback(token=token,\
59                         callback=callback_url, http_url=self.authorization_url)
60         params = oauth_request.parameters
61         data = urllib.urlencode(params)
62         full_url='%s?%s'%(self.authorization_url, data)
63         return full_url
64
65     def fetch_access_token(self, token):
66         oauth_request = oauth.OAuthRequest.from_consumer_and_token(self.consumer, token=token, http_url=self.access_token_url)
67         oauth_request.sign_request(self.signature_method, self.consumer, token)
68         params = oauth_request.parameters
69         data = urllib.urlencode(params)
70         full_url='%s?%s'%(self.access_token_url, data)
71         response = urllib2.urlopen(full_url)
72         return oauth.OAuthToken.from_string(response.read())
73
74     def fetch_data(self, token, http_url, parameters=None):
75         access_token = oauth.OAuthToken.from_string(token)
76         oauth_request = oauth.OAuthRequest.from_consumer_and_token(
77             self.consumer, token=access_token, http_method="GET",
78             http_url=http_url, parameters=parameters,
79         )
80         oauth_request.sign_request(self.signature_method, self.consumer, access_token)
81
82         url = oauth_request.to_url()
83         connection = httplib.HTTPSConnection(self.server_url)
84         connection.request(oauth_request.http_method, url)
85
86         return connection.getresponse().read()
87