forked from wrxck/mattata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OAuth2.lua
192 lines (161 loc) · 6.64 KB
/
OAuth2.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
local Http = require "socket.http"
--local Https = require "ssl.https"
--local Https = require "https"
require "https"
local Https = ssl.https
local Base64 = require "base64"
local Ltn12 = require "ltn12"
local Url = require "socket.url"
--local Crypto = require "crypto"
local Client = {}
Client.__index = Client
--
-- Encodes the key-value pairs of a table according the application/x-www-form-urlencoded content type.
local function url_encode_arguments(arguments)
local body = {}
for k,v in pairs(arguments) do
body[#body + 1] = Url.escape(tostring(k)) .. "=" .. Url.escape(tostring(v))
end
return table.concat(body, "&")
end
---
-- Performs the actual http request, using LuaSocket or LuaSec (when using an https scheme)
-- @param url is the url to request
-- @param method is the http method (GET, POST, etc)
-- @param headers are the supplied http headers as a table
-- @param arguments is an optional table with whose keys and values will be encoded as "application/x-www-form-urlencoded"
-- or a string (or something that can be converted to a string). In that case, you must supply the Content-Type.
-- @param post_body is a string with all parameters (custom + oauth ones) encoded. This is used when the OAuth provider
-- does not support the 'Authorization' header.
local function PerformRequestHelper(self, url, method, headers, arguments, post_body)
-- Remove oauth_related arguments
if type(arguments) == "table" then
for k,v in pairs(arguments) do
if type(k) == "string" and k:match("^oauth_") then
arguments[k] = nil
end
end
if not next(arguments) then
arguments = nil
end
end
-- this method screams "refactor me!"
local response_body = {}
local request_constructor = {
url = url,
method = method,
headers = headers,
sink = Ltn12.sink.table(response_body)
}
if method == "PUT" then
if type(arguments) == "table" then
error("unsupported table argument for PUT")
else
local string_data = tostring(arguments)
if string_data == "nil" then
error("data must be something convertible to a string")
end
request_constructor.headers["Content-Length"] = tostring(#string_data)
request_constructor.source = Ltn12.source.string(string_data)
end
elseif method == "POST" then
if type(arguments) == "table" then
request_constructor.headers["Content-Type"] = "application/x-www-form-urlencoded"
if not self.m_supportsAuthHeader then
-- send all parameters (oauth + custom) in the body
request_constructor.headers["Content-Length"] = tostring(#post_body)
request_constructor.source = Ltn12.source.string(post_body)
else
-- encode the custom parameters and send them in the body
local source = url_encode_arguments(arguments)
request_constructor.headers["Content-Length"] = tostring(#source)
request_constructor.source = Ltn12.source.string(source)
end
elseif arguments then
if not self.m_supportsAuthHeader then
error("can't send POST body if the server does not support 'Authorization' header")
end
local string_data = tostring(arguments)
if string_data == "nil" then
error("data must be something convertible to a string")
end
request_constructor.headers["Content-Length"] = tostring(#string_data)
request_constructor.source = Ltn12.source.string(string_data)
else
request_constructor.headers["Content-Length"] = "0"
end
elseif method == "GET" or method == "HEAD" or method == "DELETE" then
if self.m_supportsAuthHeader then
if arguments then
request_constructor.url = url .. "?" .. url_encode_arguments(arguments)
end
else
-- send all parameters (oauth + custom) in the url
request_constructor.url = url .. "?" .. post_body
end
end
local ok, response_code, response_headers, response_status_line
if url:match("^https://") then
ok, response_code, response_headers, response_status_line = Https.request(request_constructor)
elseif url:match("^http://") then
ok, response_code, response_headers, response_status_line = Http.request(request_constructor)
else
error( ("unsupported scheme '%s'"):format( tostring(url:match("^([^:]+)")) ) )
end
if not ok then
return nil, response_code, response_headers, response_status_line, response_body
end
response_body = table.concat(response_body)
--[=[
for k,v in pairs(response_headers or {}) do
print( ("%s: %s"):format(k,v) )
end
print( ("response: %s"):format(response_body) )
--]=]
return true, response_code, response_headers, response_status_line, response_body
end
--
-- After retrieving an access token, this method is used to issue properly authenticated requests.
-- (see http://tools.ietf.org/html/rfc5849#section-3)
-- @param method is the http method (GET, POST, etc)
-- @param url is the url to request
-- @param arguments is an optional table whose keys and values will be encoded as "application/x-www-form-urlencoded"
-- (when doing a POST) or encoded and sent in the query string (when doing a GET).
-- @param headers is an optional table with http headers to be sent in the request
-- @return the http status code (a number), a table with the response headers, the status line and the response itself
--
function Client:PerformRequest(method, url, arguments, headers)
assert(type(method) == "string", "'method' must be a string")
method = method:upper()
--local headers, post_body, arguments = self:BuildRequest(method, url, arguments, headers)
arguments = arguments or {}
arguments.client_id = self.m_consumer_key
local ok, response_code, response_headers, response_status_line, response_body = PerformRequestHelper(self, url, method, headers, arguments, post_body)
return response_code, response_headers, response_status_line, response_body
end
function Client.new(consumer_key, consumer_secret, endpoints, params)
params = params or {}
local newInstance = {
m_consumer_key = consumer_key,
m_consumer_secret = consumer_secret,
m_endpoints = {},
m_signature_method = params.SignatureMethod or "HMAC-SHA1",
m_supportsAuthHeader = true,
m_oauth_token = params.OAuthToken,
m_oauth_token_secret = params.OAuthTokenSecret,
m_oauth_verifier = params.OAuthVerifier
}
if type(params.UseAuthHeaders) == "boolean" then
newInstance.m_supportsAuthHeader = params.UseAuthHeaders
end
for k,v in pairs(endpoints or {}) do
if type(v) == "table" then
newInstance.m_endpoints[k] = { url = v[1], method = string.upper(v.method) }
else
newInstance.m_endpoints[k] = { url = v, method = "POST" }
end
end
setmetatable(newInstance, Client)
return newInstance
end
return Client