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