Software /
code /
prosody-modules
Annotate
mod_http_xep227/mod_http_xep227.lua @ 4868:57311c545013
mod_http_xep227: Fix validation of resulting export XML
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 15 Jan 2022 14:06:19 +0000 |
parent | 4867:9d29467f4d5b |
child | 4869:c3bf568e3977 |
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"; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 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
|
5 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 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
|
7 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
|
8 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 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
|
10 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 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
|
12 accounts = "keyval"; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 roster = "keyval"; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 private = "keyval"; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 pep = "keyval"; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 vcard = "keyval"; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 [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
|
19 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
|
20 }; |
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 local function new_user_xml(username, host) |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 local user_xml; |
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 return { |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 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
|
27 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
|
28 return nil; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 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
|
31 return true; |
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 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 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
|
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 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
|
39 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 }; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 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
|
44 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
|
45 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
|
46 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
|
47 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
|
48 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
|
49 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
|
50 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
|
51 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
|
52 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
|
53 else |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 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
|
55 end |
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 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
|
58 return 400; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 return { |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 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
|
64 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
|
65 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
|
66 }; |
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 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 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
|
70 -- 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
|
71 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
|
72 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
|
73 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 -- Return driver |
4867
9d29467f4d5b
mod_http_xep227: Fix luacheck warnings
Matthew Wild <mwild1@gmail.com>
parents:
4865
diff
changeset
|
75 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
|
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 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 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
|
79 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
|
80 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
|
81 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 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
|
83 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 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
|
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 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
|
87 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 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
|
89 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 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
|
91 -- 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
|
92 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
|
93 -- 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
|
94 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
|
95 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
|
96 -- 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
|
97 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
|
98 -- 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
|
99 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
|
100 return 500; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 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
|
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 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 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
|
108 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
|
109 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
|
110 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
|
111 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
|
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 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 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
|
117 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
|
118 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
|
119 local dest_archive = xep227_driver:open_xep0227(store_name, "archive", user_xml); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 local count, errs = 0, 0; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 for id, item, when, with in source_archive:find(username) do |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 local ok, err = dest_archive:append(username, id, item, when, with); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 if ok then |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
124 count = count + 1; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
125 else |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
126 module:log("warn", "Error: %s", err); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
127 errs = errs + 1; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
128 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 if ( count + errs ) % 100 == 0 then |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
130 module:log("info", "%d items migrated, %d errors", count, errs); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
131 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
132 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
133 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
134 |
4868
57311c545013
mod_http_xep227: Fix validation of resulting export XML
Matthew Wild <mwild1@gmail.com>
parents:
4867
diff
changeset
|
135 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
|
136 |
57311c545013
mod_http_xep227: Fix validation of resulting export XML
Matthew Wild <mwild1@gmail.com>
parents:
4867
diff
changeset
|
137 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
|
138 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
|
139 return 204; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
141 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 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
|
143 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
|
144 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
145 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
146 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
|
147 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
|
148 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
|
149 return false; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
150 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
151 -- 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
|
152 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
|
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 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
|
156 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
|
157 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
|
158 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
159 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
|
160 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
|
161 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
162 -- Some sanity checks |
4867
9d29467f4d5b
mod_http_xep227: Fix luacheck warnings
Matthew Wild <mwild1@gmail.com>
parents:
4865
diff
changeset
|
163 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
|
164 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
|
165 return 422; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
166 end |
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 -- 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
|
169 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
|
170 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
|
171 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
172 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
|
173 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
174 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
|
175 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
176 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
|
177 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
178 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
|
179 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
180 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
|
181 -- 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
|
182 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
|
183 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
184 -- 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
|
185 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
|
186 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
|
187 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
|
188 -- 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
|
189 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
|
190 return 500; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
191 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
192 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
|
193 return 500; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
194 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
195 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
196 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
197 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
|
198 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
|
199 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
|
200 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
|
201 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
|
202 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
|
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 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
206 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
207 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
|
208 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
|
209 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
|
210 local dest_archive = dest_driver:open(store_name, "archive"); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
211 local count, errs = 0, 0; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
212 for id, item, when, with in source_archive:find(username) do |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
213 local ok, err = dest_archive:append(username, id, item, when, with); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
214 if ok then |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
215 count = count + 1; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
216 else |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
217 module:log("warn", "Error: %s", err); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
218 errs = errs + 1; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
219 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
220 if ( count + errs ) % 100 == 0 then |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
221 module:log("info", "%d items migrated, %d errors", count, errs); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
222 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
223 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
224 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
225 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
226 return 200; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
227 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
228 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
229 --- |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
230 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
231 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
|
232 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
|
233 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
|
234 return false; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
235 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
236 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
237 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
|
238 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
|
239 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
|
240 return false; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
241 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
242 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
|
243 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
244 return nil; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
245 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
246 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
247 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
|
248 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
|
249 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
|
250 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
|
251 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
|
252 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
|
253 elseif session.auth_scope ~= "prosody:scope:admin" then |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
254 return false, 403; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
255 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
256 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
|
257 return true; |
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 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
260 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
|
261 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
|
262 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
|
263 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
|
264 return code; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
265 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
266 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
|
267 end; |
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 return routes; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
270 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
271 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
272 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
|
273 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
|
274 ["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
|
275 ["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
|
276 }; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
277 }); |