From b94d0b8ce6b226fe6d9a359a331e5fcd218d8330 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Sun, 11 Feb 2018 11:43:38 -0800 Subject: [PATCH] ircd::m: Split m::user compilation unit. --- ircd/Makefile.am | 1 + ircd/m/m.cc | 156 --------------------------------------------- ircd/m/user.cc | 162 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 163 insertions(+), 156 deletions(-) create mode 100644 ircd/m/user.cc diff --git a/ircd/Makefile.am b/ircd/Makefile.am index e7f444c11..d5d139cc3 100644 --- a/ircd/Makefile.am +++ b/ircd/Makefile.am @@ -83,6 +83,7 @@ libircd_la_SOURCES = \ m/keys.cc \ m/request.cc \ m/room.cc \ + m/user.cc \ m/vm.cc \ m/m.cc \ mods.cc \ diff --git a/ircd/m/m.cc b/ircd/m/m.cc index d68c935be..b1b8cfc0a 100644 --- a/ircd/m/m.cc +++ b/ircd/m/m.cc @@ -337,159 +337,3 @@ ircd::m::filter::size(const string_view &filter_id) return ret; } - -/////////////////////////////////////////////////////////////////////////////// -// -// m/user.h -// - -const ircd::m::room::id::buf -accounts_room_id -{ - "accounts", ircd::my_host() -}; - -ircd::m::room -ircd::m::user::accounts -{ - accounts_room_id -}; - -const ircd::m::room::id::buf -sessions_room_id -{ - "sessions", ircd::my_host() -}; - -ircd::m::room -ircd::m::user::sessions -{ - sessions_room_id -}; - -/// Register the user by joining them to the accounts room. -/// -/// The content of the join event may store keys including the registration -/// options. Once this call completes the join was successful and the user is -/// registered, otherwise throws. -void -ircd::m::user::activate(const json::members &contents) -try -{ - json::iov content; - json::iov::push push[] - { - { content, { "membership", "join" }}, - }; - - size_t i(0); - json::iov::push _content[contents.size()]; - for(const auto &member : contents) - new (_content + i++) json::iov::push(content, member); - - send(accounts, me.user_id, "ircd.user", user_id, content); - join(control, user_id); -} -catch(const m::ALREADY_MEMBER &e) -{ - throw m::error - { - http::CONFLICT, "M_USER_IN_USE", "The desired user ID is already in use." - }; -} - -void -ircd::m::user::deactivate(const json::members &contents) -{ - json::iov content; - json::iov::push push[] - { - { content, { "membership", "leave" }}, - }; - - size_t i(0); - json::iov::push _content[contents.size()]; - for(const auto &member : contents) - new (_content + i++) json::iov::push(content, member); - - send(accounts, me.user_id, "ircd.user", user_id, content); -} - -void -ircd::m::user::password(const string_view &password) -try -{ - //TODO: ADD SALT - char b64[64], hash[32]; - sha256{hash, password}; - const auto digest{b64encode_unpadded(b64, hash)}; - send(accounts, me.user_id, "ircd.password", user_id, - { - { "sha256", digest } - }); -} -catch(const m::ALREADY_MEMBER &e) -{ - throw m::error - { - http::CONFLICT, "M_USER_IN_USE", "The desired user ID is already in use." - }; -} - -bool -ircd::m::user::is_password(const string_view &supplied_password) -const -{ - //TODO: ADD SALT - char b64[64], hash[32]; - sha256{hash, supplied_password}; - const auto supplied_hash - { - b64encode_unpadded(b64, hash) - }; - - static const string_view type{"ircd.password"}; - const string_view &state_key{user_id}; - const room room - { - accounts.room_id - }; - - bool ret{false}; - room::state{room}.get(type, state_key, [&supplied_hash, &ret] - (const m::event &event) - { - const json::object &content - { - json::at<"content"_>(event) - }; - - const auto &correct_hash - { - unquote(content.at("sha256")) - }; - - ret = supplied_hash == correct_hash; - }); - - return ret; -} - -bool -ircd::m::user::is_active() -const -{ - bool ret{false}; - room::state{accounts}.get("ircd.user", user_id, [&ret] - (const m::event &event) - { - const json::object &content - { - at<"content"_>(event) - }; - - ret = unquote(content.at("membership")) == "join"; - }); - - return ret; -} diff --git a/ircd/m/user.cc b/ircd/m/user.cc new file mode 100644 index 000000000..80028f43b --- /dev/null +++ b/ircd/m/user.cc @@ -0,0 +1,162 @@ +// Matrix Construct +// +// Copyright (C) Matrix Construct Developers, Authors & Contributors +// Copyright (C) 2016-2018 Jason Volk +// +// 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. + +#include + +const ircd::m::room::id::buf +accounts_room_id +{ + "accounts", ircd::my_host() +}; + +ircd::m::room +ircd::m::user::accounts +{ + accounts_room_id +}; + +const ircd::m::room::id::buf +sessions_room_id +{ + "sessions", ircd::my_host() +}; + +ircd::m::room +ircd::m::user::sessions +{ + sessions_room_id +}; + +/// Register the user by joining them to the accounts room. +/// +/// The content of the join event may store keys including the registration +/// options. Once this call completes the join was successful and the user is +/// registered, otherwise throws. +void +ircd::m::user::activate(const json::members &contents) +try +{ + json::iov content; + json::iov::push push[] + { + { content, { "membership", "join" }}, + }; + + size_t i(0); + json::iov::push _content[contents.size()]; + for(const auto &member : contents) + new (_content + i++) json::iov::push(content, member); + + send(accounts, me.user_id, "ircd.user", user_id, content); + join(control, user_id); +} +catch(const m::ALREADY_MEMBER &e) +{ + throw m::error + { + http::CONFLICT, "M_USER_IN_USE", "The desired user ID is already in use." + }; +} + +void +ircd::m::user::deactivate(const json::members &contents) +{ + json::iov content; + json::iov::push push[] + { + { content, { "membership", "leave" }}, + }; + + size_t i(0); + json::iov::push _content[contents.size()]; + for(const auto &member : contents) + new (_content + i++) json::iov::push(content, member); + + send(accounts, me.user_id, "ircd.user", user_id, content); +} + +void +ircd::m::user::password(const string_view &password) +try +{ + //TODO: ADD SALT + char b64[64], hash[32]; + sha256{hash, password}; + const auto digest{b64encode_unpadded(b64, hash)}; + send(accounts, me.user_id, "ircd.password", user_id, + { + { "sha256", digest } + }); +} +catch(const m::ALREADY_MEMBER &e) +{ + throw m::error + { + http::CONFLICT, "M_USER_IN_USE", "The desired user ID is already in use." + }; +} + +bool +ircd::m::user::is_password(const string_view &supplied_password) +const +{ + //TODO: ADD SALT + char b64[64], hash[32]; + sha256{hash, supplied_password}; + const auto supplied_hash + { + b64encode_unpadded(b64, hash) + }; + + static const string_view type{"ircd.password"}; + const string_view &state_key{user_id}; + const room room + { + accounts.room_id + }; + + bool ret{false}; + room::state{room}.get(type, state_key, [&supplied_hash, &ret] + (const m::event &event) + { + const json::object &content + { + json::at<"content"_>(event) + }; + + const auto &correct_hash + { + unquote(content.at("sha256")) + }; + + ret = supplied_hash == correct_hash; + }); + + return ret; +} + +bool +ircd::m::user::is_active() +const +{ + bool ret{false}; + room::state{accounts}.get("ircd.user", user_id, [&ret] + (const m::event &event) + { + const json::object &content + { + at<"content"_>(event) + }; + + ret = unquote(content.at("membership")) == "join"; + }); + + return ret; +}