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;
|
|
|
|
|
2018-06-07 22:20:46 +02:00
|
|
|
static resource::response
|
2019-09-25 20:01:05 +02:00
|
|
|
post__register_guest(client &client, const resource::request::object<m::user::registar> &request);
|
2018-06-07 22:20:46 +02:00
|
|
|
|
|
|
|
static resource::response
|
2019-09-25 20:01:05 +02:00
|
|
|
post__register_user(client &client, const resource::request::object<m::user::registar> &request);
|
2018-06-07 22:20:46 +02:00
|
|
|
|
|
|
|
static resource::response
|
2019-09-25 20:01:05 +02:00
|
|
|
post__register(client &client, const resource::request::object<m::user::registar> &request);
|
2018-06-07 22:20:46 +02:00
|
|
|
|
|
|
|
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 }
|
|
|
|
};
|
|
|
|
|
2019-09-25 20:01:05 +02:00
|
|
|
/// see: ircd/m/register.h for the m::user::registar tuple.
|
2018-06-07 22:20:46 +02:00
|
|
|
///
|
|
|
|
resource::response
|
|
|
|
post__register(client &client,
|
2019-09-25 20:01:05 +02:00
|
|
|
const resource::request::object<m::user::registar> &request)
|
2018-06-07 22:20:46 +02:00
|
|
|
{
|
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}
|
|
|
|
};
|
|
|
|
|
2019-03-03 00:58:16 +01:00
|
|
|
if(!bool(register_enable))
|
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
http::FORBIDDEN, "M_REGISTRATION_DISABLED",
|
|
|
|
"Registration for this server is disabled."
|
|
|
|
};
|
|
|
|
|
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,
|
2019-09-25 20:01:05 +02:00
|
|
|
const resource::request::object<m::user::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
|
|
|
|
{
|
2019-03-03 00:58:16 +01:00
|
|
|
http::FORBIDDEN, "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-09-25 20:01:05 +02:00
|
|
|
const unique_buffer<mutable_buffer> buf
|
|
|
|
{
|
|
|
|
4_KiB
|
|
|
|
};
|
|
|
|
|
|
|
|
// upcast to the user::registar tuple
|
|
|
|
const m::user::registar ®istar
|
2019-01-12 21:34:34 +01:00
|
|
|
{
|
2019-09-25 20:01:05 +02:00
|
|
|
request
|
2019-01-12 21:34:34 +01:00
|
|
|
};
|
|
|
|
|
2019-09-25 20:01:05 +02:00
|
|
|
// call operator() to register the user and receive response output
|
|
|
|
const json::object response
|
2018-06-07 22:20:46 +02:00
|
|
|
{
|
2019-09-25 20:01:05 +02:00
|
|
|
registar(buf, remote(client))
|
2018-06-07 22:20:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Send response to user
|
|
|
|
return resource::response
|
|
|
|
{
|
2019-09-25 20:01:05 +02:00
|
|
|
client, http::CREATED, response
|
2018-06-07 22:20:46 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
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" },
|
2019-03-04 18:29:27 +01:00
|
|
|
{ "default", false }
|
2018-06-07 22:20:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
resource::response
|
|
|
|
post__register_guest(client &client,
|
2019-09-25 20:01:05 +02:00
|
|
|
const resource::request::object<m::user::registar> &request)
|
2018-06-07 22:20:46 +02:00
|
|
|
{
|
|
|
|
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 },
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-12 21:33:00 +01:00
|
|
|
const std::string
|
2019-09-25 20:01:05 +02:00
|
|
|
flows{R"({
|
2019-01-12 21:33:00 +01:00
|
|
|
"flows":
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"stages":
|
|
|
|
[
|
2019-03-04 18:28:02 +01:00
|
|
|
"m.login.dummy",
|
|
|
|
"m.login.password",
|
|
|
|
"m.login.email.identity"
|
2019-01-12 21:33:00 +01:00
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})"};
|