annotate mod_http_xep227/mod_http_xep227.lua @ 4871:029ae3c29683

mod_http_xep227: Handle nil/errors opening archive stores This matches how mod_mam opens archive stores. nil can be returned when there is no data in the store yet (it doesn't exist).
author Matthew Wild <mwild1@gmail.com>
date Sun, 16 Jan 2022 15:01:20 +0000
parents d8a0a8dcdc0d
children bc54f651b5a4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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);
4871
029ae3c29683 mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents: 4870
diff changeset
123 local results_iter, results_err = source_archive:find(username);
029ae3c29683 mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents: 4870
diff changeset
124 if results_iter then
029ae3c29683 mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents: 4870
diff changeset
125 local count, errs = 0, 0;
029ae3c29683 mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents: 4870
diff changeset
126 for id, item, when, with in results_iter do
029ae3c29683 mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents: 4870
diff changeset
127 local ok, err = dest_archive:append(username, id, item, when, with);
029ae3c29683 mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents: 4870
diff changeset
128 if ok then
029ae3c29683 mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents: 4870
diff changeset
129 count = count + 1;
029ae3c29683 mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents: 4870
diff changeset
130 else
029ae3c29683 mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents: 4870
diff changeset
131 module:log("warn", "Error: %s", err);
029ae3c29683 mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents: 4870
diff changeset
132 errs = errs + 1;
029ae3c29683 mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents: 4870
diff changeset
133 end
029ae3c29683 mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents: 4870
diff changeset
134 if ( count + errs ) % 100 == 0 then
029ae3c29683 mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents: 4870
diff changeset
135 module:log("info", "%d items migrated, %d errors", count, errs);
029ae3c29683 mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents: 4870
diff changeset
136 end
4865
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 end
4871
029ae3c29683 mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents: 4870
diff changeset
138 elseif results_err then
029ae3c29683 mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents: 4870
diff changeset
139 module:log("warn", "Unable to read from '%s': %s", store_name, results_err);
029ae3c29683 mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents: 4870
diff changeset
140 return 500;
4865
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 end
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 end
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143
4868
57311c545013 mod_http_xep227: Fix validation of resulting export XML
Matthew Wild <mwild1@gmail.com>
parents: 4867
diff changeset
144 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
145
57311c545013 mod_http_xep227: Fix validation of resulting export XML
Matthew Wild <mwild1@gmail.com>
parents: 4867
diff changeset
146 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
147 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
148 return 204;
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 end
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 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
152 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
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
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 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
156 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
157 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
158 return false;
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 end
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 -- 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
161 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
162 end
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 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
165 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
166 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
167
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 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
169 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
170
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 -- Some sanity checks
4867
9d29467f4d5b mod_http_xep227: Fix luacheck warnings
Matthew Wild <mwild1@gmail.com>
parents: 4865
diff changeset
172 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
173 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
174 return 422;
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 end
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 -- 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
178 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
179 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
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 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
182
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183 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
184
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185 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
186
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 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
188
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189 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
190 -- 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
191 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
192
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193 -- 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
194 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
195 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
196 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
197 -- 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
198 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
199 return 500;
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200 end
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201 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
202 return 500;
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 end
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 end
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206 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
207 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
208 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
209 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
210 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
211 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
212 end
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
213 end
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
214 end
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
215
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
216 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
217 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
218 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
219 local dest_archive = dest_driver:open(store_name, "archive");
4871
029ae3c29683 mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents: 4870
diff changeset
220 local results_iter, results_err = source_archive:find(username);
029ae3c29683 mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents: 4870
diff changeset
221 if results_iter then
029ae3c29683 mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents: 4870
diff changeset
222 local count, errs = 0, 0;
029ae3c29683 mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents: 4870
diff changeset
223 for id, item, when, with in source_archive:find(username) do
029ae3c29683 mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents: 4870
diff changeset
224 local ok, err = dest_archive:append(username, id, item, when, with);
029ae3c29683 mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents: 4870
diff changeset
225 if ok then
029ae3c29683 mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents: 4870
diff changeset
226 count = count + 1;
029ae3c29683 mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents: 4870
diff changeset
227 else
029ae3c29683 mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents: 4870
diff changeset
228 module:log("warn", "Error: %s", err);
029ae3c29683 mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents: 4870
diff changeset
229 errs = errs + 1;
029ae3c29683 mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents: 4870
diff changeset
230 end
029ae3c29683 mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents: 4870
diff changeset
231 if ( count + errs ) % 100 == 0 then
029ae3c29683 mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents: 4870
diff changeset
232 module:log("info", "%d items migrated, %d errors", count, errs);
029ae3c29683 mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents: 4870
diff changeset
233 end
4865
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
234 end
4871
029ae3c29683 mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents: 4870
diff changeset
235 elseif results_err then
029ae3c29683 mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents: 4870
diff changeset
236 module:log("warn", "Unable to read from '%s': %s", store_name, results_err);
029ae3c29683 mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents: 4870
diff changeset
237 return 500;
4865
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 end
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
240
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
241 return 200;
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
242 end
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
243
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
244 ---
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
245
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
246 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
247 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
248 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
249 return false;
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
250 end
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
251
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
252 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
253 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
254 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
255 return false;
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 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
258 end
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
259 return nil;
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
260 end
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
261
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
262 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
263 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
264 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
265 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
266 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
267 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
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 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
270 return true;
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 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
274 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
275 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
276 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
277 return code;
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
278 end
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
279 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
280 end;
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
281 end
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
282 return routes;
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
283 end
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
284
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
285 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
286 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
287 ["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
288 ["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
289 };
bd0a1f917d98 mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
290 });