Mercurial > prosody-modules
comparison mod_http_admin_api/mod_http_admin_api.lua @ 4374:e707810a943e
mod_http_admin_api: Improve invite API and support password resets
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Sat, 23 Jan 2021 11:59:23 +0000 |
| parents | 3d01bc4547b2 |
| children | 03cf0d41b272 |
comparison
equal
deleted
inserted
replaced
| 4373:41ac0941b217 | 4374:e707810a943e |
|---|---|
| 134 end | 134 end |
| 135 if not invite then | 135 if not invite then |
| 136 return 500; | 136 return 500; |
| 137 end | 137 end |
| 138 | 138 |
| 139 event.response.headers["Content-Type"] = json_content_type; | |
| 140 return json.encode(token_info_to_invite_info(invite)); | |
| 141 end | |
| 142 | |
| 143 function create_invite_type(event, invite_type) | |
| 144 local options; | |
| 145 | |
| 146 local request = event.request; | |
| 147 if request.body and #request.body > 0 then | |
| 148 if request.headers.content_type ~= json_content_type then | |
| 149 module:log("warn", "Invalid content type"); | |
| 150 return 400; | |
| 151 end | |
| 152 options = json.decode(event.request.body); | |
| 153 if not options then | |
| 154 module:log("warn", "Invalid JSON"); | |
| 155 return 400; | |
| 156 end | |
| 157 else | |
| 158 options = {}; | |
| 159 end | |
| 160 | |
| 161 local invite; | |
| 162 if invite_type == "reset" then | |
| 163 if not options.username then | |
| 164 return 400; | |
| 165 end | |
| 166 invite = invites.create_account_reset(options.username, options.ttl); | |
| 167 elseif invite_type == "group" then | |
| 168 if not options.groups then | |
| 169 return 400; | |
| 170 end | |
| 171 invite = invites.create_group(options.groups, nil, options.ttl); | |
| 172 elseif invite_type == "account" then | |
| 173 invite = invites.create_account(options.username, nil, options.ttl); | |
| 174 else | |
| 175 return 400; | |
| 176 end | |
| 177 if not invite then | |
| 178 return 500; | |
| 179 end | |
| 139 event.response.headers["Content-Type"] = json_content_type; | 180 event.response.headers["Content-Type"] = json_content_type; |
| 140 return json.encode(token_info_to_invite_info(invite)); | 181 return json.encode(token_info_to_invite_info(invite)); |
| 141 end | 182 end |
| 142 | 183 |
| 143 function delete_invite(event, invite_id) --luacheck: ignore 212/event | 184 function delete_invite(event, invite_id) --luacheck: ignore 212/event |
| 513 | 554 |
| 514 module:provides("http", { | 555 module:provides("http", { |
| 515 route = check_auth { | 556 route = check_auth { |
| 516 ["GET /invites"] = list_invites; | 557 ["GET /invites"] = list_invites; |
| 517 ["GET /invites/*"] = get_invite_by_id; | 558 ["GET /invites/*"] = get_invite_by_id; |
| 518 ["POST /invites"] = create_invite; | 559 ["POST /invites"] = create_invite; -- Deprecated |
| 560 ["POST /invites/*"] = create_invite_type; | |
| 519 ["DELETE /invites/*"] = delete_invite; | 561 ["DELETE /invites/*"] = delete_invite; |
| 520 | 562 |
| 521 ["GET /users"] = list_users; | 563 ["GET /users"] = list_users; |
| 522 ["GET /users/*"] = get_user_by_name; | 564 ["GET /users/*"] = get_user_by_name; |
| 523 ["DELETE /users/*"] = delete_user; | 565 ["DELETE /users/*"] = delete_user; |
