mirror of
https://github.com/matrix-construct/construct
synced 2024-12-26 15:33:54 +01:00
ircd:Ⓜ️ Move user::password related to modules/client/account/password.
This commit is contained in:
parent
1723a48e89
commit
ec0d151827
4 changed files with 120 additions and 83 deletions
|
@ -30,14 +30,13 @@ struct ircd::m::user
|
|||
static m::room users;
|
||||
static m::room tokens;
|
||||
|
||||
static string_view gen_password_hash(const mutable_buffer &out, const string_view &candidate);
|
||||
static string_view gen_access_token(const mutable_buffer &out);
|
||||
|
||||
id::room room_id(const mutable_buffer &) const;
|
||||
id::room::buf room_id() const;
|
||||
|
||||
bool is_password(const string_view &password) const noexcept;
|
||||
void password(const string_view &password);
|
||||
event::id::buf password(const string_view &password);
|
||||
|
||||
bool is_active() const;
|
||||
event::id::buf deactivate(const json::members &contents = {});
|
||||
|
|
38
ircd/m/m.cc
38
ircd/m/m.cc
|
@ -383,6 +383,44 @@ const
|
|||
return function(*this);
|
||||
}
|
||||
|
||||
ircd::m::event::id::buf
|
||||
ircd::m::user::password(const string_view &password)
|
||||
{
|
||||
using prototype = event::id::buf (const m::user::id &, const string_view &) noexcept;
|
||||
|
||||
static import<prototype> function
|
||||
{
|
||||
"client_account", "set_password"
|
||||
};
|
||||
|
||||
return function(user_id, password);
|
||||
}
|
||||
|
||||
bool
|
||||
ircd::m::user::is_password(const string_view &password)
|
||||
const noexcept try
|
||||
{
|
||||
using prototype = bool (const m::user::id &, const string_view &) noexcept;
|
||||
|
||||
static import<prototype> function
|
||||
{
|
||||
"client_account", "is_password"
|
||||
};
|
||||
|
||||
return function(user_id, password);
|
||||
}
|
||||
catch(const std::exception &e)
|
||||
{
|
||||
log::critical
|
||||
{
|
||||
"user::is_password(): %s %s",
|
||||
string_view{user_id},
|
||||
e.what()
|
||||
};
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// m/room.h
|
||||
|
|
|
@ -60,74 +60,6 @@ ircd::m::my(const user &user)
|
|||
return my(user.user_id);
|
||||
}
|
||||
|
||||
void
|
||||
ircd::m::user::password(const string_view &password)
|
||||
try
|
||||
{
|
||||
char buf[64];
|
||||
const auto supplied
|
||||
{
|
||||
gen_password_hash(buf, password)
|
||||
};
|
||||
|
||||
const user::room user_room{*this};
|
||||
send(user_room, user_id, "ircd.password", user_id,
|
||||
{
|
||||
{ "sha256", supplied }
|
||||
});
|
||||
}
|
||||
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 &password)
|
||||
const noexcept try
|
||||
{
|
||||
char buf[64];
|
||||
const auto supplied
|
||||
{
|
||||
gen_password_hash(buf, password)
|
||||
};
|
||||
|
||||
bool ret{false};
|
||||
const user::room user_room{*this};
|
||||
user_room.get("ircd.password", user_id, [&supplied, &ret]
|
||||
(const m::event &event)
|
||||
{
|
||||
const json::object &content
|
||||
{
|
||||
json::at<"content"_>(event)
|
||||
};
|
||||
|
||||
const auto &correct
|
||||
{
|
||||
unquote(content.at("sha256"))
|
||||
};
|
||||
|
||||
ret = supplied == correct;
|
||||
});
|
||||
|
||||
return ret;
|
||||
}
|
||||
catch(const m::NOT_FOUND &e)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
catch(const std::exception &e)
|
||||
{
|
||||
log::critical
|
||||
{
|
||||
"user::is_password(): %s %s", string_view{user_id}, e.what()
|
||||
};
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// Generates a user-room ID into buffer; see room_id() overload.
|
||||
ircd::m::id::room::buf
|
||||
ircd::m::user::room_id()
|
||||
|
@ -172,19 +104,6 @@ ircd::m::user::gen_access_token(const mutable_buffer &buf)
|
|||
return rand::string(token_dict, out);
|
||||
}
|
||||
|
||||
ircd::string_view
|
||||
ircd::m::user::gen_password_hash(const mutable_buffer &out,
|
||||
const string_view &supplied_password)
|
||||
{
|
||||
//TODO: ADD SALT
|
||||
const sha256::buf hash
|
||||
{
|
||||
sha256{supplied_password}
|
||||
};
|
||||
|
||||
return b64encode_unpadded(out, hash);
|
||||
}
|
||||
|
||||
//
|
||||
// user::room
|
||||
//
|
||||
|
|
|
@ -74,3 +74,84 @@ post_password
|
|||
post_password.REQUIRES_AUTH
|
||||
}
|
||||
};
|
||||
|
||||
static string_view
|
||||
gen_password_hash(const mutable_buffer &out,
|
||||
const string_view &supplied_password);
|
||||
|
||||
extern "C" m::event::id::buf
|
||||
set_password(const m::user::id &user_id,
|
||||
const string_view &password)
|
||||
{
|
||||
char buf[64];
|
||||
const auto supplied
|
||||
{
|
||||
gen_password_hash(buf, password)
|
||||
};
|
||||
|
||||
const m::user::room user_room{user_id};
|
||||
return send(user_room, user_id, "ircd.password", user_id,
|
||||
{
|
||||
{ "sha256", supplied }
|
||||
});
|
||||
}
|
||||
|
||||
extern "C" bool
|
||||
is_password(const m::user::id &user_id,
|
||||
const string_view &password)
|
||||
noexcept try
|
||||
{
|
||||
char buf[64];
|
||||
const auto supplied
|
||||
{
|
||||
gen_password_hash(buf, password)
|
||||
};
|
||||
|
||||
bool ret{false};
|
||||
const m::user::room user_room{user_id};
|
||||
user_room.get("ircd.password", user_id, [&supplied, &ret]
|
||||
(const m::event &event)
|
||||
{
|
||||
const json::object &content
|
||||
{
|
||||
json::at<"content"_>(event)
|
||||
};
|
||||
|
||||
const auto &correct
|
||||
{
|
||||
unquote(content.at("sha256"))
|
||||
};
|
||||
|
||||
ret = supplied == correct;
|
||||
});
|
||||
|
||||
return ret;
|
||||
}
|
||||
catch(const m::NOT_FOUND &e)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
catch(const std::exception &e)
|
||||
{
|
||||
log::critical
|
||||
{
|
||||
"is_password__user(): %s %s",
|
||||
string_view{user_id},
|
||||
e.what()
|
||||
};
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
string_view
|
||||
gen_password_hash(const mutable_buffer &out,
|
||||
const string_view &supplied_password)
|
||||
{
|
||||
//TODO: ADD SALT
|
||||
const sha256::buf hash
|
||||
{
|
||||
sha256{supplied_password}
|
||||
};
|
||||
|
||||
return b64encode_unpadded(out, hash);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue