2017-09-08 11:32:49 +02:00
|
|
|
/*
|
2016-11-29 16:23:38 +01:00
|
|
|
* Copyright (C) 2017 Charybdis Development Team
|
|
|
|
* Copyright (C) 2017 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.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2017-03-21 03:28:00 +01:00
|
|
|
using namespace ircd;
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-08-23 23:10:28 +02:00
|
|
|
const auto home_server
|
2017-03-11 04:31:20 +01:00
|
|
|
{
|
2017-08-23 23:10:28 +02:00
|
|
|
// "The hostname of the homeserver on which the account has been registered."
|
|
|
|
"cdc.z"
|
2017-03-11 04:31:20 +01:00
|
|
|
};
|
|
|
|
|
2017-09-08 11:32:49 +02:00
|
|
|
namespace name
|
2017-03-11 04:31:20 +01:00
|
|
|
{
|
2017-09-08 11:32:49 +02:00
|
|
|
constexpr auto username{"username"};
|
|
|
|
constexpr auto bind_email{"bind_email"};
|
|
|
|
constexpr auto password{"password"};
|
|
|
|
constexpr auto auth{"auth"};
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
>
|
|
|
|
{
|
|
|
|
using super_type::tuple;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Generate !accounts:host which is the MXID of the room where the members
|
|
|
|
// are the account registrations on this homeserver.
|
|
|
|
const m::id::room::buf accounts_room_id
|
|
|
|
{
|
|
|
|
"accounts", home_server
|
|
|
|
};
|
|
|
|
|
|
|
|
m::room accounts_room
|
|
|
|
{
|
|
|
|
accounts_room_id
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
join_accounts_room(const m::id::user &user_id,
|
|
|
|
const json::members &contents)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const json::builder content
|
2017-08-23 23:10:28 +02:00
|
|
|
{
|
2017-09-08 11:32:49 +02:00
|
|
|
nullptr, &contents
|
2017-08-23 23:10:28 +02:00
|
|
|
};
|
|
|
|
|
2017-09-08 11:32:49 +02:00
|
|
|
accounts_room.join(user_id, content);
|
|
|
|
}
|
|
|
|
catch(const m::ALREADY_MEMBER &e)
|
|
|
|
{
|
|
|
|
throw m::error
|
2017-08-23 23:10:28 +02:00
|
|
|
{
|
2017-09-08 11:32:49 +02:00
|
|
|
http::CONFLICT, "M_USER_IN_USE", "The desired user ID is already in use."
|
|
|
|
};
|
|
|
|
}
|
2017-08-23 23:10:28 +02:00
|
|
|
|
2017-09-08 11:32:49 +02:00
|
|
|
resource::response
|
|
|
|
handle_post_kind_user(client &client,
|
|
|
|
resource::request &request,
|
|
|
|
const resource::request::body<body> &body)
|
|
|
|
{
|
|
|
|
// 3.3.1 Additional authentication information for the user-interactive authentication API.
|
|
|
|
const auto &auth
|
|
|
|
{
|
|
|
|
at<name::auth>(body)
|
|
|
|
};
|
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.
|
|
|
|
const auto &type
|
2017-08-23 23:10:28 +02:00
|
|
|
{
|
2017-09-08 11:32:49 +02:00
|
|
|
unquote(auth.at("type"))
|
2017-08-23 23:10:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if(type != "m.login.dummy")
|
|
|
|
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-09-08 11:32:49 +02:00
|
|
|
unquote(get<name::username>(body))
|
2017-08-23 23:10:28 +02:00
|
|
|
};
|
|
|
|
|
2017-09-08 11:32:49 +02:00
|
|
|
// Generate canonical mxid
|
|
|
|
const m::id::user::buf user_id
|
|
|
|
{
|
|
|
|
username, home_server
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!user_id.valid())
|
2017-08-23 23:10:28 +02:00
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
"M_INVALID_USERNAME", "The desired user ID is not a valid user name."
|
|
|
|
};
|
|
|
|
|
2017-09-08 11:32:49 +02:00
|
|
|
if(user_id.host() != home_server)
|
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
"M_INVALID_USERNAME", "Can only register with host '%s'", home_server
|
|
|
|
};
|
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-09-08 11:32:49 +02:00
|
|
|
at<name::password>(body)
|
2017-08-23 23:10:28 +02:00
|
|
|
};
|
|
|
|
|
2017-09-08 11:32:49 +02:00
|
|
|
if(password.size() > 255)
|
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
"M_INVALID_PASSWORD", "The desired password is too long"
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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-09-08 11:32:49 +02:00
|
|
|
get<name::bind_email>(body, false)
|
2017-08-23 23:10:28 +02:00
|
|
|
};
|
|
|
|
|
2017-09-08 11:32:49 +02:00
|
|
|
// Register the user by joining them to the accounts room. Join the user
|
|
|
|
// to the accounts room by issuing a join event. The content will store
|
|
|
|
// keys from the registration options including the password - do not
|
|
|
|
// expose this to clients //TODO: store hashed pass
|
|
|
|
// Once this call completes the join was successful and the user
|
|
|
|
// is registered, otherwise throws.
|
|
|
|
char content[384];
|
|
|
|
join_accounts_room(user_id,
|
2017-08-23 23:10:28 +02:00
|
|
|
{
|
2017-09-08 11:32:49 +02:00
|
|
|
{ "password", password },
|
|
|
|
{ "bind_email", bind_email },
|
2017-08-23 23:10:28 +02:00
|
|
|
});
|
2017-03-11 04:31:20 +01:00
|
|
|
|
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-03-21 03:28:00 +01:00
|
|
|
{ "home_server", home_server },
|
2017-09-08 11:32:49 +02:00
|
|
|
// { "access_token", access_token },
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
resource::response
|
|
|
|
handle_post_kind_guest(client &client,
|
|
|
|
resource::request &request,
|
|
|
|
const resource::request::body<body> &body)
|
|
|
|
{
|
|
|
|
const m::id::user::buf user_id
|
|
|
|
{
|
|
|
|
m::generate, home_server
|
|
|
|
};
|
|
|
|
|
|
|
|
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-09-08 11:32:49 +02:00
|
|
|
{ "home_server", home_server },
|
|
|
|
//{ "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,
|
|
|
|
resource::request &request)
|
|
|
|
{
|
|
|
|
const resource::request::body<body> body
|
|
|
|
{
|
|
|
|
request
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto kind
|
|
|
|
{
|
|
|
|
request.query["kind"]
|
|
|
|
};
|
|
|
|
|
|
|
|
if(kind == "user")
|
|
|
|
return handle_post_kind_user(client, request, body);
|
|
|
|
|
|
|
|
if(kind.empty() || kind == "guest")
|
|
|
|
return handle_post_kind_guest(client, request, body);
|
|
|
|
|
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
http::BAD_REQUEST, "M_UNKNOWN", "Unknown 'kind' of registration specified in query."
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-03-21 03:28:00 +01:00
|
|
|
resource register_resource
|
2017-03-11 04:31:20 +01:00
|
|
|
{
|
2017-08-23 23:10:28 +02:00
|
|
|
"_matrix/client/r0/register",
|
2017-03-21 03:28:00 +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
|
|
|
};
|