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
|
|
|
|
2017-09-25 03:05:42 +02:00
|
|
|
namespace { namespace name
|
2017-03-11 04:31:20 +01:00
|
|
|
{
|
2017-09-25 03:05:42 +02:00
|
|
|
constexpr const auto username {"username"};
|
|
|
|
constexpr const auto bind_email {"bind_email"};
|
|
|
|
constexpr const auto password {"password"};
|
|
|
|
constexpr const auto auth {"auth"};
|
|
|
|
}}
|
2017-09-08 11:32:49 +02:00
|
|
|
|
|
|
|
struct body
|
|
|
|
:json::tuple
|
|
|
|
<
|
2017-09-08 12:15:58 +02:00
|
|
|
json::property<name::username, string_view>,
|
|
|
|
json::property<name::bind_email, bool>,
|
|
|
|
json::property<name::password, string_view>,
|
|
|
|
json::property<name::auth, json::object>
|
2017-09-08 11:32:49 +02:00
|
|
|
>
|
|
|
|
{
|
2017-09-25 03:05:42 +02:00
|
|
|
using super_type::tuple;
|
2017-09-08 11:32:49 +02:00
|
|
|
};
|
|
|
|
|
2017-09-25 03:05:42 +02:00
|
|
|
static void validate_user_id(const m::id::user &user_id);
|
|
|
|
static void validate_password(const string_view &password);
|
2017-08-23 23:10:28 +02:00
|
|
|
|
2017-09-08 11:32:49 +02:00
|
|
|
resource::response
|
|
|
|
handle_post_kind_user(client &client,
|
2017-09-25 03:05:42 +02:00
|
|
|
const resource::request::object<body> &request)
|
2017-11-16 02:48:25 +01:00
|
|
|
try
|
2017-09-08 11:32:49 +02:00
|
|
|
{
|
|
|
|
// 3.3.1 Additional authentication information for the user-interactive authentication API.
|
2018-01-29 20:36:39 +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
|
|
|
|
2017-09-08 11:32:49 +02:00
|
|
|
// 3.3.1 Required. The login type that the client is attempting to complete.
|
2018-01-29 20:36:39 +01:00
|
|
|
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")
|
2017-08-23 23:10:28 +02:00
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
"M_UNSUPPORTED", "Registration '%s' not supported.", type
|
|
|
|
};
|
|
|
|
|
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
|
|
|
{
|
2017-10-05 01:40:02 +02:00
|
|
|
unquote(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
|
|
|
{
|
2017-11-25 23:30:03 +01:00
|
|
|
unquote(at<"password"_>(request))
|
2017-08-23 23:10:28 +02:00
|
|
|
};
|
|
|
|
|
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);
|
|
|
|
|
2017-09-25 05:47:13 +02:00
|
|
|
// Represent the user
|
|
|
|
m::user user
|
|
|
|
{
|
|
|
|
user_id
|
|
|
|
};
|
|
|
|
|
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.
|
2017-09-25 05:47:13 +02:00
|
|
|
user.activate(
|
2017-08-23 23:10:28 +02:00
|
|
|
{
|
2017-11-30 19:56:18 +01:00
|
|
|
{ "bind_email", bind_email },
|
2017-08-23 23:10:28 +02:00
|
|
|
});
|
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);
|
|
|
|
|
2017-08-23 23:10:28 +02:00
|
|
|
// Send response to user
|
2017-03-21 03:28:00 +01:00
|
|
|
return resource::response
|
|
|
|
{
|
2017-09-08 17:15:14 +02:00
|
|
|
client, http::CREATED,
|
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() },
|
2017-09-08 11:32:49 +02:00
|
|
|
// { "access_token", access_token },
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2017-11-16 02:48:25 +01:00
|
|
|
catch(const m::INVALID_MXID &e)
|
|
|
|
{
|
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
http::BAD_REQUEST,
|
|
|
|
"M_INVALID_USERNAME",
|
2017-12-12 21:26:39 +01:00
|
|
|
"Not a valid username. Please try again."
|
2017-11-16 02:48:25 +01:00
|
|
|
};
|
|
|
|
};
|
2017-09-08 11:32:49 +02:00
|
|
|
|
|
|
|
resource::response
|
|
|
|
handle_post_kind_guest(client &client,
|
2017-09-25 03:05:42 +02:00
|
|
|
const resource::request::object<body> &request)
|
2017-09-08 11:32:49 +02:00
|
|
|
{
|
|
|
|
const m::id::user::buf user_id
|
|
|
|
{
|
2017-10-01 12:09:28 +02:00
|
|
|
m::generate, my_host()
|
2017-09-08 11:32:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return resource::response
|
|
|
|
{
|
2017-09-08 17:15:14 +02:00
|
|
|
client, http::CREATED,
|
2017-09-08 11:32:49 +02:00
|
|
|
{
|
2017-03-21 03:28:00 +01:00
|
|
|
{ "user_id", user_id },
|
2017-10-01 12:09:28 +02:00
|
|
|
{ "home_server", my_host() },
|
2017-09-25 03:05:42 +02:00
|
|
|
// { "access_token", access_token },
|
2017-03-21 03:28:00 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2017-03-11 04:31:20 +01:00
|
|
|
|
2017-09-08 11:32:49 +02:00
|
|
|
resource::response
|
|
|
|
handle_post(client &client,
|
2017-09-25 03:05:42 +02:00
|
|
|
const resource::request::object<body> &request)
|
2017-09-08 11:32:49 +02:00
|
|
|
{
|
|
|
|
const auto kind
|
|
|
|
{
|
|
|
|
request.query["kind"]
|
|
|
|
};
|
|
|
|
|
2018-01-29 20:27:14 +01:00
|
|
|
if(kind == "guest")
|
2017-09-25 03:05:42 +02:00
|
|
|
return handle_post_kind_guest(client, request);
|
2017-09-08 11:32:49 +02:00
|
|
|
|
2018-01-29 20:27:14 +01:00
|
|
|
if(kind.empty() || kind == "user")
|
|
|
|
return handle_post_kind_user(client, request);
|
|
|
|
|
2017-09-08 11:32:49 +02:00
|
|
|
throw m::error
|
|
|
|
{
|
2017-11-16 02:48:25 +01:00
|
|
|
http::BAD_REQUEST,
|
|
|
|
"M_UNKNOWN",
|
|
|
|
"Unknown 'kind' of registration specified in query."
|
2017-09-08 11:32:49 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-03-21 03:28:00 +01:00
|
|
|
resource register_resource
|
2017-03-11 04:31:20 +01:00
|
|
|
{
|
2017-10-12 05:52:33 +02:00
|
|
|
"/_matrix/client/r0/register",
|
2017-12-12 21:26:39 +01:00
|
|
|
{
|
|
|
|
"Register for an account on this homeserver. (3.3.1)"
|
|
|
|
}
|
2017-03-11 04:31:20 +01:00
|
|
|
};
|
|
|
|
|
2017-03-21 03:28:00 +01:00
|
|
|
resource::method post
|
2017-03-11 04:31:20 +01:00
|
|
|
{
|
2017-03-21 03:28:00 +01:00
|
|
|
register_resource, "POST", handle_post
|
2017-03-11 04:31:20 +01:00
|
|
|
};
|
|
|
|
|
2017-03-21 03:28:00 +01:00
|
|
|
mapi::header IRCD_MODULE
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-03-21 03:28:00 +01:00
|
|
|
"registers the resource 'client/register' to handle requests"
|
2016-11-29 16:23:38 +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
|
|
|
};
|
|
|
|
}
|