mod_http_admin_api/mod_http_admin_api.lua
author Matthew Wild <mwild1@gmail.com>
Sat, 23 Jan 2021 11:59:23 +0000
changeset 4378 e707810a943e
parent 4375 3d01bc4547b2
child 4379 03cf0d41b272
permissions -rw-r--r--
mod_http_admin_api: Improve invite API and support password resets
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4349
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
     1
local usermanager = require "core.usermanager";
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
     2
4372
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
     3
local id = require "util.id";
4347
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     4
local json = require "util.json";
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     5
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     6
module:depends("http");
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     7
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     8
local invites = module:depends("invites");
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     9
local tokens = module:depends("tokenauth");
4349
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
    10
local mod_pep = module:depends("pep");
4347
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
4372
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
    12
local group_info_store = module:open_store("group_info");
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
    13
local group_members_store = module:open_store("groups");
4356
f6da234b21b8 mod_http_admin_api: Add groups property to users
Matthew Wild <mwild1@gmail.com>
parents: 4355
diff changeset
    14
local group_memberships = module:open_store("groups", "map");
4367
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
    15
local push_errors = module:shared("cloud_notify/push_errors");
4356
f6da234b21b8 mod_http_admin_api: Add groups property to users
Matthew Wild <mwild1@gmail.com>
parents: 4355
diff changeset
    16
4375
3d01bc4547b2 mod_http_admin_api: Add /server/info endpoint for site_name and version
Matthew Wild <mwild1@gmail.com>
parents: 4373
diff changeset
    17
local site_name = module:get_option_string("site_name", module.host);
3d01bc4547b2 mod_http_admin_api: Add /server/info endpoint for site_name and version
Matthew Wild <mwild1@gmail.com>
parents: 4373
diff changeset
    18
4347
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    19
local json_content_type = "application/json";
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    20
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    21
local www_authenticate_header = ("Bearer realm=%q"):format(module.host.."/"..module.name);
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    22
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    23
local function check_credentials(request)
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    24
	local auth_type, auth_data = string.match(request.headers.authorization or "", "^(%S+)%s(.+)$");
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    25
	if not (auth_type and auth_data) then
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
		return false;
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
	end
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    29
	if auth_type == "Bearer" then
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    30
		local token_info = tokens.get_token_info(auth_data);
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    31
		if not token_info or not token_info.session then
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    32
			return false;
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    33
		end
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    34
		return token_info.session;
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    35
	end
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    36
	return nil;
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    37
end
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    38
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    39
function check_auth(routes)
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    40
	local function check_request_auth(event)
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    41
		local session = check_credentials(event.request);
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    42
		if not session then
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    43
			event.response.headers.authorization = www_authenticate_header;
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    44
			return false, 401;
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    45
		elseif session.auth_scope ~= "prosody:scope:admin" then
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    46
			return false, 403;
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    47
		end
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    48
		event.session = session;
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    49
		return true;
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    50
	end
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    51
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    52
	for route, handler in pairs(routes) do
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    53
		routes[route] = function (event, ...)
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    54
			local permit, code = check_request_auth(event);
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    55
			if not permit then
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    56
				return code;
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    57
			end
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    58
			return handler(event, ...);
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    59
		end;
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    60
	end
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    61
	return routes;
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    62
end
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    63
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    64
local function token_info_to_invite_info(token_info)
4353
5ca36c36ab05 mod_http_admin_api: Expose new invite properties: groups, source and reusable
Matthew Wild <mwild1@gmail.com>
parents: 4349
diff changeset
    65
	local additional_data = token_info.additional_data;
5ca36c36ab05 mod_http_admin_api: Expose new invite properties: groups, source and reusable
Matthew Wild <mwild1@gmail.com>
parents: 4349
diff changeset
    66
	local groups = additional_data and additional_data.groups or nil;
5ca36c36ab05 mod_http_admin_api: Expose new invite properties: groups, source and reusable
Matthew Wild <mwild1@gmail.com>
parents: 4349
diff changeset
    67
	local source = additional_data and additional_data.source or nil;
4347
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    68
	return {
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    69
		id = token_info.token;
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    70
		type = token_info.type;
4362
d3e0fe470877 mod_http_admin_api: Ensure 'reusable' flag is always present on an invite
Matthew Wild <mwild1@gmail.com>
parents: 4361
diff changeset
    71
		reusable = not not token_info.reusable;
4347
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    72
		inviter = token_info.inviter;
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    73
		jid = token_info.jid;
4358
d61d7d30f38d mod_http_admin_api: Add XMPP URI into invite objects
Matthew Wild <mwild1@gmail.com>
parents: 4357
diff changeset
    74
		uri = token_info.uri;
4347
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    75
		landing_page = token_info.landing_page;
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    76
		created_at = token_info.created_at;
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    77
		expires = token_info.expires;
4353
5ca36c36ab05 mod_http_admin_api: Expose new invite properties: groups, source and reusable
Matthew Wild <mwild1@gmail.com>
parents: 4349
diff changeset
    78
		groups = groups;
5ca36c36ab05 mod_http_admin_api: Expose new invite properties: groups, source and reusable
Matthew Wild <mwild1@gmail.com>
parents: 4349
diff changeset
    79
		source = source;
4347
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    80
	};
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    81
end
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    82
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    83
function list_invites(event)
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    84
	local invites_list = {};
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    85
	for token, invite in invites.pending_account_invites() do --luacheck: ignore 213/token
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    86
		table.insert(invites_list, token_info_to_invite_info(invite));
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    87
	end
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    88
	table.sort(invites_list, function (a, b)
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    89
		return a.created_at < b.created_at;
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    90
	end);
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    91
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    92
	event.response.headers["Content-Type"] = json_content_type;
4354
270025e76bf8 mod_http_admin_api: Use json.encode_array() when returning an array
Matthew Wild <mwild1@gmail.com>
parents: 4353
diff changeset
    93
	return json.encode_array(invites_list);
4347
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    94
end
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    95
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    96
function get_invite_by_id(event, invite_id)
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    97
	local invite = invites.get_account_invite_info(invite_id);
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    98
	if not invite then
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    99
		return 404;
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   100
	end
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   101
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   102
	event.response.headers["Content-Type"] = json_content_type;
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   103
	return json.encode(token_info_to_invite_info(invite));
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   104
end
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   105
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   106
function create_invite(event)
4355
c0b1b2a61e3c mod_http_admin_api: Add support for creating reusable/group/custom-ttl invites
Matthew Wild <mwild1@gmail.com>
parents: 4354
diff changeset
   107
	local invite_options;
c0b1b2a61e3c mod_http_admin_api: Add support for creating reusable/group/custom-ttl invites
Matthew Wild <mwild1@gmail.com>
parents: 4354
diff changeset
   108
c0b1b2a61e3c mod_http_admin_api: Add support for creating reusable/group/custom-ttl invites
Matthew Wild <mwild1@gmail.com>
parents: 4354
diff changeset
   109
	local request = event.request;
c0b1b2a61e3c mod_http_admin_api: Add support for creating reusable/group/custom-ttl invites
Matthew Wild <mwild1@gmail.com>
parents: 4354
diff changeset
   110
	if request.body and #request.body > 0 then
c0b1b2a61e3c mod_http_admin_api: Add support for creating reusable/group/custom-ttl invites
Matthew Wild <mwild1@gmail.com>
parents: 4354
diff changeset
   111
		if request.headers.content_type ~= json_content_type then
c0b1b2a61e3c mod_http_admin_api: Add support for creating reusable/group/custom-ttl invites
Matthew Wild <mwild1@gmail.com>
parents: 4354
diff changeset
   112
			module:log("warn", "Invalid content type");
c0b1b2a61e3c mod_http_admin_api: Add support for creating reusable/group/custom-ttl invites
Matthew Wild <mwild1@gmail.com>
parents: 4354
diff changeset
   113
			return 400;
c0b1b2a61e3c mod_http_admin_api: Add support for creating reusable/group/custom-ttl invites
Matthew Wild <mwild1@gmail.com>
parents: 4354
diff changeset
   114
		end
c0b1b2a61e3c mod_http_admin_api: Add support for creating reusable/group/custom-ttl invites
Matthew Wild <mwild1@gmail.com>
parents: 4354
diff changeset
   115
		invite_options = json.decode(event.request.body);
c0b1b2a61e3c mod_http_admin_api: Add support for creating reusable/group/custom-ttl invites
Matthew Wild <mwild1@gmail.com>
parents: 4354
diff changeset
   116
		if not invite_options then
c0b1b2a61e3c mod_http_admin_api: Add support for creating reusable/group/custom-ttl invites
Matthew Wild <mwild1@gmail.com>
parents: 4354
diff changeset
   117
			module:log("warn", "Invalid JSON");
c0b1b2a61e3c mod_http_admin_api: Add support for creating reusable/group/custom-ttl invites
Matthew Wild <mwild1@gmail.com>
parents: 4354
diff changeset
   118
			return 400;
c0b1b2a61e3c mod_http_admin_api: Add support for creating reusable/group/custom-ttl invites
Matthew Wild <mwild1@gmail.com>
parents: 4354
diff changeset
   119
		end
4363
5dbce7f35aa0 mod_http_admin_api: Fix logic bug
Matthew Wild <mwild1@gmail.com>
parents: 4362
diff changeset
   120
	else
5dbce7f35aa0 mod_http_admin_api: Fix logic bug
Matthew Wild <mwild1@gmail.com>
parents: 4362
diff changeset
   121
		invite_options = {};
4355
c0b1b2a61e3c mod_http_admin_api: Add support for creating reusable/group/custom-ttl invites
Matthew Wild <mwild1@gmail.com>
parents: 4354
diff changeset
   122
	end
c0b1b2a61e3c mod_http_admin_api: Add support for creating reusable/group/custom-ttl invites
Matthew Wild <mwild1@gmail.com>
parents: 4354
diff changeset
   123
c0b1b2a61e3c mod_http_admin_api: Add support for creating reusable/group/custom-ttl invites
Matthew Wild <mwild1@gmail.com>
parents: 4354
diff changeset
   124
	local invite;
4363
5dbce7f35aa0 mod_http_admin_api: Fix logic bug
Matthew Wild <mwild1@gmail.com>
parents: 4362
diff changeset
   125
	if invite_options.reusable then
4361
a49ca492e621 mod_invites, mod_http_admin_api: Allow specifying multiple groups when creating an invite
Matthew Wild <mwild1@gmail.com>
parents: 4358
diff changeset
   126
		invite = invites.create_group(invite_options.groups, invite_options.ttl, {
4355
c0b1b2a61e3c mod_http_admin_api: Add support for creating reusable/group/custom-ttl invites
Matthew Wild <mwild1@gmail.com>
parents: 4354
diff changeset
   127
			source = "admin_api/"..event.session.username;
c0b1b2a61e3c mod_http_admin_api: Add support for creating reusable/group/custom-ttl invites
Matthew Wild <mwild1@gmail.com>
parents: 4354
diff changeset
   128
		});
c0b1b2a61e3c mod_http_admin_api: Add support for creating reusable/group/custom-ttl invites
Matthew Wild <mwild1@gmail.com>
parents: 4354
diff changeset
   129
	else
c0b1b2a61e3c mod_http_admin_api: Add support for creating reusable/group/custom-ttl invites
Matthew Wild <mwild1@gmail.com>
parents: 4354
diff changeset
   130
		invite = invites.create_account(nil, {
c0b1b2a61e3c mod_http_admin_api: Add support for creating reusable/group/custom-ttl invites
Matthew Wild <mwild1@gmail.com>
parents: 4354
diff changeset
   131
			source = "admin_api/"..event.session.username;
4361
a49ca492e621 mod_invites, mod_http_admin_api: Allow specifying multiple groups when creating an invite
Matthew Wild <mwild1@gmail.com>
parents: 4358
diff changeset
   132
			groups = invite_options.groups;
4355
c0b1b2a61e3c mod_http_admin_api: Add support for creating reusable/group/custom-ttl invites
Matthew Wild <mwild1@gmail.com>
parents: 4354
diff changeset
   133
		});
c0b1b2a61e3c mod_http_admin_api: Add support for creating reusable/group/custom-ttl invites
Matthew Wild <mwild1@gmail.com>
parents: 4354
diff changeset
   134
	end
4347
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   135
	if not invite then
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   136
		return 500;
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   137
	end
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   138
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   139
	event.response.headers["Content-Type"] = json_content_type;
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   140
	return json.encode(token_info_to_invite_info(invite));
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   141
end
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   142
4378
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   143
function create_invite_type(event, invite_type)
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   144
	local options;
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   145
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   146
	local request = event.request;
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   147
	if request.body and #request.body > 0 then
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   148
		if request.headers.content_type ~= json_content_type then
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   149
			module:log("warn", "Invalid content type");
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   150
			return 400;
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   151
		end
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   152
		options = json.decode(event.request.body);
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   153
		if not options then
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   154
			module:log("warn", "Invalid JSON");
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   155
			return 400;
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   156
		end
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   157
	else
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   158
		options = {};
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   159
	end
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   160
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   161
	local invite;
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   162
	if invite_type == "reset" then
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   163
		if not options.username then
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   164
			return 400;
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   165
		end
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   166
		invite = invites.create_account_reset(options.username, options.ttl);
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   167
	elseif invite_type == "group" then
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   168
		if not options.groups then
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   169
			return 400;
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   170
		end
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   171
		invite = invites.create_group(options.groups, nil, options.ttl);
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   172
	elseif invite_type == "account" then
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   173
		invite = invites.create_account(options.username, nil, options.ttl);
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   174
	else
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   175
		return 400;
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   176
	end
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   177
	if not invite then
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   178
		return 500;
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   179
	end
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   180
	event.response.headers["Content-Type"] = json_content_type;
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   181
	return json.encode(token_info_to_invite_info(invite));
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   182
end
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   183
4347
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   184
function delete_invite(event, invite_id) --luacheck: ignore 212/event
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   185
	if not invites.delete_account_invite(invite_id) then
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   186
		return 404;
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   187
	end
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   188
	return 200;
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   189
end
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   190
4349
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   191
local function get_user_info(username)
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   192
	if not usermanager.user_exists(username, module.host) then
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   193
		return nil;
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   194
	end
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   195
	local display_name;
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   196
	do
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   197
		local pep_service = mod_pep.get_pep_service(username);
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   198
		local ok, _, nick_item = pep_service:get_last_item("http://jabber.org/protocol/nick", true);
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   199
		if ok and nick_item then
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   200
			display_name = nick_item:get_child_text("nick", "http://jabber.org/protocol/nick");
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   201
		end
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   202
	end
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   203
4366
116c88c28532 mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents: 4365
diff changeset
   204
	return {
116c88c28532 mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents: 4365
diff changeset
   205
		username = username;
116c88c28532 mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents: 4365
diff changeset
   206
		display_name = display_name;
116c88c28532 mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents: 4365
diff changeset
   207
	};
116c88c28532 mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents: 4365
diff changeset
   208
end
116c88c28532 mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents: 4365
diff changeset
   209
4367
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   210
local function get_session_debug_info(session)
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   211
	local info = {
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   212
		full_jid = session.full_jid;
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   213
		ip = session.ip;
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   214
		since = math.floor(session.conntime);
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   215
		status = {
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   216
			connected = not not session.conn;
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   217
			hibernating = not not session.hibernating;
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   218
		};
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   219
		features = {
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   220
			carbons = not not session.want_carbons;
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   221
			encrypted = not not session.secure;
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   222
			acks = not not session.smacks;
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   223
			resumption = not not session.resumption_token;
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   224
			mobile_optimization = not not session.csi_counter;
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   225
			push_notifications = not not session.push_identifier;
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   226
			history = not not session.mam_requested;
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   227
		};
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   228
		queues = {};
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   229
	};
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   230
	-- CSI
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   231
	if session.state then
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   232
		info.status.active = session.state == "active";
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   233
		info.queues.held_stanzas = session.csi_counter or 0;
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   234
	end
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   235
	-- Smacks queue
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   236
	if session.last_requested_h and session.last_acknowledged_stanza then
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   237
		info.queues.awaiting_acks = session.last_requested_h - session.last_acknowledged_stanza;
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   238
	end
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   239
	if session.push_identifier then
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   240
		info.push_info = {
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   241
			id = session.push_identifier;
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   242
			wakeup_push_sent = session.first_hibernated_push;
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   243
		};
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   244
	end
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   245
	return info;
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   246
end
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   247
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   248
local function get_user_omemo_info(username)
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   249
	local everything_valid = true;
4370
83370df0ce4a mod_http_admin_api: do not report OMEMO as ok if no devices exist
Jonas Schäfer <jonas@wielicki.name>
parents: 4369
diff changeset
   250
	local any_device = false;
4367
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   251
	local omemo_status = {};
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   252
	local omemo_devices;
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   253
	local pep_service = mod_pep.get_pep_service(username);
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   254
	if pep_service and pep_service.nodes then
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   255
		local ok, _, device_list = pep_service:get_last_item("eu.siacs.conversations.axolotl.devicelist", true);
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   256
		if ok and device_list then
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   257
			device_list = device_list:get_child("list", "eu.siacs.conversations.axolotl");
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   258
		end
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   259
		if device_list then
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   260
			omemo_devices = {};
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   261
			for device_entry in device_list:childtags("device") do
4370
83370df0ce4a mod_http_admin_api: do not report OMEMO as ok if no devices exist
Jonas Schäfer <jonas@wielicki.name>
parents: 4369
diff changeset
   262
				any_device = true;
4367
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   263
				local device_info = {};
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   264
				local device_id = tonumber(device_entry.attr.id or "");
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   265
				if device_id then
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   266
					device_info.id = device_id;
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   267
					local bundle_id = ("eu.siacs.conversations.axolotl.bundles:%d"):format(device_id);
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   268
					local have_bundle, _, bundle = pep_service:get_last_item(bundle_id, true);
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   269
					if have_bundle and bundle and bundle:get_child("bundle", "eu.siacs.conversations.axolotl") then
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   270
						device_info.have_bundle = true;
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   271
						local config_ok, bundle_config = pep_service:get_node_config(bundle_id, true);
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   272
						if config_ok and bundle_config then
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   273
							device_info.bundle_config = bundle_config;
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   274
							if bundle_config.max_items == 1
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   275
							and bundle_config.access_model == "open"
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   276
							and bundle_config.persist_items == true
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   277
							and bundle_config.publish_model == "publishers" then
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   278
								device_info.valid = true;
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   279
							end
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   280
						end
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   281
					end
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   282
				end
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   283
				if device_info.valid == nil then
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   284
					device_info.valid = false;
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   285
					everything_valid = false;
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   286
				end
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   287
				table.insert(omemo_devices, device_info);
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   288
			end
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   289
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   290
			local config_ok, list_config = pep_service:get_node_config("eu.siacs.conversations.axolotl.devicelist", true);
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   291
			if config_ok and list_config then
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   292
				omemo_status.config = list_config;
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   293
				if list_config.max_items == 1
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   294
				and list_config.access_model == "open"
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   295
				and list_config.persist_items == true
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   296
				and list_config.publish_model == "publishers" then
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   297
					omemo_status.config_valid = true;
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   298
				end
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   299
			end
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   300
			if omemo_status.config_valid == nil then
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   301
				omemo_status.config_valid = false;
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   302
				everything_valid = false;
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   303
			end
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   304
		end
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   305
	end
4370
83370df0ce4a mod_http_admin_api: do not report OMEMO as ok if no devices exist
Jonas Schäfer <jonas@wielicki.name>
parents: 4369
diff changeset
   306
	omemo_status.valid = everything_valid and any_device;
4367
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   307
	return {
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   308
		status = omemo_status;
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   309
		devices = omemo_devices;
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   310
	};
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   311
end
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   312
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   313
local function get_user_debug_info(username)
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   314
	local debug_info = {
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   315
		time = os.time();
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   316
	};
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   317
	-- Online sessions
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   318
	do
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   319
		local user_sessions = hosts[module.host].sessions[username];
4369
f975a4d31f35 mod_http_admin_api: make the api a bit less untested
Jonas Schäfer <jonas@wielicki.name>
parents: 4368
diff changeset
   320
		if user_sessions then
f975a4d31f35 mod_http_admin_api: make the api a bit less untested
Jonas Schäfer <jonas@wielicki.name>
parents: 4368
diff changeset
   321
			user_sessions = user_sessions.sessions
f975a4d31f35 mod_http_admin_api: make the api a bit less untested
Jonas Schäfer <jonas@wielicki.name>
parents: 4368
diff changeset
   322
		end
4367
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   323
		local sessions = {};
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   324
		if user_sessions then
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   325
			for _, session in pairs(user_sessions) do
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   326
				table.insert(sessions, get_session_debug_info(session));
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   327
			end
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   328
		end
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   329
		debug_info.sessions = sessions;
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   330
	end
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   331
	-- Push registrations
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   332
	do
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   333
		local store = module:open_store("cloud_notify");
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   334
		local services = store:get(username);
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   335
		local push_registrations = {};
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   336
		if services then
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   337
			for identifier, push_info in pairs(services) do
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   338
				push_registrations[identifier] = {
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   339
					since = push_info.timestamp;
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   340
					service = push_info.jid;
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   341
					node = push_info.node;
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   342
					error_count = push_errors[identifier] or 0;
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   343
				};
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   344
			end
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   345
		end
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   346
		debug_info.push_registrations = push_registrations;
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   347
	end
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   348
	-- OMEMO
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   349
	debug_info.omemo = get_user_omemo_info(username);
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   350
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   351
	return debug_info;
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   352
end
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   353
4366
116c88c28532 mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents: 4365
diff changeset
   354
local function get_user_groups(username)
4356
f6da234b21b8 mod_http_admin_api: Add groups property to users
Matthew Wild <mwild1@gmail.com>
parents: 4355
diff changeset
   355
	local groups;
f6da234b21b8 mod_http_admin_api: Add groups property to users
Matthew Wild <mwild1@gmail.com>
parents: 4355
diff changeset
   356
	do
f6da234b21b8 mod_http_admin_api: Add groups property to users
Matthew Wild <mwild1@gmail.com>
parents: 4355
diff changeset
   357
		local group_set = group_memberships:get_all(username);
f6da234b21b8 mod_http_admin_api: Add groups property to users
Matthew Wild <mwild1@gmail.com>
parents: 4355
diff changeset
   358
		if group_set and next(group_set) then
f6da234b21b8 mod_http_admin_api: Add groups property to users
Matthew Wild <mwild1@gmail.com>
parents: 4355
diff changeset
   359
			groups = {};
f6da234b21b8 mod_http_admin_api: Add groups property to users
Matthew Wild <mwild1@gmail.com>
parents: 4355
diff changeset
   360
			for group_id in pairs(group_set) do
f6da234b21b8 mod_http_admin_api: Add groups property to users
Matthew Wild <mwild1@gmail.com>
parents: 4355
diff changeset
   361
				table.insert(groups, group_id);
f6da234b21b8 mod_http_admin_api: Add groups property to users
Matthew Wild <mwild1@gmail.com>
parents: 4355
diff changeset
   362
			end
f6da234b21b8 mod_http_admin_api: Add groups property to users
Matthew Wild <mwild1@gmail.com>
parents: 4355
diff changeset
   363
		end
f6da234b21b8 mod_http_admin_api: Add groups property to users
Matthew Wild <mwild1@gmail.com>
parents: 4355
diff changeset
   364
	end
4366
116c88c28532 mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents: 4365
diff changeset
   365
	return groups;
4349
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   366
end
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   367
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   368
function list_users(event)
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   369
	local user_list = {};
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   370
	for username in usermanager.users(module.host) do
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   371
		table.insert(user_list, get_user_info(username));
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   372
	end
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   373
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   374
	event.response.headers["Content-Type"] = json_content_type;
4354
270025e76bf8 mod_http_admin_api: Use json.encode_array() when returning an array
Matthew Wild <mwild1@gmail.com>
parents: 4353
diff changeset
   375
	return json.encode_array(user_list);
4349
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   376
end
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   377
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   378
function get_user_by_name(event, username)
4366
116c88c28532 mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents: 4365
diff changeset
   379
	local property
116c88c28532 mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents: 4365
diff changeset
   380
	do
116c88c28532 mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents: 4365
diff changeset
   381
		local name, sub_path = username:match("^([^/]+)/(%w+)$");
116c88c28532 mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents: 4365
diff changeset
   382
		if name then
116c88c28532 mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents: 4365
diff changeset
   383
			username = name;
116c88c28532 mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents: 4365
diff changeset
   384
			property = sub_path;
116c88c28532 mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents: 4365
diff changeset
   385
		end
116c88c28532 mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents: 4365
diff changeset
   386
	end
116c88c28532 mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents: 4365
diff changeset
   387
116c88c28532 mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents: 4365
diff changeset
   388
	if property == "groups" then
4368
49cf9d188b26 mod_http_admin_api: set content-type for debug API
Jonas Schäfer <jonas@wielicki.name>
parents: 4367
diff changeset
   389
		event.response.headers["Content-Type"] = json_content_type;
4366
116c88c28532 mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents: 4365
diff changeset
   390
		return json.encode(get_user_groups(username));
4367
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   391
	elseif property == "debug" then
4368
49cf9d188b26 mod_http_admin_api: set content-type for debug API
Jonas Schäfer <jonas@wielicki.name>
parents: 4367
diff changeset
   392
		event.response.headers["Content-Type"] = json_content_type;
4367
636d56bbad97 mod_http_admin_api: 100% untested user debug info endpoint
Matthew Wild <mwild1@gmail.com>
parents: 4366
diff changeset
   393
		return json.encode(get_user_debug_info(username));
4366
116c88c28532 mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents: 4365
diff changeset
   394
	end
116c88c28532 mod_http_admin_api: restructure group-related info in API
Jonas Schäfer <jonas@wielicki.name>
parents: 4365
diff changeset
   395
4349
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   396
	local user_info = get_user_info(username);
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   397
	if not user_info then
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   398
		return 404;
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   399
	end
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   400
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   401
	event.response.headers["Content-Type"] = json_content_type;
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   402
	return json.encode(user_info);
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   403
end
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   404
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   405
function delete_user(event, username) --luacheck: ignore 212/event
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   406
	if not usermanager.delete_user(username, module.host) then
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   407
		return 404;
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   408
	end
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   409
	return 200;
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   410
end
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   411
4357
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   412
function list_groups(event)
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   413
	local group_list = {};
4372
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   414
	for group_id in group_info_store:users() do
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   415
		local group_info = group_info_store:get(group_id);
4357
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   416
		table.insert(group_list, {
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   417
			id = group_id;
4372
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   418
			name = group_info.name;
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   419
			members = group_members_store:get(group_id);
4357
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   420
		});
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   421
	end
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   422
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   423
	event.response.headers["Content-Type"] = json_content_type;
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   424
	return json.encode_array(group_list);
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   425
end
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   426
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   427
function get_group_by_id(event, group_id)
4372
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   428
	local group = group_info_store:get(group_id);
4357
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   429
	if not group then
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   430
		return 404;
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   431
	end
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   432
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   433
	event.response.headers["Content-Type"] = json_content_type;
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   434
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   435
	return json.encode({
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   436
		id = group_id;
4372
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   437
		name = group.name;
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   438
		members = group_members_store:get(group_id);
4357
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   439
	});
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   440
end
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   441
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   442
function create_group(event)
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   443
	local request = event.request;
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   444
	if request.headers.content_type ~= json_content_type
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   445
	or (not request.body or #request.body == 0) then
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   446
		return 400;
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   447
	end
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   448
	local group = json.decode(event.request.body);
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   449
	if not group then
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   450
		return 400;
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   451
	end
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   452
4372
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   453
	if not group.name then
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   454
		module:log("warn", "Group missing name property");
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   455
		return 400;
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   456
	end
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   457
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   458
	local group_id = id.short();
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   459
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   460
	local ok = group_info_store:set(group_id, {
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   461
		name = group.name;
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   462
	});
4357
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   463
	if not ok then
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   464
		return 500;
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   465
	end
4365
7f1f3b79d991 mod_http_admin_api: Return new group object on successful creation
Matthew Wild <mwild1@gmail.com>
parents: 4364
diff changeset
   466
7f1f3b79d991 mod_http_admin_api: Return new group object on successful creation
Matthew Wild <mwild1@gmail.com>
parents: 4364
diff changeset
   467
	event.response.headers["Content-Type"] = json_content_type;
7f1f3b79d991 mod_http_admin_api: Return new group object on successful creation
Matthew Wild <mwild1@gmail.com>
parents: 4364
diff changeset
   468
	return json.encode({
4372
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   469
		id = group_id;
4365
7f1f3b79d991 mod_http_admin_api: Return new group object on successful creation
Matthew Wild <mwild1@gmail.com>
parents: 4364
diff changeset
   470
		name = group.name;
4372
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   471
		members = {};
4365
7f1f3b79d991 mod_http_admin_api: Return new group object on successful creation
Matthew Wild <mwild1@gmail.com>
parents: 4364
diff changeset
   472
	});
4357
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   473
end
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   474
4372
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   475
function update_group(event, group) --luacheck: ignore 212/event
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   476
	local group_id, member_name = group:match("^([^/]+)/members/([^/]+)$");
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   477
	if group_id and member_name then
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   478
		if not group_info_store:get(group_id) then
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   479
			return 404;
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   480
		elseif not group_memberships:set(group_id, member_name, true) then
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   481
			return 500;
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   482
		end
4373
29b7f445aec5 mod_http_admin_api: add support for updating groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4372
diff changeset
   483
		return 204;
29b7f445aec5 mod_http_admin_api: add support for updating groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4372
diff changeset
   484
	end
29b7f445aec5 mod_http_admin_api: add support for updating groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4372
diff changeset
   485
29b7f445aec5 mod_http_admin_api: add support for updating groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4372
diff changeset
   486
	local group_id = group:match("^([^/]+)$")
29b7f445aec5 mod_http_admin_api: add support for updating groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4372
diff changeset
   487
	if group_id then
29b7f445aec5 mod_http_admin_api: add support for updating groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4372
diff changeset
   488
		local request = event.request;
29b7f445aec5 mod_http_admin_api: add support for updating groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4372
diff changeset
   489
		if request.headers.content_type ~= json_content_type
29b7f445aec5 mod_http_admin_api: add support for updating groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4372
diff changeset
   490
		or (not request.body or #request.body == 0) then
29b7f445aec5 mod_http_admin_api: add support for updating groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4372
diff changeset
   491
			return 400;
29b7f445aec5 mod_http_admin_api: add support for updating groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4372
diff changeset
   492
		end
29b7f445aec5 mod_http_admin_api: add support for updating groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4372
diff changeset
   493
29b7f445aec5 mod_http_admin_api: add support for updating groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4372
diff changeset
   494
		local update = json.decode(event.request.body);
29b7f445aec5 mod_http_admin_api: add support for updating groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4372
diff changeset
   495
		if not update then
29b7f445aec5 mod_http_admin_api: add support for updating groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4372
diff changeset
   496
			return 400;
29b7f445aec5 mod_http_admin_api: add support for updating groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4372
diff changeset
   497
		end
29b7f445aec5 mod_http_admin_api: add support for updating groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4372
diff changeset
   498
29b7f445aec5 mod_http_admin_api: add support for updating groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4372
diff changeset
   499
		local group_info = group_info_store:get(group_id);
29b7f445aec5 mod_http_admin_api: add support for updating groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4372
diff changeset
   500
		if not group_info then
29b7f445aec5 mod_http_admin_api: add support for updating groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4372
diff changeset
   501
			return 404;
29b7f445aec5 mod_http_admin_api: add support for updating groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4372
diff changeset
   502
		end
29b7f445aec5 mod_http_admin_api: add support for updating groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4372
diff changeset
   503
29b7f445aec5 mod_http_admin_api: add support for updating groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4372
diff changeset
   504
		if update.name then
29b7f445aec5 mod_http_admin_api: add support for updating groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4372
diff changeset
   505
			group_info["name"] = update.name
29b7f445aec5 mod_http_admin_api: add support for updating groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4372
diff changeset
   506
		end
29b7f445aec5 mod_http_admin_api: add support for updating groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4372
diff changeset
   507
		group_info_store:set(group_id, group_info);
29b7f445aec5 mod_http_admin_api: add support for updating groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4372
diff changeset
   508
		return 204;
4372
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   509
	end
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   510
	return 400;
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   511
end
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   512
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   513
function delete_group(event, subpath) --luacheck: ignore 212/event
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   514
	-- Check if this is a membership deletion and handle it
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   515
	local group_id, member_name = subpath:match("^([^/]+)/members/([^/]+)$");
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   516
	if group_id and member_name then
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   517
		if not group_info_store:get(group_id) then
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   518
			return 404;
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   519
		end
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   520
		if group_memberships:set(group_id, member_name, nil) then
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   521
			return 200;
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   522
		else
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   523
			return 500;
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   524
		end
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   525
	else
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   526
		group_id = subpath;
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   527
	end
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   528
4357
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   529
	if not group_id then
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   530
		return 400;
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   531
	end
4372
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   532
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   533
	if not group_info_store:get(group_id) then
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   534
		return 404;
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   535
	end
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   536
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   537
	if not group_members_store:set(group_id, nil) then
4357
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   538
		return 500;
4372
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   539
	else
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   540
		if not group_info_store:set(group_id, nil) then
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   541
			return 500;
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   542
		end
4357
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   543
	end
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   544
	return 200;
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   545
end
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   546
4375
3d01bc4547b2 mod_http_admin_api: Add /server/info endpoint for site_name and version
Matthew Wild <mwild1@gmail.com>
parents: 4373
diff changeset
   547
local function get_server_info(event)
3d01bc4547b2 mod_http_admin_api: Add /server/info endpoint for site_name and version
Matthew Wild <mwild1@gmail.com>
parents: 4373
diff changeset
   548
	event.response.headers["Content-Type"] = json_content_type;
3d01bc4547b2 mod_http_admin_api: Add /server/info endpoint for site_name and version
Matthew Wild <mwild1@gmail.com>
parents: 4373
diff changeset
   549
	return json.encode({
3d01bc4547b2 mod_http_admin_api: Add /server/info endpoint for site_name and version
Matthew Wild <mwild1@gmail.com>
parents: 4373
diff changeset
   550
		site_name = site_name;
3d01bc4547b2 mod_http_admin_api: Add /server/info endpoint for site_name and version
Matthew Wild <mwild1@gmail.com>
parents: 4373
diff changeset
   551
		version = prosody.version;
3d01bc4547b2 mod_http_admin_api: Add /server/info endpoint for site_name and version
Matthew Wild <mwild1@gmail.com>
parents: 4373
diff changeset
   552
	});
3d01bc4547b2 mod_http_admin_api: Add /server/info endpoint for site_name and version
Matthew Wild <mwild1@gmail.com>
parents: 4373
diff changeset
   553
end
3d01bc4547b2 mod_http_admin_api: Add /server/info endpoint for site_name and version
Matthew Wild <mwild1@gmail.com>
parents: 4373
diff changeset
   554
4347
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   555
module:provides("http", {
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   556
	route = check_auth {
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   557
		["GET /invites"] = list_invites;
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   558
		["GET /invites/*"] = get_invite_by_id;
4378
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   559
		["POST /invites"] = create_invite; -- Deprecated
e707810a943e mod_http_admin_api: Improve invite API and support password resets
Matthew Wild <mwild1@gmail.com>
parents: 4375
diff changeset
   560
		["POST /invites/*"] = create_invite_type;
4347
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   561
		["DELETE /invites/*"] = delete_invite;
4349
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   562
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   563
		["GET /users"] = list_users;
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   564
		["GET /users/*"] = get_user_by_name;
1bb08e9ffa82 mod_http_admin_api: Add methods for managing users
Matthew Wild <mwild1@gmail.com>
parents: 4347
diff changeset
   565
		["DELETE /users/*"] = delete_user;
4357
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   566
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   567
		["GET /groups"] = list_groups;
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   568
		["GET /groups/*"] = get_group_by_id;
4364
76bec3f66b24 mod_http_admin_api: Switch PUT to POST where appropriate
Matthew Wild <mwild1@gmail.com>
parents: 4363
diff changeset
   569
		["POST /groups"] = create_group;
4372
e0c8d866d58c mod_http_admin_api: Some fixes and improvements for the groups API
Matthew Wild <mwild1@gmail.com>
parents: 4370
diff changeset
   570
		["PUT /groups/*"] = update_group;
4357
535d80be110d mod_http_admin_api: Add groups management endpoints
Matthew Wild <mwild1@gmail.com>
parents: 4356
diff changeset
   571
		["DELETE /groups/*"] = delete_group;
4375
3d01bc4547b2 mod_http_admin_api: Add /server/info endpoint for site_name and version
Matthew Wild <mwild1@gmail.com>
parents: 4373
diff changeset
   572
3d01bc4547b2 mod_http_admin_api: Add /server/info endpoint for site_name and version
Matthew Wild <mwild1@gmail.com>
parents: 4373
diff changeset
   573
		["GET /server/info"] = get_server_info;
4347
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   574
	};
ee313922b8d1 mod_http_admin_api: HTTP API for managing users and invites
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   575
});