Mercurial > prosody-modules
comparison mod_adhoc_oauth2_client/mod_adhoc_oauth2_client.lua @ 4261:608be9a66876
mod_adhoc_oauth2_client: Allow creating OAuth2 clients via ad-hoc
To go with mod_http_oauth2
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 22 Nov 2020 00:50:45 +0100 |
parents | |
children | 6d7fb22c0440 |
comparison
equal
deleted
inserted
replaced
4260:c539334dd01a | 4261:608be9a66876 |
---|---|
1 local adhoc = require "util.adhoc"; | |
2 local dataforms = require "util.dataforms"; | |
3 local errors = require "util.error"; | |
4 local id = require "util.id"; | |
5 local jid = require "util.jid"; | |
6 | |
7 local clients = module:open_store("oauth2_clients", "map"); | |
8 | |
9 local new_client = dataforms.new({ | |
10 title = "Create OAuth2 client"; | |
11 {var = "FORM_TYPE"; type = "hidden"; value = "urn:uuid:ff0d55ed-2187-4ee0-820a-ab633a911c14#create"}; | |
12 {name = "name"; type = "text-single"; label = "Client name"; required = true}; | |
13 {name = "description"; type = "text-multi"; label = "Description"}; | |
14 {name = "info_url"; type = "text-single"; label = "Informative URL"; desc = "Link to information about your client"}; | |
15 {name = "redirect_uri"; type = "text-single"; label = "Redirection URI"; desc = "Where to redirect the user after authorizing."; required = true}; | |
16 }) | |
17 | |
18 local client_created = dataforms.new({ | |
19 title = "New OAuth2 client created"; | |
20 instructions = "Save these details, they will not be shown again"; | |
21 {var = "FORM_TYPE"; type = "hidden"; value = "urn:uuid:ff0d55ed-2187-4ee0-820a-ab633a911c14#created"}; | |
22 {name = "client_id"; type = "text-single"; label = "Client ID"}; | |
23 {name = "client_secret"; type = "text-single"; label = "Client secret"}; | |
24 }) | |
25 | |
26 local function create_client(client, formerr, data) | |
27 if formerr then | |
28 local errmsg = {"Error in form:"}; | |
29 for field, err in pairs(formerr) do table.insert(errmsg, field .. ": " .. err); end | |
30 return {status = "error"; error = {message = table.concat(errmsg, "\n")}}; | |
31 end | |
32 | |
33 local creator = jid.split(data.from); | |
34 local client_id = id.short(); | |
35 | |
36 client.client_id = jid.join(creator, module.host, client_id); | |
37 client.client_secret = id.long(); | |
38 | |
39 local ok, err = errors.coerce(clients:set(creator, client_id, client)); | |
40 module:log("info", "OAuth2 client %q created by %s", client_id, data.from); | |
41 if not ok then return {status = "error"; error = {message = err}}; end | |
42 | |
43 return {status = "completed"; result = {layout = client_created; values = client}}; | |
44 end | |
45 | |
46 local handler = adhoc.new_simple_form(new_client, create_client); | |
47 | |
48 module:provides("adhoc", module:require "adhoc".new(new_client.title, new_client[1].value, handler, "local_user")); |