Mercurial > prosody-modules
annotate mod_http_xep227/mod_http_xep227.lua @ 4870:d8a0a8dcdc0d
mod_http_xep227: Don't require admin privileges to use import/export
These APIs always operate on the current user account only. In the future we
may want to have a more specific permission check though.
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sun, 16 Jan 2022 13:44:26 +0000 |
parents | c3bf568e3977 |
children | 029ae3c29683 |
rev | line source |
---|---|
4865
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 local it = require "util.iterators"; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 local http = require "util.http"; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 local sm = require "core.storagemanager"; |
4869
c3bf568e3977
mod_http_xep227: Initialize XEP-0227 XML
Matthew Wild <mwild1@gmail.com>
parents:
4868
diff
changeset
|
4 local st = require "util.stanza"; |
4865
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 local xml = require "util.xml"; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 local tokens = module:depends("tokenauth"); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 module:depends("storage_xep0227"); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 local archive_store_name = module:get_option("archive_store", "archive"); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 local known_stores = { |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 accounts = "keyval"; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 roster = "keyval"; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 private = "keyval"; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 pep = "keyval"; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 vcard = "keyval"; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 [archive_store_name] = "archive"; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 pep_data = "archive"; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 }; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 local function new_user_xml(username, host) |
4869
c3bf568e3977
mod_http_xep227: Initialize XEP-0227 XML
Matthew Wild <mwild1@gmail.com>
parents:
4868
diff
changeset
|
24 local user_xml = st.stanza("server-data", {xmlns='urn:xmpp:pie:0'}) |
c3bf568e3977
mod_http_xep227: Initialize XEP-0227 XML
Matthew Wild <mwild1@gmail.com>
parents:
4868
diff
changeset
|
25 :tag("host", { jid = host }) |
c3bf568e3977
mod_http_xep227: Initialize XEP-0227 XML
Matthew Wild <mwild1@gmail.com>
parents:
4868
diff
changeset
|
26 :tag("user", { name = username }):reset(); |
4865
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 return { |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 set_user_xml = function (_, store_username, store_host, new_xml) |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 if username ~= store_username or store_host ~= host then |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 return nil; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 user_xml = new_xml; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 return true; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 end; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 get_user_xml = function (_, store_username, store_host) |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 if username ~= store_username or store_host ~= host then |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 return nil; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 return user_xml; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 }; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 local function get_selected_stores(query_params) |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 local selected_kv_stores, selected_archive_stores, export_pep_data = {}, {}, false; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 if query_params.stores then |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 for store_name in query_params.stores:gmatch("[^,]+") do |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 local store_type = known_stores[store_name]; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 if store_type == "keyval" then |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 table.insert(selected_kv_stores, store_name); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 elseif store_type == "archive" then |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 if store_name == "pep_data" then |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 export_pep_data = true; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 else |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 table.insert(selected_archive_stores, store_name); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 else |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 module:log("warn", "Unknown store: %s", store_name); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 return 400; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 return { |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 keyval = selected_kv_stores; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 archive = selected_archive_stores; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 export_pep_data = export_pep_data; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 }; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 local function get_config_driver(store_name, host) |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 -- Fiddling to handle the 'pep_data' storage config override |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 if store_name:find("pep_", 1, true) == 1 then |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 store_name = "pep_data"; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 -- Return driver |
4867
9d29467f4d5b
mod_http_xep227: Fix luacheck warnings
Matthew Wild <mwild1@gmail.com>
parents:
4865
diff
changeset
|
78 return sm.get_driver(host, store_name); |
4865
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 local function handle_export_227(event) |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 local session = assert(event.session, "No session found"); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 local xep227_driver = sm.load_driver(session.host, "xep0227"); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 local username = session.username; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 local user_xml = new_user_xml(session.username, session.host); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 local query_params = http.formdecode(event.request.url.query or ""); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 local selected_stores = get_selected_stores(query_params); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 for store_name in it.values(selected_stores.keyval) do |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 -- Open the source store that contains the data |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 local store = sm.open(session.host, store_name); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 -- Read the current data |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 local data, err = store:get(username); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 if data ~= nil or not err then |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 -- Initialize the destination store (XEP-0227 backed) |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
100 local target_store = xep227_driver:open_xep0227(store_name, nil, user_xml); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 -- Transform the data and update user_xml (via the _set_user_xml callback) |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 if not target_store:set(username, data == nil and {} or data) then |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 return 500; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 elseif err then |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 return 500; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 if selected_stores.export_pep_data then |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
111 local pep_node_list = sm.open(session.host, "pep"):get(session.username); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 if pep_node_list then |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 for node_name in it.keys(pep_node_list) do |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
114 table.insert(selected_stores.archive, "pep_"..node_name); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
117 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
118 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
119 for store_name in it.values(selected_stores.archive) do |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 local source_driver = get_config_driver(store_name, session.host); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 local source_archive = source_driver:open(store_name, "archive"); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 local dest_archive = xep227_driver:open_xep0227(store_name, "archive", user_xml); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 local count, errs = 0, 0; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
124 for id, item, when, with in source_archive:find(username) do |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
125 local ok, err = dest_archive:append(username, id, item, when, with); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
126 if ok then |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
127 count = count + 1; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
128 else |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 module:log("warn", "Error: %s", err); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
130 errs = errs + 1; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
131 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
132 if ( count + errs ) % 100 == 0 then |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
133 module:log("info", "%d items migrated, %d errors", count, errs); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
134 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
135 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 |
4868
57311c545013
mod_http_xep227: Fix validation of resulting export XML
Matthew Wild <mwild1@gmail.com>
parents:
4867
diff
changeset
|
138 local xml_data = user_xml:get_user_xml(username, session.host); |
57311c545013
mod_http_xep227: Fix validation of resulting export XML
Matthew Wild <mwild1@gmail.com>
parents:
4867
diff
changeset
|
139 |
57311c545013
mod_http_xep227: Fix validation of resulting export XML
Matthew Wild <mwild1@gmail.com>
parents:
4867
diff
changeset
|
140 if not xml_data or not xml_data:find("host/user") then |
57311c545013
mod_http_xep227: Fix validation of resulting export XML
Matthew Wild <mwild1@gmail.com>
parents:
4867
diff
changeset
|
141 module:log("warn", "No data to export: %s", tostring(xml_data)); |
4865
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 return 204; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
143 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
144 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
145 event.response.headers["Content-Type"] = "application/xml"; |
4868
57311c545013
mod_http_xep227: Fix validation of resulting export XML
Matthew Wild <mwild1@gmail.com>
parents:
4867
diff
changeset
|
146 return [[<?xml version="1.0" encoding="utf-8" ?>]]..tostring(xml_data); |
4865
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
147 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
148 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 local function is_looking_like_xep227(xml_data) |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
150 if not xml_data or xml_data.name ~= "server-data" |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
151 or xml_data.attr.xmlns ~= "urn:xmpp:pie:0" then |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
152 return false; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
153 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
154 -- Looks like 227, but check it has at least one host + user element |
4867
9d29467f4d5b
mod_http_xep227: Fix luacheck warnings
Matthew Wild <mwild1@gmail.com>
parents:
4865
diff
changeset
|
155 return not not xml_data:find("host/user"); |
4865
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
156 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
158 local function handle_import_227(event) |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
159 local session = assert(event.session, "No session found"); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
160 local username = session.username; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
161 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
162 local input_xml_raw = event.request.body; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
163 local input_xml_parsed = xml.parse(input_xml_raw); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
164 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
165 -- Some sanity checks |
4867
9d29467f4d5b
mod_http_xep227: Fix luacheck warnings
Matthew Wild <mwild1@gmail.com>
parents:
4865
diff
changeset
|
166 if not input_xml_parsed or not is_looking_like_xep227(input_xml_parsed) then |
4865
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
167 module:log("warn", "No data to import"); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
168 return 422; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
169 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
170 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
171 -- Set the host and username of the import to the new account's user/host |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
172 input_xml_parsed:find("host").attr.jid = session.host; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
173 input_xml_parsed:find("host/user").attr.name = username; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
174 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
175 local user_xml = new_user_xml(session.username, session.host); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
176 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
177 user_xml:set_user_xml(username, session.host, input_xml_parsed); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
178 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
179 local xep227_driver = sm.load_driver(session.host, "xep0227"); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
180 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
181 local selected_stores = get_selected_stores(event.request.url.query); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
182 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
183 for _, store_name in ipairs(selected_stores.keyval) do |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
184 -- Initialize the destination store (XEP-0227 backed) |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
185 local store = xep227_driver:open_xep0227(store_name, nil, user_xml); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
186 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
187 -- Read the current data |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
188 local data, err = store:get(username); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
189 if data ~= nil or not err then |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
190 local target_store = sm.open(session.host, store_name); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
191 -- Transform the data and update user_xml (via the _set_user_xml callback) |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
192 if not target_store:set(username, data == nil and {} or data) then |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
193 return 500; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
194 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
195 elseif err then |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
196 return 500; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
197 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
198 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
199 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
200 if selected_stores.export_pep_data then |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
201 local pep_store = xep227_driver:open_xep0277("pep", nil, user_xml); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
202 local pep_node_list = pep_store:get(session.username); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
203 if pep_node_list then |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
204 for node_name in it.keys(pep_node_list) do |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
205 table.insert(selected_stores.archive, "pep_"..node_name); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
206 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
207 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
208 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
209 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
210 for store_name in it.values(selected_stores.archive) do |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
211 local source_archive = xep227_driver:open_xep0227(store_name, "archive", user_xml); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
212 local dest_driver = get_config_driver(store_name, session.host); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
213 local dest_archive = dest_driver:open(store_name, "archive"); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
214 local count, errs = 0, 0; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
215 for id, item, when, with in source_archive:find(username) do |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
216 local ok, err = dest_archive:append(username, id, item, when, with); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
217 if ok then |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
218 count = count + 1; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
219 else |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
220 module:log("warn", "Error: %s", err); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
221 errs = errs + 1; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
222 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
223 if ( count + errs ) % 100 == 0 then |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
224 module:log("info", "%d items migrated, %d errors", count, errs); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
225 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
226 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
227 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
228 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
229 return 200; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
230 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
231 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
232 --- |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
233 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
234 local function check_credentials(request) |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
235 local auth_type, auth_data = string.match(request.headers.authorization or "", "^(%S+)%s(.+)$"); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
236 if not (auth_type and auth_data) then |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
237 return false; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
238 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
239 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
240 if auth_type == "Bearer" then |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
241 local token_info = tokens.get_token_info(auth_data); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
242 if not token_info or not token_info.session then |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
243 return false; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
244 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
245 return token_info.session; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
246 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
247 return nil; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
248 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
249 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
250 local function check_auth(routes) |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
251 local function check_request_auth(event) |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
252 local session = check_credentials(event.request); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
253 if not session then |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
254 event.response.headers.authorization = ("Bearer realm=%q"):format(module.host.."/"..module.name); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
255 return false, 401; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
256 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
257 event.session = session; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
258 return true; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
259 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
260 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
261 for route, handler in pairs(routes) do |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
262 routes[route] = function (event, ...) |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
263 local permit, code = check_request_auth(event); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
264 if not permit then |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
265 return code; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
266 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
267 return handler(event, ...); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
268 end; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
269 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
270 return routes; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
271 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
272 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
273 module:provides("http", { |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
274 route = check_auth { |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
275 ["GET /export"] = handle_export_227; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
276 ["PUT /import"] = handle_import_227; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
277 }; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
278 }); |