-
Notifications
You must be signed in to change notification settings - Fork 3
/
oauth.py
executable file
·221 lines (167 loc) · 7.27 KB
/
oauth.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
# -*- coding: utf-8 -*-
import os, sys
import httplib
import time
import Cookie
import uuid
from log import logging
from urllib import urlencode, unquote
from urlparse import urlparse
import out
import tools
import config
class GeekNoteAuth(object):
consumerKey = config.CONSUMER_KEY
consumerSecret = config.CONSUMER_SECRET
url = {
"base" : config.USER_BASE_URL,
"oauth" : "/OAuth.action?oauth_token=%s",
"access": "/OAuth.action",
"token" : "/oauth",
"login" : "/Login.action",
}
cookies = {}
postData = {
'login': {
'login': 'Sign in',
'username': '',
'password': '',
'targetUrl': None,
},
'access': {
'authorize': 'Authorize',
'oauth_token': None,
'oauth_callback': None,
'embed': 'false',
}
}
username = None
password = None
tmpOAuthToken = None
verifierToken = None
OAuthToken = None
incorrectLogin = 0
def getTokenRequestData(self, **kwargs):
params = {
'oauth_consumer_key': self.consumerKey,
'oauth_signature': self.consumerSecret+'%26',
'oauth_signature_method': 'PLAINTEXT',
'oauth_timestamp': str(int(time.time())),
'oauth_nonce': uuid.uuid4().hex
}
if kwargs:
params = dict(params.items() + kwargs.items())
return params
def loadPage(self, url, uri=None, method="GET", params=""):
if not url:
logging.error("Request URL undefined")
tools.exit()
if not uri:
urlData = urlparse(url)
url = urlData.netloc
uri = urlData.path + '?' + urlData.query
# prepare params, append to uri
if params :
params = urlencode(params)
if method == "GET":
uri += ('?' if uri.find('?') == -1 else '&') + params
params = ""
# insert local cookies in request
headers = {
"Cookie": '; '.join( [ key+'='+self.cookies[key] for key in self.cookies.keys() ] )
}
if method == "POST":
headers["Content-type"] = "application/x-www-form-urlencoded"
logging.debug("Request URL: %s:/%s > %s # %s", url, uri, unquote(params), headers["Cookie"])
conn = httplib.HTTPSConnection(url)
conn.request(method, uri, params, headers)
response = conn.getresponse()
data = response.read()
conn.close()
logging.debug("Response : %s > %s", response.status, response.getheaders())
result = tools.Struct(status=response.status, location=response.getheader('location', None), data=data)
# update local cookies
sk = Cookie.SimpleCookie(response.getheader("Set-Cookie", ""))
for key in sk:
self.cookies[key] = sk[key].value
return result
def parseResponse(self, data):
data = unquote(data)
return dict( item.split('=', 1) for item in data.split('?')[-1].split('&') )
def getToken(self):
out.preloader.setMessage('Authorize...')
self.getTmpOAuthToken()
self.login()
out.preloader.setMessage('Allow Access...')
self.allowAccess()
out.preloader.setMessage('Getting Token...')
self.getOAuthToken()
#out.preloader.stop()
return self.OAuthToken
def getTmpOAuthToken(self):
response = self.loadPage(self.url['base'], self.url['token'], "GET",
self.getTokenRequestData(oauth_callback="https://"+self.url['base']))
if response.status != 200:
logging.error("Unexpected response status on get temporary oauth_token 200 != %s", response.status)
tools.exit()
responseData = self.parseResponse(response.data)
if not responseData.has_key('oauth_token'):
logging.error("OAuth temporary not found")
tools.exit()
self.tmpOAuthToken = responseData['oauth_token']
logging.debug("Temporary OAuth token : %s", self.tmpOAuthToken)
def login(self):
response = self.loadPage(self.url['base'], self.url['login'], "GET", {'oauth_token': self.tmpOAuthToken})
if response.status != 200:
logging.error("Unexpected response status on login 200 != %s", response.status)
tools.exit()
if not self.cookies.has_key('JSESSIONID'):
logging.error("Not found value JSESSIONID in the response cookies")
tools.exit()
# get login/password
self.username, self.password = out.GetUserCredentials()
self.postData['login']['username'] = self.username
self.postData['login']['password'] = self.password
self.postData['login']['targetUrl'] = self.url['oauth']%self.tmpOAuthToken
response = self.loadPage(self.url['base'], self.url['login']+";jsessionid="+self.cookies['JSESSIONID'], "POST",
self.postData['login'])
if not response.location and response.status == 200:
if self.incorrectLogin < 3:
out.preloader.stop()
out.printLine('Sorry, incorrect login or password')
out.preloader.setMessage('Authorize...')
self.incorrectLogin += 1
return self.login()
else:
logging.error("Incorrect login or password")
if not response.location:
logging.error("Target URL was not found in the response on login")
tools.exit()
logging.debug("Success authorize, redirect to access page")
#self.allowAccess(response.location)
def allowAccess(self):
self.postData['access']['oauth_token'] = self.tmpOAuthToken
self.postData['access']['oauth_callback'] = "https://"+self.url['base']
response = self.loadPage(self.url['base'], self.url['access'], "POST", self.postData['access'])
if response.status != 302:
logging.error("Unexpected response status on allowing access 302 != %s", response.status)
tools.exit()
responseData = self.parseResponse(response.location)
if not responseData.has_key('oauth_verifier'):
logging.error("OAuth verifier not found")
tools.exit()
self.verifierToken = responseData['oauth_verifier']
logging.debug("OAuth verifier token take")
#self.getOAuthToken(verifier)
def getOAuthToken(self):
response = self.loadPage(self.url['base'], self.url['token'], "GET",
self.getTokenRequestData(oauth_token=self.tmpOAuthToken, oauth_verifier=self.verifierToken))
if response.status != 200:
logging.error("Unexpected response status on getting oauth token 200 != %s", response.status)
tools.exit()
responseData = self.parseResponse(response.data)
if not responseData.has_key('oauth_token'):
logging.error("OAuth token not found")
tools.exit()
logging.debug("OAuth token take : %s", responseData['oauth_token'])
self.OAuthToken = responseData['oauth_token']