Mercurial > prosody-modules
annotate mod_http_xep227/mod_http_xep227.lua @ 4867:9d29467f4d5b
mod_http_xep227: Fix luacheck warnings
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 15 Jan 2022 13:46:34 +0000 |
parents | bd0a1f917d98 |
children | 57311c545013 |
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 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
135 if not user_xml or not user_xml:find("host/user") then |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 module:log("warn", "No data to export: %s", tostring(user_xml)); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 return 204; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
138 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
139 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 event.response.headers["Content-Type"] = "application/xml"; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
141 return [[<?xml version="1.0" encoding="utf-8" ?>]]..tostring(user_xml); |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
143 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
144 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
|
145 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
|
146 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
|
147 return false; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
148 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 -- 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
|
150 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
|
151 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
152 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
153 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
|
154 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
|
155 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
|
156 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 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
|
158 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
|
159 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
160 -- Some sanity checks |
4867
9d29467f4d5b
mod_http_xep227: Fix luacheck warnings
Matthew Wild <mwild1@gmail.com>
parents:
4865
diff
changeset
|
161 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
|
162 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
|
163 return 422; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
164 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
165 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
166 -- 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
|
167 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
|
168 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
|
169 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
170 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
|
171 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
172 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
|
173 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
174 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
|
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 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
|
177 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
178 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
|
179 -- 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
|
180 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
|
181 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
182 -- 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
|
183 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
|
184 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
|
185 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
|
186 -- 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
|
187 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
|
188 return 500; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
189 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
190 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
|
191 return 500; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
192 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
193 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
194 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
195 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
|
196 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
|
197 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
|
198 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
|
199 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
|
200 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
|
201 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
202 end |
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 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
205 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
|
206 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
|
207 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
|
208 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
|
209 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
|
210 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
|
211 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
|
212 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
|
213 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
|
214 else |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
215 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
|
216 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
|
217 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
218 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
|
219 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
|
220 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
221 end |
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 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
224 return 200; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
225 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
226 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
227 --- |
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 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
|
230 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
|
231 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
|
232 return false; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
233 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
234 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
235 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
|
236 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
|
237 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
|
238 return false; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
239 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
240 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
|
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 nil; |
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 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
245 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
|
246 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
|
247 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
|
248 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
|
249 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
|
250 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
|
251 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
|
252 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
|
253 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
254 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
|
255 return true; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
256 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
257 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
258 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
|
259 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
|
260 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
|
261 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
|
262 return code; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
263 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
264 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
|
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 end |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
267 return routes; |
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 |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
270 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
|
271 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
|
272 ["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
|
273 ["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
|
274 }; |
bd0a1f917d98
mod_http_xep227: New module providing HTTP API for account data import/export
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
275 }); |