forked from ubraz/GGTwitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoAuth.lua
349 lines (268 loc) · 10.1 KB
/
oAuth.lua
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
-- Project: Twitter sample app
--
-- File name: oAuth.lua
--
-- Author: Corona Labs
--
-- Abstract: Demonstrates how to connect to Twitter using Oauth Authenication.
--
-- History
-- March 5, 2013 Uses network.request to access twitter.com (https)
--
-- Sample code is MIT licensed, see http://www.coronalabs.com/links/code/license
-- Copyright (C) 2010-2013 Corona Labs Inc. All Rights Reserved.
-----------------------------------------------------------------------------------------
module(...,package.seeall)
local http = require("socket.http")
local ltn12 = require("ltn12")
local crypto = require("crypto")
local mime = require("mime")
local MultipartFormData = require ("multipartForm")
-----------------------------------------------------------------------------------------
-- GET REQUEST TOKEN (step 1)
-- Added callback for response
-----------------------------------------------------------------------------------------
--
function getRequestToken( consumer_key, token_ready_url, request_token_url,
consumer_secret, callback )
local post_data =
{
oauth_consumer_key = consumer_key,
oauth_timestamp = get_timestamp(),
oauth_version = '1.0',
oauth_nonce = get_nonce(),
oauth_callback = token_ready_url,
oauth_signature_method = "HMAC-SHA1"
}
local post_data = oAuthSign(request_token_url, "POST", post_data, consumer_secret)
return rawPostRequest(request_token_url, post_data, callback)
end
-----------------------------------------------------------------------------------------
-- GET ACCESS TOKEN (step 3)
-- Added callback for response
-----------------------------------------------------------------------------------------
--
function getAccessToken(token, verifier, token_secret, consumer_key, consumer_secret,
access_token_url, callback)
local post_data =
{
oauth_consumer_key = consumer_key,
oauth_timestamp = get_timestamp(),
oauth_version = '1.0',
oauth_nonce = get_nonce(),
oauth_token = token,
oauth_token_secret = token_secret,
oauth_verifier = verifier,
oauth_signature_method = "HMAC-SHA1"
}
local post_data = oAuthSign(access_token_url, "POST", post_data, consumer_secret)
return rawPostRequest(access_token_url, post_data, callback)
end
-----------------------------------------------------------------------------------------
-- MAKE REQUEST
-- Added callback for response
-----------------------------------------------------------------------------------------
--
function makeRequest(url, body, consumer_key, token, consumer_secret, token_secret, method, callback)
local post_data =
{
oauth_consumer_key = consumer_key,
oauth_nonce = get_nonce(),
oauth_signature_method = "HMAC-SHA1",
oauth_token = token,
oauth_timestamp = get_timestamp(),
oauth_version = '1.0',
oauth_token_secret = token_secret
}
for i=1, #body do
post_data[body[i].key] = body[i].value
end
local post_data = oAuthSign(url, method, post_data, consumer_secret)
local result
if method == "POST" then
result = rawPostRequest(url, post_data, callback)
else
result = rawGetRequest(post_data, callback)
end
return result
end
function makeRequestWithMedia(url, body, media, consumer_key, token, consumer_secret, token_secret, method, callback)
local post_data =
{
oauth_consumer_key = consumer_key,
oauth_nonce = get_nonce(),
oauth_signature_method = "HMAC-SHA1",
oauth_token = token,
oauth_timestamp = get_timestamp(),
oauth_version = '1.0',
oauth_token_secret = token_secret
}
local auth = oAuthSign(url, method, post_data, consumer_secret, true)
local result = rawPostRequestMultipart(url, auth, body, media, callback)
return result
end
-----------------------------------------------------------------------------------------
-- OAUTH SIGN
-- Adds "oauth_signature=xyz" to results
-----------------------------------------------------------------------------------------
--
function oAuthSign(url, method, args, consumer_secret, multipart)
local token_secret = args.oauth_token_secret or ""
args.oauth_token_secret = nil
local keys_and_values = {}
for key, val in pairs(args) do
table.insert(keys_and_values,
{
key = encode_parameter(key),
val = encode_parameter(val)
})
end
table.sort(keys_and_values, function(a,b)
if a.key < b.key then
return true
elseif a.key > b.key then
return false
else
return a.val < b.val
end
end)
local key_value_pairs = {}
for _, rec in pairs(keys_and_values) do
table.insert(key_value_pairs, rec.key .. "=" .. rec.val)
end
local query_string_except_signature = table.concat(key_value_pairs, "&")
local sign_base_string
sign_base_string = method .. '&' .. encode_parameter(url) .. '&'
.. encode_parameter(query_string_except_signature)
local key = encode_parameter(consumer_secret) .. '&' .. encode_parameter(token_secret)
local hmac_binary = sha1(sign_base_string, key, true)
local hmac_b64 = mime.b64(hmac_binary)
local query_string = query_string_except_signature .. '&oauth_signature=' .. encode_parameter(hmac_b64)
--If this is a multipart request, we do not need to include the post body in the signature.
if multipart then
key_value_pairs = {}
for _, rec in pairs(keys_and_values) do
table.insert(key_value_pairs, rec.key .. "=\"" .. rec.val .. "\"")
end
query_string_except_signature = table.concat(key_value_pairs, ", ")
local auth = "OAuth " .. query_string_except_signature .. ', oauth_signature=\"' .. encode_parameter(hmac_b64) .."\""
return auth
end
if method == "GET" then
return url .. "?" .. query_string
else
return query_string
end
end
-----------------------------------------------------------------------------------------
-- ENCODE PARAMETER (URL_Encode)
-- Replaces unsafe URL characters with %hh (two hex characters)
-----------------------------------------------------------------------------------------
--
function encode_parameter(str)
return str:gsub('[^-%._~a-zA-Z0-9]',
function(c)
return string.format("%%%02x",c:byte()):upper()
end
)
end
-----------------------------------------------------------------------------------------
-- SHA 1
-- Returns a SHA1 hash of string
-----------------------------------------------------------------------------------------
--
function sha1(str,key,binary)
binary = binary or false
return crypto.hmac(crypto.sha1,str,key,binary)
end
-----------------------------------------------------------------------------------------
-- GET NONCE
-- Returns a random generated string
-----------------------------------------------------------------------------------------
--
function get_nonce()
return mime.b64(crypto.hmac(crypto.sha1,tostring(math.random()) .. "random"
.. tostring(os.time()),"keyyyy") )
end
-----------------------------------------------------------------------------------------
-- GET TIMESTAMP
-----------------------------------------------------------------------------------------
--
function get_timestamp()
return tostring(os.time() + 1)
end
-----------------------------------------------------------------------------------------
-- RAW GET REQUEST
-- Added callback when response is ready
-----------------------------------------------------------------------------------------
--
function rawGetRequest(url, callback)
-- Callback from network loader
local function networkListener( event )
if event.isError then
print( "Network error!", event.status, event.response)
else
-- print ( "RESPONSE: ", event.status, event.response ) -- **debug
end
if callback then
callback( event.isError, event.response) -- return with response
end
end
local params = {}
local headers = {}
headers["Content-Type"] = "application/x-www-form-urlencoded"
params.headers = headers
local result = network.request( url, "GET", networkListener, params)
return result
end
-----------------------------------------------------------------------------------------
-- RAW POST REQUEST
-- Added callback when response is ready
-----------------------------------------------------------------------------------------
--
function rawPostRequest(url, rawdata, callback)
-- Callback from network loader
local function networkListener( event )
if event.isError then
print( "Network error!", event.status, event.response)
else
-- print ( "RESPONSE: ", event.status, event.response ) -- **debug
end
if callback then
callback( event.isError, event.response) -- return with response
end
end
local params = {}
local headers = {}
headers["Content-Type"] = "application/x-www-form-urlencoded"
params.headers = headers
params.body = rawdata
local result = network.request( url, "POST", networkListener, params)
return result
end
function rawPostRequestMultipart(url, auth, body, image, callback)
local r,c,h
local response = {}
local params = {}
local multipart = MultipartFormData.new()
multipart:addHeader("Authorization", auth)
multipart:addFile("media_data[]", system.pathForFile( image, system.DocumentsDirectory ), "application/octet-stream", "./"..image)
for i=1, #body do
multipart:addField(body[i].key, body[i].value)
end
params.body = multipart:getBody()
params.headers = multipart:getHeaders()
-- Callback from network loader
local function networkListener( event )
if event.isError then
print( "Network error!", event.status, event.response)
else
-- print ( "RESPONSE: ", event.status, event.response ) -- **debug
end
if callback then
callback( event.isError, event.response) -- return with response
end
end
local result = network.request( url, "POST", networkListener, params)
return result
end