2018-02-04 03:22:01 +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.
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-03-21 03:28:00 +01:00
|
|
|
using namespace ircd;
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2018-02-15 22:06:49 +01:00
|
|
|
mapi::header
|
|
|
|
IRCD_MODULE
|
|
|
|
{
|
|
|
|
"Client 3.4.1 :Register"
|
|
|
|
};
|
|
|
|
|
2019-01-12 21:33:00 +01:00
|
|
|
extern const std::string
|
|
|
|
flows;
|
|
|
|
|
|
|
|
static void
|
|
|
|
validate_password(const string_view &password);
|
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
validate_user_id(const m::id::user &user_id);
|
2018-06-07 22:20:46 +02:00
|
|
|
|
|
|
|
extern "C" std::string
|
|
|
|
register_user(const m::registar &,
|
|
|
|
const client *const & = nullptr,
|
|
|
|
const bool &gen_token = false);
|
|
|
|
|
|
|
|
static resource::response
|
|
|
|
post__register_guest(client &client, const resource::request::object<m::registar> &request);
|
|
|
|
|
|
|
|
static resource::response
|
|
|
|
post__register_user(client &client, const resource::request::object<m::registar> &request);
|
|
|
|
|
|
|
|
static resource::response
|
|
|
|
post__register(client &client, const resource::request::object<m::registar> &request);
|
|
|
|
|
|
|
|
resource
|
|
|
|
register_resource
|
|
|
|
{
|
|
|
|
"/_matrix/client/r0/register",
|
|
|
|
{
|
|
|
|
"(3.3.1) Register for an account on this homeserver."
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
resource::method
|
|
|
|
method_post
|
|
|
|
{
|
|
|
|
register_resource, "POST", post__register
|
|
|
|
};
|
|
|
|
|
2018-05-21 03:12:49 +02:00
|
|
|
ircd::conf::item<bool>
|
|
|
|
register_enable
|
|
|
|
{
|
|
|
|
{ "name", "ircd.client.register.enable" },
|
|
|
|
{ "default", true }
|
|
|
|
};
|
|
|
|
|
2018-06-07 22:20:46 +02:00
|
|
|
/// see: ircd/m/register.h for the m::registar tuple.
|
|
|
|
///
|
|
|
|
resource::response
|
|
|
|
post__register(client &client,
|
|
|
|
const resource::request::object<m::registar> &request)
|
|
|
|
{
|
|
|
|
if(!bool(register_enable))
|
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
http::UNAUTHORIZED, "M_REGISTRATION_DISABLED",
|
|
|
|
"Registration for this server is disabled."
|
|
|
|
};
|
|
|
|
|
2019-01-12 21:33:00 +01:00
|
|
|
const json::object &auth
|
|
|
|
{
|
|
|
|
json::get<"auth"_>(request)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(empty(auth))
|
|
|
|
return resource::response
|
|
|
|
{
|
|
|
|
client, http::UNAUTHORIZED, json::object{flows}
|
|
|
|
};
|
|
|
|
|
2018-06-07 22:20:46 +02:00
|
|
|
const auto kind
|
|
|
|
{
|
|
|
|
request.query["kind"]
|
|
|
|
};
|
|
|
|
|
|
|
|
if(kind == "guest")
|
|
|
|
return post__register_guest(client, request);
|
|
|
|
|
|
|
|
if(kind.empty() || kind == "user")
|
|
|
|
return post__register_user(client, request);
|
|
|
|
|
|
|
|
throw m::UNSUPPORTED
|
|
|
|
{
|
|
|
|
"Unknown 'kind' of registration specified in query."
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::conf::item<bool>
|
|
|
|
register_user_enable
|
|
|
|
{
|
|
|
|
{ "name", "ircd.client.register.user.enable" },
|
|
|
|
{ "default", true }
|
|
|
|
};
|
2017-08-23 23:10:28 +02:00
|
|
|
|
2017-09-08 11:32:49 +02:00
|
|
|
resource::response
|
2018-02-17 02:17:18 +01:00
|
|
|
post__register_user(client &client,
|
2018-03-05 12:05:59 +01:00
|
|
|
const resource::request::object<m::registar> &request)
|
2017-11-16 02:48:25 +01:00
|
|
|
try
|
2017-09-08 11:32:49 +02:00
|
|
|
{
|
2018-06-07 22:20:46 +02:00
|
|
|
if(!bool(register_user_enable))
|
2018-05-21 03:12:49 +02:00
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
http::UNAUTHORIZED, "M_REGISTRATION_DISABLED",
|
2018-06-07 22:20:46 +02:00
|
|
|
"User registration for this server is disabled."
|
2018-05-21 03:12:49 +02:00
|
|
|
};
|
|
|
|
|
2019-01-12 21:34:34 +01:00
|
|
|
const bool &inhibit_login
|
|
|
|
{
|
|
|
|
json::get<"inhibit_login"_>(request)
|
|
|
|
};
|
|
|
|
|
2018-06-07 22:20:46 +02:00
|
|
|
const std::string response
|
|
|
|
{
|
2019-01-12 21:34:34 +01:00
|
|
|
register_user(request, &client, !inhibit_login)
|
2018-06-07 22:20:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Send response to user
|
|
|
|
return resource::response
|
|
|
|
{
|
|
|
|
client, http::CREATED, json::object{response}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
catch(const m::INVALID_MXID &e)
|
|
|
|
{
|
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
http::BAD_REQUEST, "M_INVALID_USERNAME",
|
|
|
|
"Not a valid username. Please try again."
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
ircd::conf::item<bool>
|
|
|
|
register_guest_enable
|
|
|
|
{
|
|
|
|
{ "name", "ircd.client.register.guest.enable" },
|
|
|
|
{ "default", true }
|
|
|
|
};
|
|
|
|
|
|
|
|
resource::response
|
|
|
|
post__register_guest(client &client,
|
|
|
|
const resource::request::object<m::registar> &request)
|
|
|
|
{
|
|
|
|
if(!bool(register_guest_enable))
|
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
http::FORBIDDEN, "M_GUEST_DISABLED",
|
|
|
|
"Guest access is disabled"
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::id::user::buf user_id
|
|
|
|
{
|
|
|
|
m::generate, my_host()
|
|
|
|
};
|
|
|
|
|
|
|
|
char access_token_buf[32];
|
|
|
|
const string_view access_token
|
|
|
|
{
|
|
|
|
m::user::gen_access_token(access_token_buf)
|
|
|
|
};
|
|
|
|
|
|
|
|
return resource::response
|
|
|
|
{
|
|
|
|
client, http::CREATED,
|
|
|
|
{
|
|
|
|
{ "user_id", user_id },
|
|
|
|
{ "home_server", my_host() },
|
|
|
|
{ "access_token", access_token },
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
register_user(const m::registar &request,
|
|
|
|
const client *const &client,
|
|
|
|
const bool &gen_token)
|
|
|
|
{
|
2017-09-08 11:32:49 +02:00
|
|
|
// 3.3.1 Additional authentication information for the user-interactive authentication API.
|
2018-02-15 22:11:51 +01:00
|
|
|
const json::object &auth
|
2017-09-08 11:32:49 +02:00
|
|
|
{
|
2018-01-29 20:36:39 +01:00
|
|
|
json::get<"auth"_>(request)
|
2017-09-08 11:32:49 +02:00
|
|
|
};
|
2017-08-23 23:10:28 +02:00
|
|
|
|
2018-02-15 22:11:51 +01:00
|
|
|
// 3.3.1 The login type that the client is attempting to complete.
|
|
|
|
const string_view &type
|
2017-08-23 23:10:28 +02:00
|
|
|
{
|
2018-01-29 20:36:39 +01:00
|
|
|
!empty(auth)? unquote(auth.at("type")) : string_view{}
|
2017-08-23 23:10:28 +02:00
|
|
|
};
|
|
|
|
|
2017-09-25 03:05:42 +02:00
|
|
|
// We only support this for now, for some reason. TODO: XXX
|
2018-01-29 20:36:39 +01:00
|
|
|
if(type && type != "m.login.dummy")
|
2018-02-15 06:54:07 +01:00
|
|
|
throw m::UNSUPPORTED
|
2017-08-23 23:10:28 +02:00
|
|
|
{
|
2018-02-15 06:54:07 +01:00
|
|
|
"Registration '%s' not supported.", type
|
2017-08-23 23:10:28 +02:00
|
|
|
};
|
|
|
|
|
2017-09-08 11:32:49 +02:00
|
|
|
// 3.3.1 The local part of the desired Matrix ID. If omitted, the homeserver MUST
|
|
|
|
// generate a Matrix ID local part.
|
|
|
|
const auto &username
|
2017-08-23 23:10:28 +02:00
|
|
|
{
|
2019-02-19 01:44:28 +01:00
|
|
|
json::get<"username"_>(request)
|
2017-08-23 23:10:28 +02:00
|
|
|
};
|
|
|
|
|
2017-09-25 03:05:42 +02:00
|
|
|
// Generate canonical mxid. The home_server is appended if one is not
|
|
|
|
// specified. We do not generate a user_id here if the local part is not
|
|
|
|
// specified. TODO: isn't that guest reg?
|
2017-09-08 11:32:49 +02:00
|
|
|
const m::id::user::buf user_id
|
|
|
|
{
|
2017-10-01 12:09:28 +02:00
|
|
|
username, my_host()
|
2017-09-08 11:32:49 +02:00
|
|
|
};
|
|
|
|
|
2017-09-25 03:05:42 +02:00
|
|
|
// Check if the the user_id is acceptably formed for this server or throws
|
|
|
|
validate_user_id(user_id);
|
2017-08-23 23:10:28 +02:00
|
|
|
|
2017-09-08 11:32:49 +02:00
|
|
|
// 3.3.1 Required. The desired password for the account.
|
|
|
|
const auto &password
|
2017-08-23 23:10:28 +02:00
|
|
|
{
|
2019-02-19 01:44:28 +01:00
|
|
|
at<"password"_>(request)
|
2017-08-23 23:10:28 +02:00
|
|
|
};
|
|
|
|
|
2018-02-15 22:11:51 +01:00
|
|
|
// (r0.3.0) 3.4.1 ID of the client device. If this does not correspond to a
|
|
|
|
// known client device, a new device will be created. The server will auto-
|
|
|
|
// generate a device_id if this is not specified.
|
|
|
|
const auto requested_device_id
|
|
|
|
{
|
2019-02-19 01:44:28 +01:00
|
|
|
json::get<"device_id"_>(request)
|
2018-02-15 22:11:51 +01:00
|
|
|
};
|
|
|
|
|
2019-01-12 21:34:34 +01:00
|
|
|
const m::id::device::buf device_id
|
2018-02-15 22:11:51 +01:00
|
|
|
{
|
|
|
|
requested_device_id?
|
|
|
|
m::id::device::buf{requested_device_id, my_host()}:
|
2019-01-12 21:34:34 +01:00
|
|
|
gen_token?
|
|
|
|
m::id::device::buf{m::id::generate, my_host()}:
|
|
|
|
m::id::device::buf{}
|
2018-02-15 22:11:51 +01:00
|
|
|
};
|
|
|
|
|
2019-02-19 01:48:52 +01:00
|
|
|
const auto &initial_device_display_name
|
|
|
|
{
|
|
|
|
json::get<"initial_device_display_name"_>(request)
|
|
|
|
};
|
|
|
|
|
2017-09-08 11:32:49 +02:00
|
|
|
// 3.3.1 If true, the server binds the email used for authentication to the
|
|
|
|
// Matrix ID with the ID Server. Defaults to false.
|
|
|
|
const auto &bind_email
|
2017-08-23 23:10:28 +02:00
|
|
|
{
|
2017-10-05 01:40:02 +02:00
|
|
|
get<"bind_email"_>(request, false)
|
2017-08-23 23:10:28 +02:00
|
|
|
};
|
|
|
|
|
2017-09-25 03:05:42 +02:00
|
|
|
// Check if the password is acceptable for this server or throws
|
|
|
|
validate_password(password);
|
|
|
|
|
2018-03-09 18:00:28 +01:00
|
|
|
//TODO: ABA
|
|
|
|
if(exists(user_id))
|
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
http::CONFLICT, "M_USER_IN_USE",
|
|
|
|
"The desired user ID is already in use."
|
|
|
|
};
|
|
|
|
|
|
|
|
//TODO: ABA / TXN
|
2017-09-25 05:47:13 +02:00
|
|
|
// Represent the user
|
|
|
|
m::user user
|
|
|
|
{
|
2018-03-09 18:00:28 +01:00
|
|
|
m::create(user_id)
|
2017-09-25 05:47:13 +02:00
|
|
|
};
|
|
|
|
|
2018-02-11 22:37:00 +01:00
|
|
|
// Activate the account. Underneath this will create a special room
|
|
|
|
// for this user in the form of !@user:host and set a key in !users:host
|
|
|
|
// If the user_id is taken this throws 409 Conflict because those assets
|
|
|
|
// will already exist; otherwise the user is registered after this call.
|
2018-03-09 18:00:28 +01:00
|
|
|
//TODO: ABA / TXN
|
|
|
|
user.activate();
|
2017-03-11 04:31:20 +01:00
|
|
|
|
2017-09-25 05:47:13 +02:00
|
|
|
// Set the password for the account. This issues an ircd.password state
|
2018-02-11 22:37:00 +01:00
|
|
|
// event to the user's room. User will be able to login with
|
|
|
|
// m.login.password
|
2017-09-25 05:47:13 +02:00
|
|
|
user.password(password);
|
|
|
|
|
2018-03-05 13:21:19 +01:00
|
|
|
// Store the options from registration.
|
|
|
|
m::user::room user_room{user};
|
|
|
|
send(user_room, user.user_id, "ircd.account.options", "registration",
|
|
|
|
{
|
|
|
|
{ "bind_email", bind_email },
|
|
|
|
});
|
|
|
|
|
2018-02-15 22:11:51 +01:00
|
|
|
char access_token_buf[32];
|
|
|
|
const string_view access_token
|
|
|
|
{
|
2018-06-07 22:20:46 +02:00
|
|
|
gen_token?
|
|
|
|
m::user::gen_access_token(access_token_buf):
|
|
|
|
string_view{}
|
2018-02-15 22:11:51 +01:00
|
|
|
};
|
|
|
|
|
2019-02-19 01:48:52 +01:00
|
|
|
// Log the user in by issuing an event in the tokens room containing
|
|
|
|
// the generated token. When this call completes without throwing the
|
|
|
|
// access_token will be committed and the user will be logged in.
|
2018-06-07 22:20:46 +02:00
|
|
|
if(gen_token)
|
2019-02-21 00:46:48 +01:00
|
|
|
{
|
|
|
|
const m::event::id::buf access_token_id
|
2018-06-07 22:20:46 +02:00
|
|
|
{
|
2019-02-21 00:46:48 +01:00
|
|
|
m::send(m::user::tokens, user_id, "ircd.access_token", access_token,
|
|
|
|
{
|
2019-02-21 03:46:30 +01:00
|
|
|
{ "ip", client? string(remote(*client)) : std::string{} },
|
|
|
|
{ "device_id", device_id },
|
2019-02-21 00:46:48 +01:00
|
|
|
})
|
|
|
|
};
|
2019-02-19 01:48:52 +01:00
|
|
|
|
2019-02-20 22:09:32 +01:00
|
|
|
m::device::set(user_id,
|
2019-02-19 01:48:52 +01:00
|
|
|
{
|
2019-02-21 00:46:48 +01:00
|
|
|
{ "device_id", device_id },
|
|
|
|
{ "display_name", initial_device_display_name },
|
|
|
|
{ "last_seen_ts", ircd::time<milliseconds>() },
|
|
|
|
{ "last_seen_ip", string(remote(*client)) },
|
|
|
|
{ "access_token_id", access_token_id },
|
2019-02-19 01:48:52 +01:00
|
|
|
});
|
2019-02-21 00:46:48 +01:00
|
|
|
}
|
2018-02-15 22:11:51 +01:00
|
|
|
|
2017-08-23 23:10:28 +02:00
|
|
|
// Send response to user
|
2018-06-07 22:20:46 +02:00
|
|
|
return json::strung
|
2017-03-21 03:28:00 +01:00
|
|
|
{
|
2018-06-07 22:20:46 +02:00
|
|
|
json::members
|
2017-03-21 03:28:00 +01:00
|
|
|
{
|
2017-09-08 11:32:49 +02:00
|
|
|
{ "user_id", user_id },
|
2017-10-01 12:09:28 +02:00
|
|
|
{ "home_server", my_host() },
|
2018-02-15 22:11:51 +01:00
|
|
|
{ "access_token", access_token },
|
|
|
|
{ "device_id", device_id },
|
2017-09-08 11:32:49 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2017-03-11 04:31:20 +01:00
|
|
|
|
2017-09-25 03:05:42 +02:00
|
|
|
void
|
|
|
|
validate_user_id(const m::id::user &user_id)
|
|
|
|
{
|
2017-10-01 12:09:28 +02:00
|
|
|
if(user_id.host() != my_host())
|
2017-09-25 03:05:42 +02:00
|
|
|
throw m::error
|
|
|
|
{
|
2017-11-16 02:48:25 +01:00
|
|
|
http::BAD_REQUEST,
|
|
|
|
"M_INVALID_USERNAME",
|
|
|
|
"Can only register with host '%s'",
|
|
|
|
my_host()
|
2017-09-25 03:05:42 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
validate_password(const string_view &password)
|
|
|
|
{
|
|
|
|
if(password.size() > 255)
|
|
|
|
throw m::error
|
|
|
|
{
|
2017-11-16 02:48:25 +01:00
|
|
|
http::BAD_REQUEST,
|
|
|
|
"M_INVALID_PASSWORD",
|
|
|
|
"The desired password is too long"
|
2017-09-25 03:05:42 +02:00
|
|
|
};
|
|
|
|
}
|
2019-01-12 21:33:00 +01:00
|
|
|
|
|
|
|
const std::string
|
|
|
|
flows
|
|
|
|
{R"({
|
|
|
|
"flows":
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"stages":
|
|
|
|
[
|
|
|
|
"m.login.dummy", "m.login.password"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})"};
|