-
Notifications
You must be signed in to change notification settings - Fork 0
/
powder.lua
248 lines (239 loc) · 6.95 KB
/
powder.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
local cqueues = require("cqueues")
local secret_config = require("secret_config")
local http_cookie = require("http.cookie")
local http_request = require("http.request")
local lunajson = require("lunajson")
local subst = require("util").subst
local basexx = require("basexx")
local config = require("config")
local function valid_user(tname)
if #tname == 0 or #tname > 32 or tname:find("[^a-zA-Z0-9_-]") then
return false
end
return true
end
local function fetch_user(tname)
if not valid_user(tname) then
return false
end
local req, err = http_request.new_from_uri(subst("$/User.json?Name=$", secret_config.backend_base, tname))
if not req then
return nil, err
end
req.version = 1.1
req.cookie_store = http_cookie.new_store()
local deadline = cqueues.monotime() + config.powder.fetch_user_timeout
local headers, stream = req:go(deadline - cqueues.monotime())
if not headers then
return nil, stream
end
local code = headers:get(":status")
if code ~= "200" then
return nil, "status code " .. code
end
local body, err = stream:get_body_as_string(deadline - cqueues.monotime())
if not body then
return nil, err
end
if body == "Error: 404" then
return false
end
local ok, json = pcall(lunajson.decode, body)
if not ok then
return nil, json
end
if not (type(json) == "table" and
type(json.User) == "table" and
type(json.User.ID) == "number" and
type(json.User.Username) == "string") then
return nil, "invalid response from backend"
end
return json.User
end
local function valid_save(id)
if #id == 0 or #id > 9 or id:find("[^%d]") then
return false
end
return true
end
local function fetch_save(id)
if not valid_save(id) then
return false
end
local req, err = http_request.new_from_uri(subst("$/Browse/View.json?ID=$", secret_config.backend_base, id))
if not req then
return nil, err
end
req.version = 1.1
req.cookie_store = http_cookie.new_store()
local deadline = cqueues.monotime() + config.powder.fetch_save_timeout
local headers, stream = req:go(deadline - cqueues.monotime())
if not headers then
return nil, stream
end
local code = headers:get(":status")
if code ~= "200" then
return nil, "status code " .. code
end
local body, err = stream:get_body_as_string(deadline - cqueues.monotime())
if not body then
return nil, err
end
local ok, json = pcall(lunajson.decode, body)
if not ok then
return nil, json
end
if type(json) == "table" and json.Username == "FourOhFour" then
return false
end
if not (type(json) == "table" and
type(json.Name) == "string" and
type(json.Description) == "string" and
type(json.Username) == "string" and
type(json.Score) == "number" and
type(json.Date) == "number") then
return nil, "invalid response from backend"
end
return json
end
local function get_motd_regions(motd)
local regions = {}
local fragments = ""
local it = 1
while it <= #motd do
local function find(it, ch)
while it <= #motd do
if motd:sub(it, it) == ch then
break
end
it = it + 1
end
return it
end
local begin_region_it = find(it, "{")
local begin_data_it = find(begin_region_it, ":")
local begin_text_it = find(begin_data_it, "|")
local end_region_it = find(begin_text_it, "}")
if end_region_it > #motd then
break
end
local action = motd:sub(begin_region_it + 1, begin_data_it - 1)
local data = motd:sub(begin_data_it + 1, begin_text_it - 1)
local text = motd:sub(begin_text_it + 1, end_region_it - 1)
fragments = fragments .. motd:sub(it, begin_region_it - 1)
local good = false
if action == "a" and #data > 0 and #text > 0 then
local region = {}
local old_size = #fragments
fragments = fragments .. text
region.size = #fragments - old_size
region.pos = old_size + 1
region.action = "link"
region.url = data
table.insert(regions, region)
good = true
end
if not good then
fragments = fragments .. motd:sub(begin_region_it, end_region_it)
end
it = end_region_it + 1
end
fragments = fragments .. motd:sub(it)
return fragments, regions
end
local function fetch_motd()
local req, err = http_request.new_from_uri(subst("$/Startup.json", secret_config.backend_base))
if not req then
return nil, err
end
req.version = 1.1
req.cookie_store = http_cookie.new_store()
local deadline = cqueues.monotime() + config.powder.fetch_motd_timeout
local headers, stream = req:go(deadline - cqueues.monotime())
if not headers then
return nil, stream
end
local code = headers:get(":status")
if code ~= "200" then
return nil, "status code " .. code
end
local body, err = stream:get_body_as_string(deadline - cqueues.monotime())
if not body then
return nil, err
end
local ok, json = pcall(lunajson.decode, body)
if not ok then
return nil, json
end
if not (type(json) == "table" and
type(json.MessageOfTheDay) == "string") then
return nil, "invalid response from backend"
end
local text, regions = get_motd_regions(json.MessageOfTheDay)
local function remove(pat)
text = text:gsub(pat:gsub("%.", utf8.charpattern), "")
end
remove("\b.")
remove("\14")
remove("\15...")
return text, regions
end
local function token_payload(token)
local payload = token:match("^[^%.]+%.([^%.]+)%.[^%.]+$")
if not payload then
return nil, "no payload"
end
local unb64 = basexx.from_url64(payload)
if not unb64 then
return nil, "bad base64"
end
local ok, json = pcall(lunajson.decode, unb64)
if not ok then
return nil, "bad json: " .. json
end
if type(json) ~= "table" then
return nil, "bad payload document"
end
if type(json.sub) ~= "string" or json.sub:find("[^0-9]") then
return nil, "bad payload subject"
end
if json.aud ~= secret_config.backend_audience then
return nil, "bad payload audience"
end
return json
end
local function external_auth(powder_token)
local req, err = http_request.new_from_uri(subst("$/ExternalAuth.api?Action=Check&MaxAge=$&Token=$", secret_config.backend_base, config.powder.powder_token_max_age, powder_token))
if not req then
return nil, "failed to create request: " .. err
end
req.version = 1.1
req.cookie_store = http_cookie.new_store()
local deadline = cqueues.monotime() + config.powder.externalauth_timeout
local headers, stream = req:go(deadline - cqueues.monotime())
if not headers then
return nil, "failed to submit request: " .. stream
end
local code = headers:get(":status")
if code ~= "200" then
return nil, "status code " .. code
end
local body, err = stream:get_body_as_string(deadline - cqueues.monotime())
if not body then
return nil, "failed to get response body: " .. err
end
local ok, json = pcall(lunajson.decode, body)
if not ok or type(json) ~= "table" then
return nil, "bad json: " .. json .. " with body " .. body
end
return json.Status
end
return {
valid_user = valid_user,
fetch_user = fetch_user,
valid_save = valid_save,
fetch_save = fetch_save,
fetch_motd = fetch_motd,
token_payload = token_payload,
external_auth = external_auth,
}