2019-02-21 03:34:22 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2019 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
|
|
|
|
upload_device_keys(client &,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &,
|
2019-03-11 18:41:28 +01:00
|
|
|
const m::device::id &,
|
|
|
|
const m::device_keys &);
|
|
|
|
|
2019-08-09 07:44:54 +02:00
|
|
|
static void
|
|
|
|
upload_one_time_keys(client &,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &,
|
2019-08-09 07:44:54 +02:00
|
|
|
const m::device::id &,
|
|
|
|
const json::object &);
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
static m::resource::response
|
2019-03-11 18:41:28 +01:00
|
|
|
post__keys_upload(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request);
|
2019-03-11 18:41:28 +01:00
|
|
|
|
2019-02-21 03:34:22 +01:00
|
|
|
mapi::header
|
|
|
|
IRCD_MODULE
|
|
|
|
{
|
|
|
|
"Client 14.11.5.2 :Key management API"
|
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
ircd::m::resource
|
2019-02-21 03:34:22 +01:00
|
|
|
upload_resource
|
|
|
|
{
|
|
|
|
"/_matrix/client/r0/keys/upload",
|
|
|
|
{
|
|
|
|
"(14.11.5.2.1) Keys Upload",
|
|
|
|
resource::DIRECTORY
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
ircd::m::resource
|
2019-02-21 03:34:22 +01:00
|
|
|
upload_resource__unstable
|
|
|
|
{
|
|
|
|
"/_matrix/client/unstable/keys/upload",
|
|
|
|
{
|
|
|
|
"(14.11.5.2.1) Keys Upload",
|
|
|
|
resource::DIRECTORY
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::method
|
2019-03-11 18:41:28 +01:00
|
|
|
method_post
|
|
|
|
{
|
|
|
|
upload_resource, "POST", post__keys_upload,
|
|
|
|
{
|
|
|
|
method_post.REQUIRES_AUTH
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::method
|
2019-03-11 18:41:28 +01:00
|
|
|
method_post__unstable
|
|
|
|
{
|
|
|
|
upload_resource__unstable, "POST", post__keys_upload,
|
|
|
|
{
|
|
|
|
method_post.REQUIRES_AUTH
|
|
|
|
}
|
|
|
|
};
|
2019-02-21 03:34:22 +01:00
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::response
|
2019-02-21 03:34:22 +01:00
|
|
|
post__keys_upload(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request)
|
2019-02-21 03:34:22 +01:00
|
|
|
{
|
|
|
|
const m::user::room user_room
|
|
|
|
{
|
|
|
|
request.user_id
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::object &device_keys
|
|
|
|
{
|
|
|
|
request["device_keys"]
|
|
|
|
};
|
|
|
|
|
2019-02-25 01:12:30 +01:00
|
|
|
const m::device::id::buf device_id
|
|
|
|
{
|
2020-04-02 02:14:51 +02:00
|
|
|
m::user::tokens::device(request.access_token)
|
2019-02-25 01:12:30 +01:00
|
|
|
};
|
|
|
|
|
2019-02-21 03:34:22 +01:00
|
|
|
if(!empty(device_keys))
|
2019-02-25 01:12:30 +01:00
|
|
|
upload_device_keys(client, request, device_id, device_keys);
|
2019-02-21 03:34:22 +01:00
|
|
|
|
|
|
|
const json::object &one_time_keys
|
|
|
|
{
|
|
|
|
request["one_time_keys"]
|
|
|
|
};
|
|
|
|
|
2019-08-09 07:44:54 +02:00
|
|
|
if(!empty(one_time_keys))
|
|
|
|
upload_one_time_keys(client, request, device_id, one_time_keys);
|
2019-02-25 01:12:30 +01:00
|
|
|
|
2019-08-09 07:44:54 +02:00
|
|
|
const unique_buffer<mutable_buffer> buf
|
2019-02-21 03:34:22 +01:00
|
|
|
{
|
2019-08-09 07:44:54 +02:00
|
|
|
32_KiB
|
|
|
|
};
|
2019-08-07 04:22:11 +02:00
|
|
|
|
2019-08-09 07:44:54 +02:00
|
|
|
json::stack out{buf};
|
|
|
|
json::stack::object top{out};
|
|
|
|
json::stack::object one_time_key_counts
|
|
|
|
{
|
|
|
|
top, "one_time_key_counts"
|
|
|
|
};
|
2019-08-07 04:22:11 +02:00
|
|
|
|
2019-08-09 07:44:54 +02:00
|
|
|
const auto counts
|
|
|
|
{
|
2020-04-02 03:30:02 +02:00
|
|
|
m::user::devices::count_one_time_keys(request.user_id, device_id)
|
2019-08-09 07:44:54 +02:00
|
|
|
};
|
2019-08-07 04:22:11 +02:00
|
|
|
|
2019-08-09 07:44:54 +02:00
|
|
|
for(const auto &[algorithm, count] : counts)
|
|
|
|
json::stack::member
|
2019-08-07 04:22:11 +02:00
|
|
|
{
|
2019-08-09 07:44:54 +02:00
|
|
|
one_time_key_counts, algorithm, json::value{count}
|
2019-08-07 04:22:11 +02:00
|
|
|
};
|
|
|
|
|
2019-08-09 07:44:54 +02:00
|
|
|
one_time_key_counts.~object();
|
|
|
|
top.~object();
|
2019-09-29 01:12:07 +02:00
|
|
|
return m::resource::response
|
2019-08-09 07:44:54 +02:00
|
|
|
{
|
|
|
|
client, json::object
|
2019-02-25 01:12:30 +01:00
|
|
|
{
|
2019-08-09 07:44:54 +02:00
|
|
|
out.completed()
|
2019-02-25 01:12:30 +01:00
|
|
|
}
|
2019-02-21 03:34:22 +01:00
|
|
|
};
|
2019-08-09 07:44:54 +02:00
|
|
|
}
|
2019-02-21 03:34:22 +01:00
|
|
|
|
2019-08-09 07:44:54 +02:00
|
|
|
void
|
|
|
|
upload_one_time_keys(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request,
|
2019-08-09 07:44:54 +02:00
|
|
|
const m::device::id &device_id,
|
|
|
|
const json::object &one_time_keys)
|
|
|
|
{
|
2020-04-02 03:30:02 +02:00
|
|
|
const m::user::devices devices
|
|
|
|
{
|
|
|
|
request.user_id
|
|
|
|
};
|
|
|
|
|
2019-08-09 07:44:54 +02:00
|
|
|
for(const auto &[ident, object] : one_time_keys)
|
2019-02-21 03:34:22 +01:00
|
|
|
{
|
2019-08-09 07:44:54 +02:00
|
|
|
const auto &[algorithm, name]
|
2019-02-21 03:34:22 +01:00
|
|
|
{
|
2019-08-09 07:44:54 +02:00
|
|
|
split(ident, ':')
|
2019-02-25 01:12:30 +01:00
|
|
|
};
|
|
|
|
|
2019-08-09 07:44:54 +02:00
|
|
|
if(empty(algorithm) || empty(name))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
char state_key_buf[128]
|
2019-08-07 04:22:11 +02:00
|
|
|
{
|
2019-08-09 07:44:54 +02:00
|
|
|
"one_time_key|"
|
|
|
|
};
|
2019-02-25 01:12:30 +01:00
|
|
|
|
2019-08-09 07:44:54 +02:00
|
|
|
const string_view state_key
|
|
|
|
{
|
2020-12-22 18:16:52 +01:00
|
|
|
ircd::strlcat(state_key_buf, ident)
|
2019-08-09 07:44:54 +02:00
|
|
|
};
|
|
|
|
|
2020-04-02 03:30:02 +02:00
|
|
|
const auto set
|
|
|
|
{
|
|
|
|
devices.set(device_id, state_key, object)
|
|
|
|
};
|
2019-08-09 07:44:54 +02:00
|
|
|
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
m::log, "Received one_time_key:%s for %s on %s",
|
|
|
|
ident,
|
|
|
|
string_view{device_id},
|
|
|
|
string_view{request.user_id},
|
|
|
|
};
|
|
|
|
}
|
2019-02-21 03:34:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
upload_device_keys(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request,
|
2019-02-25 01:12:30 +01:00
|
|
|
const m::device::id &device_id,
|
2019-02-21 03:34:22 +01:00
|
|
|
const m::device_keys &device_keys)
|
|
|
|
{
|
|
|
|
if(at<"user_id"_>(device_keys) != request.user_id)
|
|
|
|
throw m::FORBIDDEN
|
|
|
|
{
|
|
|
|
"client 14.11.5.2.1: device_keys.user_id: "
|
|
|
|
"Must match the user_id used when logging in."
|
|
|
|
};
|
|
|
|
|
|
|
|
if(at<"device_id"_>(device_keys) != device_id)
|
|
|
|
throw m::FORBIDDEN
|
|
|
|
{
|
|
|
|
"client 14.11.5.2.1: device_keys.device_id: "
|
|
|
|
"Must match the device_id used when logging in."
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::array &algorithms
|
|
|
|
{
|
|
|
|
at<"algorithms"_>(device_keys)
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::object &keys
|
|
|
|
{
|
|
|
|
at<"keys"_>(device_keys)
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::object &signatures
|
|
|
|
{
|
|
|
|
at<"signatures"_>(device_keys)
|
|
|
|
};
|
2019-02-21 04:19:54 +01:00
|
|
|
|
2020-04-02 03:30:02 +02:00
|
|
|
const m::user::devices devices
|
|
|
|
{
|
|
|
|
request.user_id
|
|
|
|
};
|
|
|
|
|
2019-02-21 04:19:54 +01:00
|
|
|
m::device data;
|
|
|
|
json::get<"device_id"_>(data) = device_id;
|
|
|
|
json::get<"keys"_>(data) = request["device_keys"];
|
2020-04-02 03:30:02 +02:00
|
|
|
const auto set
|
|
|
|
{
|
|
|
|
devices.set(data)
|
|
|
|
};
|
2019-02-21 03:34:22 +01:00
|
|
|
}
|