mod_invites_bootstrap: API and behaviour improvements
This commit is contained in:
parent
dcc02d374b
commit
b2431438b2
|
@ -6,13 +6,10 @@ local secret = module:get_option_string("invites_bootstrap_secret");
|
|||
if not secret then return; end
|
||||
|
||||
local invites_bootstrap_store = module:open_store("invites_bootstrap");
|
||||
local bootstrap_records = invites_bootstrap_store:get() or {};
|
||||
|
||||
local index = module:get_option_number("invites_bootstrap_index");
|
||||
if #bootstrap_records > 0 and (index or -1) <= bootstrap_records[#bootstrap_records].index then
|
||||
module:log("debug", "Already bootstrapped for index %d", index or 0);
|
||||
return;
|
||||
end
|
||||
-- This should be a non-negative integer higher than any set for the
|
||||
-- previous bootstrap event (if any)
|
||||
local current_index = module:get_option_number("invites_bootstrap_index");
|
||||
|
||||
local invites = module:depends("invites");
|
||||
module:depends("http");
|
||||
|
@ -24,23 +21,39 @@ local function handle_request(event)
|
|||
return 403;
|
||||
end
|
||||
|
||||
local invite, err = invites.create_account(nil, {
|
||||
local bootstrap_records = invites_bootstrap_store:get() or {};
|
||||
if #bootstrap_records > 0 then
|
||||
local last_bootstrap = bootstrap_records[#bootstrap_records];
|
||||
if current_index == last_bootstrap.index then
|
||||
event.response.headers.Location = last_bootstrap.result;
|
||||
return 303;
|
||||
end
|
||||
end
|
||||
|
||||
-- Create invite
|
||||
local invite, invite_err = invites.create_account(nil, {
|
||||
roles = { ["prosody:admin"] = true };
|
||||
source = "api/token/bootstrap";
|
||||
source = "api/token/bootstrap-"..current_index;
|
||||
});
|
||||
if not invite then
|
||||
module:log("error", "Failed to create bootstrap invite! %s", err);
|
||||
module:log("error", "Failed to create bootstrap invite! %s", invite_err);
|
||||
return 500;
|
||||
end
|
||||
|
||||
-- Record this bootstrap event (to prevent replay)
|
||||
table.insert(bootstrap_records, {
|
||||
index = index;
|
||||
index = current_index;
|
||||
timestamp = os.time();
|
||||
result = invite.landing_page or invite.uri;
|
||||
});
|
||||
local record_ok, record_err = invites_bootstrap_store:set(bootstrap_records);
|
||||
if not record_ok then
|
||||
module:log("error", "Failed to store bootstrap record: %s", record_err);
|
||||
return 500;
|
||||
end
|
||||
|
||||
event.response.headers.Location = invite.landing_page or invite.uri;
|
||||
|
||||
return 201;
|
||||
return 303;
|
||||
end
|
||||
|
||||
module:provides("http", {
|
||||
|
|
Loading…
Reference in New Issue