author | Matthew Wild <mwild1@gmail.com> |
Mon, 22 Aug 2022 13:28:01 +0100 | |
changeset 5011 | 34fb3d239ac1 |
parent 5003 | 65cdbbf9703a |
permissions | -rw-r--r-- |
4869
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"; |
4873
c3bf568e3977
mod_http_xep227: Initialize XEP-0227 XML
Matthew Wild <mwild1@gmail.com>
parents:
4872
diff
changeset
|
4 |
local st = require "util.stanza"; |
4869
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 |
|
4880
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
7 |
local jid_join = require "util.jid".join; |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
8 |
|
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
9 |
local mod_pep = module:depends("pep"); |
4869
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 |
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
|
11 |
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
|
12 |
|
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 |
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
|
14 |
|
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 |
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
|
16 |
accounts = "keyval"; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 |
roster = "keyval"; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 |
private = "keyval"; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 |
pep = "keyval"; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 |
vcard = "keyval"; |
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 |
[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
|
23 |
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
|
24 |
}; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 |
|
4880
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
26 |
local xmlns_pubsub = "http://jabber.org/protocol/pubsub"; |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
27 |
|
4869
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 |
local function new_user_xml(username, host) |
4873
c3bf568e3977
mod_http_xep227: Initialize XEP-0227 XML
Matthew Wild <mwild1@gmail.com>
parents:
4872
diff
changeset
|
29 |
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:
4872
diff
changeset
|
30 |
:tag("host", { jid = host }) |
c3bf568e3977
mod_http_xep227: Initialize XEP-0227 XML
Matthew Wild <mwild1@gmail.com>
parents:
4872
diff
changeset
|
31 |
:tag("user", { name = username }):reset(); |
4869
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 |
|
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 |
return { |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 |
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
|
35 |
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
|
36 |
return nil; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 |
end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 |
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
|
39 |
return true; |
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 |
|
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 |
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
|
43 |
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
|
44 |
return nil; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 |
end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 |
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
|
47 |
end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 |
}; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 |
end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 |
|
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 |
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
|
52 |
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
|
53 |
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
|
54 |
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
|
55 |
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
|
56 |
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
|
57 |
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
|
58 |
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
|
59 |
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
|
60 |
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
|
61 |
else |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 |
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
|
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 |
else |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 |
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
|
66 |
return 400; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 |
end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 |
end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 |
end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 |
return { |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 |
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
|
72 |
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
|
73 |
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
|
74 |
}; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 |
end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 |
|
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 |
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
|
78 |
-- 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
|
79 |
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
|
80 |
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
|
81 |
end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 |
-- Return driver |
4871
9d29467f4d5b
mod_http_xep227: Fix luacheck warnings
Matthew Wild <mwild1@gmail.com>
parents:
4869
diff
changeset
|
83 |
return sm.get_driver(host, store_name); |
4869
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 |
end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 |
|
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 |
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
|
87 |
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
|
88 |
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
|
89 |
|
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 |
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
|
91 |
|
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 |
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
|
93 |
|
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 |
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
|
95 |
|
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 |
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
|
97 |
|
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 |
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
|
99 |
-- 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
|
100 |
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
|
101 |
-- 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
|
102 |
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
|
103 |
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
|
104 |
-- 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
|
105 |
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
|
106 |
-- 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
|
107 |
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
|
108 |
return 500; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 |
end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 |
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
|
111 |
return 500; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 |
end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 |
end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
114 |
|
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 |
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
|
116 |
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
|
117 |
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
|
118 |
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
|
119 |
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
|
120 |
end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 |
end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 |
end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 |
|
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
124 |
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
|
125 |
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
|
126 |
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
|
127 |
local dest_archive = xep227_driver:open_xep0227(store_name, "archive", user_xml); |
4875
029ae3c29683
mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents:
4874
diff
changeset
|
128 |
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:
4874
diff
changeset
|
129 |
if results_iter then |
029ae3c29683
mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents:
4874
diff
changeset
|
130 |
local count, errs = 0, 0; |
029ae3c29683
mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents:
4874
diff
changeset
|
131 |
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:
4874
diff
changeset
|
132 |
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:
4874
diff
changeset
|
133 |
if ok then |
029ae3c29683
mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents:
4874
diff
changeset
|
134 |
count = count + 1; |
029ae3c29683
mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents:
4874
diff
changeset
|
135 |
else |
029ae3c29683
mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents:
4874
diff
changeset
|
136 |
module:log("warn", "Error: %s", err); |
029ae3c29683
mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents:
4874
diff
changeset
|
137 |
errs = errs + 1; |
029ae3c29683
mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents:
4874
diff
changeset
|
138 |
end |
029ae3c29683
mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents:
4874
diff
changeset
|
139 |
if ( count + errs ) % 100 == 0 then |
029ae3c29683
mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents:
4874
diff
changeset
|
140 |
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:
4874
diff
changeset
|
141 |
end |
4869
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 |
end |
4875
029ae3c29683
mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents:
4874
diff
changeset
|
143 |
elseif results_err then |
029ae3c29683
mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents:
4874
diff
changeset
|
144 |
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:
4874
diff
changeset
|
145 |
return 500; |
4869
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
146 |
end |
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 |
|
4872
57311c545013
mod_http_xep227: Fix validation of resulting export XML
Matthew Wild <mwild1@gmail.com>
parents:
4871
diff
changeset
|
149 |
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:
4871
diff
changeset
|
150 |
|
57311c545013
mod_http_xep227: Fix validation of resulting export XML
Matthew Wild <mwild1@gmail.com>
parents:
4871
diff
changeset
|
151 |
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:
4871
diff
changeset
|
152 |
module:log("warn", "No data to export: %s", tostring(xml_data)); |
4869
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
153 |
return 204; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
154 |
end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
155 |
|
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
156 |
event.response.headers["Content-Type"] = "application/xml"; |
4872
57311c545013
mod_http_xep227: Fix validation of resulting export XML
Matthew Wild <mwild1@gmail.com>
parents:
4871
diff
changeset
|
157 |
return [[<?xml version="1.0" encoding="utf-8" ?>]]..tostring(xml_data); |
4869
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
158 |
end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
159 |
|
4880
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
160 |
local function generic_keyval_importer(username, host, store_name, source_store) |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
161 |
-- Read the current data |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
162 |
local data, err = source_store:get(username); |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
163 |
if data ~= nil or not err then |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
164 |
local target_store = sm.open(host, store_name); |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
165 |
-- Transform the data and update user_xml (via the _set_user_xml callback) |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
166 |
if not target_store:set(username, data == nil and {} or data) then |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
167 |
return 500; |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
168 |
end |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
169 |
module:log("debug", "Imported data for '%s' store", store_name); |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
170 |
elseif err then |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
171 |
return nil, err; |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
172 |
else |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
173 |
module:log("debug", "No data for store '%s'", store_name); |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
174 |
end |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
175 |
return true; |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
176 |
end |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
177 |
|
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
178 |
local function generic_archive_importer(username, host, store_name, source_archive) |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
179 |
local dest_driver = get_config_driver(store_name, host); |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
180 |
local dest_archive = dest_driver:open(store_name, "archive"); |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
181 |
local results_iter, results_err = source_archive:find(username); |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
182 |
if results_iter then |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
183 |
local count, errs = 0, 0; |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
184 |
for id, item, when, with in source_archive:find(username) do |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
185 |
local ok, err = dest_archive:append(username, id, item, when, with); |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
186 |
if ok then |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
187 |
count = count + 1; |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
188 |
else |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
189 |
module:log("warn", "Error: %s", err); |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
190 |
errs = errs + 1; |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
191 |
end |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
192 |
if ( count + errs ) % 100 == 0 then |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
193 |
module:log("info", "%d items migrated, %d errors", count, errs); |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
194 |
end |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
195 |
end |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
196 |
elseif results_err then |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
197 |
module:log("warn", "Unable to read from '%s': %s", store_name, results_err); |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
198 |
return nil, "error reading from source archive"; |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
199 |
end |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
200 |
return true; |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
201 |
end |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
202 |
|
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
203 |
local special_keyval_importers = {}; |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
204 |
|
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
205 |
function special_keyval_importers.pep(username, host, store_name, store) --luacheck: ignore 212/store_name |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
206 |
local user_jid = jid_join(username, host); |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
207 |
local pep_service = mod_pep.get_pep_service(username); |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
208 |
local pep_nodes, store_err = store:get(username); |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
209 |
if not pep_nodes and store_err then |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
210 |
return nil, store_err; |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
211 |
end |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
212 |
|
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
213 |
local all_ok = true; |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
214 |
for node_name, node_config in pairs(pep_nodes) do |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
215 |
local ok, ret = pep_service:get_node_config(node_name, user_jid); |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
216 |
if not ok and ret == "item-not-found" then |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
217 |
-- Create node according to imported data |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
218 |
if node_config == true then node_config = {}; end |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
219 |
local create_ok, create_err = pep_service:create(node_name, user_jid, node_config.config); |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
220 |
if not create_ok then |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
221 |
module:log("warn", "Failed to create PEP node: %s", create_err); |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
222 |
all_ok = false; |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
223 |
end |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
224 |
end |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
225 |
end |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
226 |
|
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
227 |
return all_ok; |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
228 |
end |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
229 |
|
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
230 |
local special_archive_importers = setmetatable({}, { |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
231 |
__index = function (t, k) |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
232 |
if k:match("^pep_") then |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
233 |
return t.pep_data; |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
234 |
end |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
235 |
end; |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
236 |
}); |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
237 |
|
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
238 |
function special_archive_importers.pep_data(username, host, store_name, source_archive) |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
239 |
local user_jid = jid_join(username, host); |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
240 |
local pep_service = mod_pep.get_pep_service(username); |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
241 |
|
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
242 |
local node_name = store_name:match("^pep_(.+)$"); |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
243 |
if not node_name then |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
244 |
return nil, "invalid store name"; |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
245 |
end |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
246 |
|
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
247 |
local results_iter, results_err = source_archive:find(username); |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
248 |
if results_iter then |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
249 |
local count, errs = 0, 0; |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
250 |
for id, item in source_archive:find(username) do |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
251 |
local wrapped_item = st.stanza("item", { xmlns = xmlns_pubsub, id = id }) |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
252 |
:add_child(item); |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
253 |
local ok, err = pep_service:publish(node_name, user_jid, id, wrapped_item); |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
254 |
if not ok then |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
255 |
module:log("warn", "Failed to publish PEP item to '%s': %s", node_name, err, tostring(wrapped_item)); |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
256 |
end |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
257 |
end |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
258 |
module:log("debug", "Imported %d PEP items (%d errors)", count, errs); |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
259 |
elseif results_err then |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
260 |
return nil, "store access error"; |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
261 |
end |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
262 |
return true; |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
263 |
end |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
264 |
|
4869
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
265 |
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
|
266 |
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
|
267 |
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
|
268 |
return false; |
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 |
-- Looks like 227, but check it has at least one host + user element |
4871
9d29467f4d5b
mod_http_xep227: Fix luacheck warnings
Matthew Wild <mwild1@gmail.com>
parents:
4869
diff
changeset
|
271 |
return not not xml_data:find("host/user"); |
4869
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
272 |
end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
273 |
|
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
274 |
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
|
275 |
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
|
276 |
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
|
277 |
|
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
278 |
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
|
279 |
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
|
280 |
|
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
281 |
-- Some sanity checks |
4871
9d29467f4d5b
mod_http_xep227: Fix luacheck warnings
Matthew Wild <mwild1@gmail.com>
parents:
4869
diff
changeset
|
282 |
if not input_xml_parsed or not is_looking_like_xep227(input_xml_parsed) then |
4869
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
283 |
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
|
284 |
return 422; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
285 |
end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
286 |
|
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
287 |
-- 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
|
288 |
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
|
289 |
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
|
290 |
|
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
291 |
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
|
292 |
|
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
293 |
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
|
294 |
|
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
295 |
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
|
296 |
|
4876
bc54f651b5a4
mod_http_xep227: Correctly read selected stores from URL query part
Matthew Wild <mwild1@gmail.com>
parents:
4875
diff
changeset
|
297 |
local query_params = http.formdecode(event.request.url.query or ""); |
bc54f651b5a4
mod_http_xep227: Correctly read selected stores from URL query part
Matthew Wild <mwild1@gmail.com>
parents:
4875
diff
changeset
|
298 |
local selected_stores = get_selected_stores(query_params); |
4869
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
299 |
|
4880
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
300 |
module:log("debug", "Importing %d keyval stores (%s)...", #selected_stores.keyval, table.concat(selected_stores.keyval, ", ")); |
4869
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
301 |
for _, store_name in ipairs(selected_stores.keyval) do |
4880
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
302 |
module:log("debug", "Importing keyval store %s...", store_name); |
4869
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
303 |
-- Initialize the destination store (XEP-0227 backed) |
4880
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
304 |
local source_store = xep227_driver:open_xep0227(store_name, nil, user_xml); |
4869
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
305 |
|
4880
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
306 |
local importer = special_keyval_importers[store_name] or generic_keyval_importer; |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
307 |
local ok, err = importer(username, session.host, store_name, source_store); |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
308 |
if not ok then |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
309 |
module:log("warn", "Importer for keyval store '%s' encountered error: %s", store_name, err or "<no error returned>"); |
4869
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
310 |
return 500; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
311 |
end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
312 |
end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
313 |
|
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
314 |
if selected_stores.export_pep_data then |
4877
541b2cf68e93
mod_http_xep227: Fix typo in method name
Matthew Wild <mwild1@gmail.com>
parents:
4876
diff
changeset
|
315 |
local pep_store = xep227_driver:open_xep0227("pep", nil, user_xml); |
4869
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
316 |
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
|
317 |
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
|
318 |
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
|
319 |
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
|
320 |
end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
321 |
end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
322 |
end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
323 |
|
4880
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
324 |
module:log("debug", "Importing %d archive stores (%s)...", #selected_stores.archive, table.concat(selected_stores.archive, ", ")); |
4869
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
325 |
for store_name in it.values(selected_stores.archive) do |
4880
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
326 |
module:log("debug", "Importing archive store %s...", store_name); |
4869
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
327 |
local source_archive = xep227_driver:open_xep0227(store_name, "archive", user_xml); |
4880
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
328 |
|
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
329 |
local importer = special_archive_importers[store_name] or generic_archive_importer; |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
330 |
local ok, err = importer(username, session.host, store_name, source_archive); |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
331 |
if not ok then |
0f5f2d4475b9
mod_http_xep227: Add support for import via APIs rather than direct store manipulation
Matthew Wild <mwild1@gmail.com>
parents:
4877
diff
changeset
|
332 |
module:log("warn", "Importer for archive store '%s' encountered error: %s", err or "<no error returned>"); |
4875
029ae3c29683
mod_http_xep227: Handle nil/errors opening archive stores
Matthew Wild <mwild1@gmail.com>
parents:
4874
diff
changeset
|
333 |
return 500; |
4869
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
334 |
end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
335 |
end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
336 |
|
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
337 |
return 200; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
338 |
end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
339 |
|
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
340 |
--- |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
341 |
|
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
342 |
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
|
343 |
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
|
344 |
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
|
345 |
return false; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
346 |
end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
347 |
|
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
348 |
if auth_type == "Bearer" then |
5003
65cdbbf9703a
mod_http_xep227: Updates for new mod_tokenauth (trunk/0.13 only)
Matthew Wild <mwild1@gmail.com>
parents:
4880
diff
changeset
|
349 |
return tokens.get_token_session(auth_data); |
4869
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
350 |
end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
351 |
return nil; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
352 |
end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
353 |
|
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
354 |
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
|
355 |
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
|
356 |
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
|
357 |
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
|
358 |
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
|
359 |
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
|
360 |
end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
361 |
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
|
362 |
return true; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
363 |
end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
364 |
|
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
365 |
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
|
366 |
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
|
367 |
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
|
368 |
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
|
369 |
return code; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
370 |
end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
371 |
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
|
372 |
end; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
373 |
end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
374 |
return routes; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
375 |
end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
376 |
|
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
377 |
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
|
378 |
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
|
379 |
["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
|
380 |
["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
|
381 |
}; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
382 |
}); |