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-08-23 23:10:28 +02:00
|
|
|
using namespace ircd;
|
2016-11-29 16:23:38 +01:00
|
|
|
|
|
|
|
resource login_resource
|
|
|
|
{
|
2017-08-23 23:10:28 +02:00
|
|
|
"_matrix/client/r0/login",
|
2016-11-29 16:23:38 +01:00
|
|
|
"Authenticates the user by password, and issues an access token "
|
|
|
|
"they can use to authorize themself in subsequent requests. (3.2.2)"
|
|
|
|
};
|
|
|
|
|
2017-08-23 23:10:28 +02:00
|
|
|
const auto home_server
|
|
|
|
{
|
|
|
|
// "The hostname of the homeserver on which the account has been registered."
|
|
|
|
"cdc.z"
|
|
|
|
};
|
|
|
|
|
2017-09-08 11:32:49 +02:00
|
|
|
namespace name
|
|
|
|
{
|
|
|
|
extern constexpr auto password{"password"};
|
|
|
|
extern constexpr auto medium{"medium"};
|
|
|
|
extern constexpr auto type{"type"};
|
|
|
|
extern constexpr auto user{"user"};
|
|
|
|
extern constexpr auto address{"address"};
|
|
|
|
}
|
2017-08-23 23:10:28 +02:00
|
|
|
|
2017-09-08 11:32:49 +02:00
|
|
|
struct body
|
|
|
|
:json::tuple
|
|
|
|
<
|
|
|
|
json::member<name::password, string_view>,
|
|
|
|
json::member<name::medium, time_t>,
|
|
|
|
json::member<name::type, string_view>,
|
|
|
|
json::member<name::user, string_view>,
|
|
|
|
json::member<name::address, string_view>
|
|
|
|
>
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-09-08 11:32:49 +02:00
|
|
|
using super_type::tuple;
|
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-09-08 11:32:49 +02:00
|
|
|
// Generate !accounts:host which is the MXID of the room where the members
|
|
|
|
// are the actual account registrations themselves for this homeserver.
|
|
|
|
const m::id::room::buf accounts_room_id
|
|
|
|
{
|
|
|
|
"accounts", home_server
|
|
|
|
};
|
2017-08-23 23:10:28 +02:00
|
|
|
|
2017-09-08 11:32:49 +02:00
|
|
|
// Handle to the accounts room
|
|
|
|
m::room accounts_room
|
|
|
|
{
|
|
|
|
accounts_room_id
|
|
|
|
};
|
|
|
|
|
|
|
|
resource::response
|
|
|
|
post_login_password(client &client,
|
|
|
|
const resource::request &request,
|
|
|
|
const resource::request::body<body> &body)
|
|
|
|
{
|
|
|
|
const m::id::user::buf user_id
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-09-08 11:32:49 +02:00
|
|
|
unquote(at<name::user>(body)), home_server
|
2016-11-29 16:23:38 +01:00
|
|
|
};
|
|
|
|
|
2017-09-08 11:32:49 +02:00
|
|
|
if(!user_id.valid() || user_id.host() != home_server)
|
2017-08-23 23:10:28 +02:00
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
http::FORBIDDEN, "M_FORBIDDEN", "Access denied."
|
|
|
|
};
|
|
|
|
|
2017-09-08 11:32:49 +02:00
|
|
|
const auto &supplied_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
|
|
|
const auto check{[&supplied_password]
|
|
|
|
(const auto &event)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-09-08 11:32:49 +02:00
|
|
|
const json::object &content
|
|
|
|
{
|
|
|
|
json::val<m::name::content>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &membership
|
|
|
|
{
|
|
|
|
unquote(content.at("membership"))
|
|
|
|
};
|
|
|
|
|
|
|
|
if(membership != "join")
|
|
|
|
return false;
|
2017-03-21 03:28:00 +01:00
|
|
|
|
2017-09-08 11:32:49 +02:00
|
|
|
const auto &correct_password
|
|
|
|
{
|
|
|
|
content.at("password")
|
|
|
|
};
|
|
|
|
|
|
|
|
if(supplied_password != correct_password)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}};
|
|
|
|
|
|
|
|
const m::events::where::equal query
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-09-08 11:32:49 +02:00
|
|
|
{ "type", "m.room.member" },
|
|
|
|
{ "state_key", user_id }
|
2017-08-23 23:10:28 +02:00
|
|
|
};
|
|
|
|
|
2017-09-08 11:32:49 +02:00
|
|
|
if(!accounts_room.any(query, check))
|
2017-03-21 03:28:00 +01:00
|
|
|
throw m::error
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-08-23 23:10:28 +02:00
|
|
|
http::FORBIDDEN, "M_FORBIDDEN", "Access denied."
|
2017-03-21 03:28:00 +01:00
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-09-08 11:32:49 +02:00
|
|
|
// Send response to user
|
2017-03-21 03:28:00 +01:00
|
|
|
return resource::response
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-03-21 03:28:00 +01:00
|
|
|
client,
|
|
|
|
{
|
2017-09-08 11:32:49 +02:00
|
|
|
{ "user_id", string_view{user_id} },
|
|
|
|
{ "home_server", home_server },
|
|
|
|
// { "access_token", access_token },
|
2017-03-21 03:28:00 +01:00
|
|
|
}
|
2016-11-29 16:23:38 +01:00
|
|
|
};
|
2017-03-21 03:28:00 +01:00
|
|
|
}
|
2017-08-23 23:10:28 +02:00
|
|
|
|
|
|
|
resource::response
|
2017-08-23 23:51:34 +02:00
|
|
|
post_login(client &client, const resource::request &request)
|
2017-03-21 03:28:00 +01:00
|
|
|
{
|
2017-09-08 11:32:49 +02:00
|
|
|
const resource::request::body<body> body
|
|
|
|
{
|
|
|
|
request
|
|
|
|
};
|
|
|
|
|
|
|
|
// x.x.x Required. The login type being used.
|
|
|
|
// Currently only "m.login.password" is supported.
|
2017-08-23 23:10:28 +02:00
|
|
|
const auto type
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-09-08 11:32:49 +02:00
|
|
|
unquote(at<name::type>(body))
|
2016-11-29 16:23:38 +01:00
|
|
|
};
|
2017-08-23 23:10:28 +02:00
|
|
|
|
|
|
|
if(type == "m.login.password")
|
2017-09-08 11:32:49 +02:00
|
|
|
return post_login_password(client, request, body);
|
2017-08-23 23:10:28 +02:00
|
|
|
else
|
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
"M_UNSUPPORTED", "Login type is not supported."
|
|
|
|
};
|
2017-03-21 03:28:00 +01:00
|
|
|
}
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-08-23 23:51:34 +02:00
|
|
|
resource::method method_post
|
2017-03-21 03:28:00 +01:00
|
|
|
{
|
2017-08-23 23:51:34 +02:00
|
|
|
login_resource, "POST", post_login
|
|
|
|
};
|
|
|
|
|
|
|
|
resource::response
|
|
|
|
get_login(client &client, const resource::request &request)
|
|
|
|
{
|
|
|
|
static const json::object flows
|
|
|
|
{
|
|
|
|
R"({"flows":[{"type":"m.login.password"}]})"
|
|
|
|
};
|
|
|
|
|
|
|
|
return resource::response
|
|
|
|
{
|
|
|
|
client, flows
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
resource::method method_get
|
|
|
|
{
|
|
|
|
login_resource, "GET", get_login
|
2017-03-21 03:28:00 +01:00
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
|
|
|
mapi::header IRCD_MODULE
|
|
|
|
{
|
|
|
|
"registers the resource 'client/login' to handle requests"
|
|
|
|
};
|