2018-02-15 06:56:47 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
|
|
|
using namespace ircd;
|
|
|
|
|
2019-03-11 18:41:28 +01:00
|
|
|
static void
|
2020-04-02 03:30:02 +02:00
|
|
|
_get_device(json::stack::object &,
|
|
|
|
const m::user::devices &,
|
2019-03-11 18:41:28 +01:00
|
|
|
const string_view &device_id);
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
static m::resource::response
|
2019-03-11 18:41:28 +01:00
|
|
|
get__devices_all(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request,
|
2019-03-11 18:41:28 +01:00
|
|
|
const m::room user_room);
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
static m::resource::response
|
2019-03-11 18:41:28 +01:00
|
|
|
get__devices(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request);
|
2019-03-11 18:41:28 +01:00
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
static m::resource::response
|
2019-03-11 18:41:28 +01:00
|
|
|
put__devices(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request);
|
2019-03-11 18:41:28 +01:00
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
static m::resource::response
|
2019-03-11 18:41:28 +01:00
|
|
|
delete__devices(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request);
|
2019-03-11 18:41:28 +01:00
|
|
|
|
|
|
|
extern const std::string flows;
|
|
|
|
|
2018-02-15 06:56:47 +01:00
|
|
|
mapi::header
|
|
|
|
IRCD_MODULE
|
|
|
|
{
|
2018-02-15 22:06:49 +01:00
|
|
|
"Client 11.9 Device Management"
|
2018-02-15 06:56:47 +01:00
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
ircd::m::resource
|
2018-02-15 06:56:47 +01:00
|
|
|
devices_resource
|
|
|
|
{
|
|
|
|
"/_matrix/client/r0/devices/",
|
|
|
|
{
|
|
|
|
"(11.9) Device Management",
|
|
|
|
resource::DIRECTORY,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::method
|
2019-03-11 18:41:28 +01:00
|
|
|
method_get
|
2019-02-20 22:09:32 +01:00
|
|
|
{
|
2019-03-11 18:41:28 +01:00
|
|
|
devices_resource, "GET", get__devices,
|
2019-02-20 22:09:32 +01:00
|
|
|
{
|
2019-03-11 18:41:28 +01:00
|
|
|
method_get.REQUIRES_AUTH
|
|
|
|
}
|
|
|
|
};
|
2019-02-20 22:09:32 +01:00
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::method
|
2019-03-11 18:41:28 +01:00
|
|
|
method_delete
|
|
|
|
{
|
|
|
|
devices_resource, "DELETE", delete__devices,
|
2018-02-15 06:56:47 +01:00
|
|
|
{
|
2019-03-11 18:41:28 +01:00
|
|
|
method_delete.REQUIRES_AUTH
|
|
|
|
}
|
|
|
|
};
|
2018-02-15 06:56:47 +01:00
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::method
|
2019-03-11 18:41:28 +01:00
|
|
|
method_put
|
|
|
|
{
|
|
|
|
devices_resource, "PUT", put__devices,
|
2018-02-15 06:56:47 +01:00
|
|
|
{
|
2019-03-11 18:41:28 +01:00
|
|
|
method_put.REQUIRES_AUTH
|
|
|
|
}
|
|
|
|
};
|
2019-02-20 02:42:17 +01:00
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::response
|
2018-02-15 06:56:47 +01:00
|
|
|
get__devices(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request)
|
2018-02-15 06:56:47 +01:00
|
|
|
{
|
2018-02-16 02:27:42 +01:00
|
|
|
const m::user::room user_room
|
2018-02-15 06:56:47 +01:00
|
|
|
{
|
|
|
|
request.user_id
|
|
|
|
};
|
|
|
|
|
|
|
|
if(request.parv.size() < 1)
|
|
|
|
return get__devices_all(client, request, user_room);
|
|
|
|
|
2018-02-16 02:27:42 +01:00
|
|
|
m::id::device::buf device_id
|
2018-02-15 06:56:47 +01:00
|
|
|
{
|
2019-02-20 22:09:32 +01:00
|
|
|
url::decode(device_id, request.parv[0])
|
2018-02-15 06:56:47 +01:00
|
|
|
};
|
|
|
|
|
2020-04-02 03:30:02 +02:00
|
|
|
const m::user::devices devices
|
|
|
|
{
|
|
|
|
request.user_id
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!devices.has(device_id))
|
2019-02-20 22:09:32 +01:00
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"Device ID '%s' not found", device_id
|
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::response::chunked response
|
2018-02-15 06:56:47 +01:00
|
|
|
{
|
2019-02-20 22:09:32 +01:00
|
|
|
client, http::OK
|
|
|
|
};
|
2018-02-15 06:56:47 +01:00
|
|
|
|
2019-02-20 22:09:32 +01:00
|
|
|
json::stack out
|
|
|
|
{
|
|
|
|
response.buf, response.flusher()
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::object top
|
2019-02-20 02:42:17 +01:00
|
|
|
{
|
2019-02-20 22:09:32 +01:00
|
|
|
out
|
2019-02-20 02:42:17 +01:00
|
|
|
};
|
2019-02-20 22:09:32 +01:00
|
|
|
|
2020-04-02 03:30:02 +02:00
|
|
|
_get_device(top, devices, device_id);
|
2019-02-20 22:09:32 +01:00
|
|
|
return {};
|
2018-02-15 06:56:47 +01:00
|
|
|
}
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::response
|
2018-02-15 06:56:47 +01:00
|
|
|
put__devices(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request)
|
2018-02-15 06:56:47 +01:00
|
|
|
{
|
|
|
|
if(request.parv.size() < 1)
|
|
|
|
throw m::NEED_MORE_PARAMS
|
|
|
|
{
|
|
|
|
"device_id required"
|
|
|
|
};
|
|
|
|
|
2018-02-16 02:27:42 +01:00
|
|
|
m::id::device::buf device_id
|
2018-02-15 06:56:47 +01:00
|
|
|
{
|
2020-03-29 00:43:10 +01:00
|
|
|
url::decode(device_id, request.parv[0])
|
2018-02-15 06:56:47 +01:00
|
|
|
};
|
|
|
|
|
2020-04-02 03:30:02 +02:00
|
|
|
const m::user::devices devices
|
|
|
|
{
|
|
|
|
request.user_id
|
|
|
|
};
|
|
|
|
|
2019-02-20 22:09:32 +01:00
|
|
|
m::device data{request.content};
|
|
|
|
json::get<"device_id"_>(data) = device_id;
|
2018-02-15 06:56:47 +01:00
|
|
|
|
2020-04-02 03:30:02 +02:00
|
|
|
const bool set
|
|
|
|
{
|
|
|
|
devices.set(data)
|
|
|
|
};
|
2018-02-15 06:56:47 +01:00
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
return m::resource::response
|
2018-02-15 06:56:47 +01:00
|
|
|
{
|
|
|
|
client, http::OK
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::response
|
2018-02-15 06:56:47 +01:00
|
|
|
delete__devices(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request)
|
2018-02-15 06:56:47 +01:00
|
|
|
{
|
|
|
|
if(request.parv.size() < 1)
|
|
|
|
throw m::NEED_MORE_PARAMS
|
|
|
|
{
|
|
|
|
"device_id required"
|
|
|
|
};
|
|
|
|
|
2018-02-16 02:27:42 +01:00
|
|
|
m::id::device::buf device_id
|
2018-02-15 06:56:47 +01:00
|
|
|
{
|
2020-03-29 00:43:10 +01:00
|
|
|
url::decode(device_id, request.parv[0])
|
2018-02-15 06:56:47 +01:00
|
|
|
};
|
|
|
|
|
2019-02-21 00:45:32 +01:00
|
|
|
const json::object &auth
|
|
|
|
{
|
|
|
|
request["auth"]
|
|
|
|
};
|
|
|
|
|
|
|
|
// 14.10.2 Security considerations
|
|
|
|
const json::string &type{auth["type"]};
|
|
|
|
if(type != "m.login.password")
|
2019-09-29 01:12:07 +02:00
|
|
|
return m::resource::response
|
2019-02-21 00:45:32 +01:00
|
|
|
{
|
|
|
|
client, http::UNAUTHORIZED, json::object{flows}
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::string &password{auth["password"]};
|
|
|
|
if(!m::user(request.user_id).is_password(password))
|
|
|
|
throw m::ACCESS_DENIED
|
|
|
|
{
|
|
|
|
"Incorrect password."
|
|
|
|
};
|
|
|
|
|
2023-03-03 21:13:47 +01:00
|
|
|
const m::user::tokens tokens
|
|
|
|
{
|
|
|
|
request.user_id
|
|
|
|
};
|
|
|
|
|
|
|
|
const size_t revoked
|
|
|
|
{
|
|
|
|
tokens.del_by_device(device_id)
|
|
|
|
};
|
|
|
|
|
2020-04-02 03:30:02 +02:00
|
|
|
const m::user::devices devices
|
|
|
|
{
|
|
|
|
request.user_id
|
|
|
|
};
|
|
|
|
|
|
|
|
const bool deleted
|
|
|
|
{
|
|
|
|
devices.del(device_id)
|
|
|
|
};
|
2018-02-15 06:56:47 +01:00
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
return m::resource::response
|
2018-02-15 06:56:47 +01:00
|
|
|
{
|
2019-02-20 02:42:17 +01:00
|
|
|
client, http::OK
|
2018-02-15 06:56:47 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::response
|
2019-03-11 18:41:28 +01:00
|
|
|
get__devices_all(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request,
|
2019-03-11 18:41:28 +01:00
|
|
|
const m::room user_room)
|
2018-02-15 06:56:47 +01:00
|
|
|
{
|
2020-04-02 03:30:02 +02:00
|
|
|
const m::user::devices user_devices
|
|
|
|
{
|
|
|
|
request.user_id
|
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::response::chunked response
|
2018-02-15 06:56:47 +01:00
|
|
|
{
|
2019-03-11 18:41:28 +01:00
|
|
|
client, http::OK
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack out
|
|
|
|
{
|
|
|
|
response.buf, response.flusher()
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::object top
|
|
|
|
{
|
|
|
|
out
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::array devices
|
|
|
|
{
|
|
|
|
top, "devices"
|
|
|
|
};
|
|
|
|
|
2020-04-02 03:30:02 +02:00
|
|
|
user_devices.for_each([&user_devices, &devices]
|
|
|
|
(const auto &event_idx, const string_view &device_id)
|
2019-03-11 18:41:28 +01:00
|
|
|
{
|
2020-04-02 03:30:02 +02:00
|
|
|
json::stack::object obj
|
|
|
|
{
|
|
|
|
devices
|
|
|
|
};
|
|
|
|
|
|
|
|
_get_device(obj, user_devices, device_id);
|
2019-03-11 18:41:28 +01:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_get_device(json::stack::object &obj,
|
2020-04-02 03:30:02 +02:00
|
|
|
const m::user::devices &devices,
|
2019-03-11 18:41:28 +01:00
|
|
|
const string_view &device_id)
|
|
|
|
{
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
obj, "device_id", device_id
|
|
|
|
};
|
|
|
|
|
2023-04-22 02:32:03 +02:00
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
obj, "user_id", devices.user.user_id
|
|
|
|
};
|
|
|
|
|
2020-04-02 03:30:02 +02:00
|
|
|
devices.get(std::nothrow, device_id, "display_name", [&obj]
|
|
|
|
(const auto &, const string_view &value)
|
2019-03-11 18:41:28 +01:00
|
|
|
{
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
obj, "display_name", unquote(value)
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-04-02 03:30:02 +02:00
|
|
|
devices.get(std::nothrow, device_id, "last_seen_ip", [&obj]
|
|
|
|
(const auto &, const string_view &value)
|
2019-03-11 18:41:28 +01:00
|
|
|
{
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
obj, "last_seen_ip", unquote(value)
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-04-02 03:30:02 +02:00
|
|
|
devices.get(std::nothrow, device_id, "last_seen_ts", [&obj]
|
|
|
|
(const auto &, const string_view &value)
|
2019-03-11 18:41:28 +01:00
|
|
|
{
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
obj, "last_seen_ts", value
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
2019-02-21 00:45:32 +01:00
|
|
|
|
|
|
|
const std::string
|
|
|
|
flows
|
|
|
|
{
|
|
|
|
ircd::string(512 | SHRINK_TO_FIT, [](const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
json::stack out{buf};
|
|
|
|
{
|
|
|
|
json::stack::object top{out};
|
|
|
|
json::stack::array flows{top, "flows"};
|
|
|
|
json::stack::object flow{flows};
|
|
|
|
json::stack::array stages{flow, "stages"};
|
|
|
|
stages.append("m.login.password");
|
|
|
|
}
|
|
|
|
|
|
|
|
return out.completed();
|
|
|
|
})
|
|
|
|
};
|