-
Notifications
You must be signed in to change notification settings - Fork 0
/
flickrAuth.py
260 lines (212 loc) · 8.26 KB
/
flickrAuth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/env python
""" Provides abstraction for authentication.
Implements several methods that helps simplifying the code in consumer
"""
# imports
import httplib2
import json;
import os.path;
from os.path import expanduser;
import time
import urlparse
import webbrowser;
import oauth2
# description
__author__ = "Viral Rathod"
__copyright__ = "Copyright 2014"
__credits__ = ["Viral Rathod"]
__license__ = "MIT"
__version__ = "0.0.1"
__maintainer__ = "Viral Rathod"
__status__ = "Development"
# local imports
import flickrAuthConstants;
class flickrOAuth(object):
'''
classdocs
'''
# data attributes
consumer = None;
token = None;
req_token = dict();
access_token = dict();
# token file
fileName = ".flickrrc"
filePath = "";
def __init__(self):
'''
Constructor
'''
# Setup the Consumer with the api_keys given by the provider
self.consumer = oauth2.Consumer(key=flickrAuthConstants.keys['apikey'],
secret=flickrAuthConstants.keys['apisecret']);
self.filePath = expanduser('~') + "/" + self.fileName;
# check if we have previous token serialized
self.deSerializeToken();
#if not, its user responsibility
if ( not self.isAuthenticated()) :
self.access_token = dict();
else :
self.token = oauth2.Token(self.access_token['oauth_token'], self.access_token['oauth_token_secret'])
'''
request_token(api_key, api_secret)
Given api_key and api_secret, it requests oauth token.
returns dictionary of token
in case of error, it will return error details in dictionary
in case offline, it will return None
'''
def request_token(self):
# parameters
params = {
'oauth_signature_method':"HMAC-SHA1",
'oauth_version': "1.0",
'oauth_callback': "oob",
'oauth_timestamp': str(int(time.time())),
'oauth_nonce': oauth2.generate_nonce(),
# 'oauth_consumer_key': keys['apikey']
'oauth_consumer_key': flickrAuthConstants.keys['apikey']
}
# Create our request. Change method, etc. accordingly.
req = oauth2.Request(method="GET",
url=flickrAuthConstants.request_token_url,
parameters=params)
# Create the signature
signature = oauth2.SignatureMethod_HMAC_SHA1().sign(req, self.consumer, None)
# Add the Signature to the request
req['oauth_signature'] = signature
try:
# Make the request to get the oauth_token and the oauth_token_secret
# I had to directly use the httplib2 here, instead of the oauth library.
h = httplib2.Http(".cache")
resp, content = h.request(req.to_url(), "GET")
# lets get contents into dictionary
self.req_token = dict(urlparse.parse_qsl(content))
# Create the token object with returned oauth_token and oauth_token_secret
self.token = oauth2.Token(self.req_token['oauth_token'], self.req_token['oauth_token_secret'])
except Exception, e:
print e;
self.token = None;
return self.token;
'''
getAuthorizeURL(request_token_response)
Given valid response of request_token() call, this function will generate authorization
URL and return it
Note:
1. the caller shall use this url and open in a web-browser
2. it will ask user to authorize the request
3. Once user authorizes, Flickr shall generate a PIN
4. User should use that pin to validate the authorization for this app
'''
def getAuthorizeURL(self):
return "%s?oauth_token=%s&perms=read" % (flickrAuthConstants.authorize_url, self.req_token['oauth_token'])
'''
validateAuthorization(api_key, pin) :
Given api_key and PIN, it shall confirm and get OAuth LLTs
return true if LLT received successfully, false otherwise
Note: if returns true, it shall store LLT in ~/.flickrToken
TODO: encrypt it
'''
def validateAuthorization(self, pin) :
#Generate Token
self.token.set_verifier(pin)
#access params
access_token_params = {
'oauth_consumer_key': flickrAuthConstants.keys['apikey'],
'oauth_nonce': oauth2.generate_nonce(),
'oauth_signature_method':"HMAC-SHA1",
'oauth_timestamp': str(int(time.time())),
'oauth_token': self.req_token['oauth_token'],
'oauth_verifier' : pin
}
#setup request
req = oauth2.Request(method="GET", url=flickrAuthConstants.access_token_url,
parameters=access_token_params)
#create the signature
signature = oauth2.SignatureMethod_HMAC_SHA1().sign(req,self.consumer,self.token)
# assign the signature to the request
req['oauth_signature'] = signature
try:
#make the request
h = httplib2.Http(".cache")
resp, content = h.request(req.to_url(), "GET")
#parse the response
self.access_token = dict(urlparse.parse_qsl(content))
except Exception, e:
print e;
return self.isAuthenticated();
'''
isAuthenticated
'''
def isAuthenticated(self):
if('oauth_token' in self.access_token and 'oauth_token_secret' in self.access_token):
return True;
'''
validateTokenOnFlickr(oauth_token)
'''
def validateTokenOnFlickr(self):
params = {
'method':'flickr.auth.oauth.checkToken',
'oauth_nonce': oauth2.generate_nonce(),
'oauth_signature_method':"HMAC-SHA1",
'oauth_timestamp': str(int(time.time())),
'oauth_consumer_key':flickrAuthConstants.keys['apikey'],
'oauth_token':self.access_token['oauth_token']
,'format':'json'
,'nojsoncallback':1
}
# Create our request. Change method, etc. accordingly.
req = oauth2.Request(method="GET",
url=flickrAuthConstants.base_url,
parameters=params);
# Create the signature
signature = oauth2.SignatureMethod_HMAC_SHA1().sign(req, self.consumer, self.token);
req['oauth_signature'] = signature;
h = httplib2.Http(".cache")
resp, jsonData = h.request(req.to_url(), "GET")
content = json.loads(jsonData);
return content['stat'];
'''
authenticate
'''
def authenticate(self):
if self.isAuthenticated():
print "Already authenticated";
return True;
if not self.request_token() :
print "Could not fetch request token";
return False;
auth_url = self.getAuthorizeURL();
webbrowser.open_new(auth_url);
# Once you get the verified pin, input it
accepted = 'y'
while accepted.lower() != 'y':
accepted = raw_input('Have you authorized me? (y/n) ');
oauth_verifier = raw_input('What is the PIN? ')
if (self.validateAuthorization(oauth_verifier)) :
print "validated"
self.serializeToken()
print "serialized";
return True;
else:
return False;
def serializeToken(self):
with open(self.filePath, 'w') as f:
f.write(self.access_token['oauth_token'] + '\n');
f.write(self.access_token['oauth_token_secret']);
f.close()
def deSerializeToken(self):
if not os.path.isfile(self.filePath):
# print "local token not found";
return;
try:
with open(self.filePath, 'r') as f:
#read oauth_token
line = f.readline().strip(" \r\n");
if line.__len__() > 0:
self.access_token['oauth_token'] = line;
#read token secret
line = f.readline().strip(" \r\n");
if line.__len__() > 0:
self.access_token['oauth_token_secret'] = line;
except IOError:
print "exception"