From 050eacd142401b2b8fdea7ceaea3533dbcc2af14 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Thu, 15 Feb 2018 13:05:00 -0800 Subject: [PATCH] ircd::m: Add m::user helpers to generate password hash and access tokens. --- include/ircd/m/user.h | 3 +++ ircd/m/user.cc | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/include/ircd/m/user.h b/include/ircd/m/user.h index 6dd771ded..e114e0d7e 100644 --- a/include/ircd/m/user.h +++ b/include/ircd/m/user.h @@ -25,6 +25,9 @@ struct ircd::m::user static room accounts; static room sessions; + static string_view gen_password_hash(const mutable_buffer &out, const string_view &candidate); + static string_view gen_access_token(const mutable_buffer &out); + bool is_active() const; bool is_password(const string_view &password) const; diff --git a/ircd/m/user.cc b/ircd/m/user.cc index eec1a0b5a..edbf50291 100644 --- a/ircd/m/user.cc +++ b/ircd/m/user.cc @@ -201,3 +201,30 @@ const buf, user_id.local(), my_host() }; } + +ircd::string_view +ircd::m::user::gen_access_token(const mutable_buffer &buf) +{ + static const size_t token_max{32}; + static const auto &token_dict{rand::dict::alpha}; + + const mutable_buffer out + { + data(buf), std::min(token_max, size(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); +}