2019-02-08 07:29:14 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
2019-02-21 21:52:07 +01:00
|
|
|
// Copyright (C) 2016-2019 Jason Volk <jason@zemos.net>
|
2019-02-08 07:29:14 +01:00
|
|
|
//
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
mapi::header
|
|
|
|
IRCD_MODULE
|
|
|
|
{
|
|
|
|
"Federation 21 :End-to-End Encryption"
|
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource
|
2019-02-08 07:29:14 +01:00
|
|
|
user_keys_query_resource
|
|
|
|
{
|
|
|
|
"/_matrix/federation/v1/user/keys/query",
|
|
|
|
{
|
|
|
|
"federation user keys query",
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-22 01:29:32 +01:00
|
|
|
static void
|
2020-04-02 03:30:02 +02:00
|
|
|
_query_user_device(client &,
|
|
|
|
const m::resource::request &,
|
|
|
|
const m::user::devices &,
|
2019-02-21 21:52:07 +01:00
|
|
|
const string_view &device_id,
|
|
|
|
json::stack::object &out);
|
2019-02-08 07:29:14 +01:00
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
static m::resource::response
|
2019-02-21 21:52:07 +01:00
|
|
|
post__user_keys_query(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request);
|
2019-02-08 07:29:14 +01:00
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::method
|
2019-02-08 07:29:14 +01:00
|
|
|
user_keys_query__post
|
|
|
|
{
|
2019-02-09 02:55:35 +01:00
|
|
|
user_keys_query_resource, "POST", post__user_keys_query,
|
2019-02-08 07:29:14 +01:00
|
|
|
{
|
|
|
|
user_keys_query__post.VERIFY_ORIGIN
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::response
|
2019-02-08 07:29:14 +01:00
|
|
|
post__user_keys_query(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request)
|
2019-02-08 07:29:14 +01:00
|
|
|
{
|
2019-02-21 21:44:21 +01:00
|
|
|
const json::object &request_keys
|
2019-02-08 07:29:14 +01:00
|
|
|
{
|
2019-02-21 21:44:21 +01:00
|
|
|
request.at("device_keys")
|
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::response::chunked response
|
2019-02-21 21:44:21 +01:00
|
|
|
{
|
|
|
|
client, http::OK
|
2019-02-08 07:29:14 +01:00
|
|
|
};
|
2019-02-21 21:44:21 +01:00
|
|
|
|
|
|
|
json::stack out
|
|
|
|
{
|
|
|
|
response.buf, response.flusher()
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::object top{out};
|
|
|
|
json::stack::object response_keys
|
|
|
|
{
|
|
|
|
top, "device_keys"
|
|
|
|
};
|
|
|
|
|
2020-04-02 03:30:02 +02:00
|
|
|
for(const auto &[user_id_, device_ids_] : request_keys)
|
2019-02-21 21:44:21 +01:00
|
|
|
{
|
2020-04-02 03:30:02 +02:00
|
|
|
const m::user::id &user_id
|
|
|
|
{
|
|
|
|
user_id_
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::array &device_ids
|
|
|
|
{
|
|
|
|
device_ids_
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::user::devices devices
|
|
|
|
{
|
|
|
|
user_id
|
|
|
|
};
|
|
|
|
|
2019-02-21 21:44:21 +01:00
|
|
|
json::stack::object response_keys_user
|
|
|
|
{
|
|
|
|
response_keys, user_id
|
|
|
|
};
|
|
|
|
|
|
|
|
if(empty(device_ids))
|
2020-04-02 03:30:02 +02:00
|
|
|
devices.for_each([&client, &request, &devices, &response_keys_user]
|
|
|
|
(const auto &event_idx, const string_view &device_id)
|
2019-02-21 21:44:21 +01:00
|
|
|
{
|
2020-04-02 03:30:02 +02:00
|
|
|
_query_user_device(client, request, devices, device_id, response_keys_user);
|
2019-02-21 21:44:21 +01:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
else
|
|
|
|
for(const json::string &device_id : device_ids)
|
2020-04-02 03:30:02 +02:00
|
|
|
_query_user_device(client, request, devices, device_id, response_keys_user);
|
2019-02-21 21:44:21 +01:00
|
|
|
}
|
|
|
|
|
2019-06-24 07:09:41 +02:00
|
|
|
return std::move(response);
|
2019-02-21 21:44:21 +01:00
|
|
|
}
|
|
|
|
|
2019-02-22 01:29:32 +01:00
|
|
|
void
|
2019-02-21 21:44:21 +01:00
|
|
|
_query_user_device(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request,
|
2020-04-02 03:30:02 +02:00
|
|
|
const m::user::devices &devices,
|
2019-02-21 21:44:21 +01:00
|
|
|
const string_view &device_id,
|
|
|
|
json::stack::object &out)
|
|
|
|
{
|
2020-04-02 03:30:02 +02:00
|
|
|
if(!devices.has(device_id, "keys"))
|
2019-03-10 03:48:59 +01:00
|
|
|
return;
|
|
|
|
|
2019-02-22 01:29:32 +01:00
|
|
|
json::stack::object object
|
|
|
|
{
|
|
|
|
out, device_id
|
|
|
|
};
|
|
|
|
|
2020-04-02 03:30:02 +02:00
|
|
|
devices.get(std::nothrow, device_id, "keys", [&device_id, &object]
|
|
|
|
(const auto &event_idx, const json::object &device_keys)
|
2019-02-21 21:44:21 +01:00
|
|
|
{
|
2019-02-22 01:29:32 +01:00
|
|
|
for(const auto &member : device_keys)
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
object, member.first, member.second
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-04-02 03:30:02 +02:00
|
|
|
devices.get(std::nothrow, device_id, "display_name", [&device_id, &object]
|
|
|
|
(const auto &event_idx, const string_view &display_name)
|
2019-02-22 01:29:32 +01:00
|
|
|
{
|
|
|
|
json::stack::object non_hancock
|
|
|
|
{
|
|
|
|
object, "unsigned"
|
|
|
|
};
|
|
|
|
|
2019-02-21 21:44:21 +01:00
|
|
|
json::stack::member
|
|
|
|
{
|
2019-02-22 01:29:32 +01:00
|
|
|
non_hancock, "device_display_name", display_name
|
2019-02-21 21:44:21 +01:00
|
|
|
};
|
|
|
|
});
|
2019-02-08 07:29:14 +01:00
|
|
|
}
|