6 from forum.authentication.base import AuthenticationConsumer, InvalidAuthentication
\r
7 from django.utils.translation import ugettext as _
\r
9 from lib import oauth
\r
11 class OAuthAbstractAuthConsumer(AuthenticationConsumer):
\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
17 self.consumer = oauth.OAuthConsumer(consumer_key, consumer_secret)
\r
18 self.signature_method = oauth.OAuthSignatureMethod_HMAC_SHA1()
\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
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
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
35 token = oauth.OAuthToken.from_string(unauthed_token)
\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
40 access_token = self.fetch_access_token(token)
\r
42 return access_token.to_string()
\r
44 def get_user_data(self, key):
\r
45 #token = oauth.OAuthToken.from_string(access_token)
\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
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
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
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
80 oauth_request.sign_request(self.signature_method, self.consumer, access_token)
\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
86 return connection.getresponse().read()
\r