2018-09-14 14:08:08 +02:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 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. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
2019-08-12 07:22:57 +02:00
|
|
|
namespace ircd::m
|
|
|
|
{
|
2019-09-26 21:16:22 +02:00
|
|
|
static string_view gen_password_hash(const mutable_buffer &, const string_view &);
|
2019-08-12 07:22:57 +02:00
|
|
|
static room create_user_room(const user::id &, const room::id &, const json::members &contents);
|
|
|
|
}
|
|
|
|
|
2021-01-25 07:01:36 +01:00
|
|
|
ircd::m::room::id::buf
|
|
|
|
ircd::m::viewing(const user &user,
|
|
|
|
size_t i)
|
|
|
|
{
|
|
|
|
const m::breadcrumbs breadcrumbs
|
|
|
|
{
|
|
|
|
user
|
|
|
|
};
|
|
|
|
|
|
|
|
room::id::buf ret;
|
|
|
|
breadcrumbs.for_each([&ret, &i]
|
|
|
|
(const auto &room_id)
|
|
|
|
{
|
|
|
|
if(i-- > 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
ret = room_id;
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-09-27 04:28:25 +02:00
|
|
|
bool
|
2020-04-30 10:03:14 +02:00
|
|
|
ircd::m::is_oper(const user &user)
|
2019-09-27 04:28:25 +02:00
|
|
|
{
|
2020-04-30 10:03:14 +02:00
|
|
|
const m::room::id::buf control_room_id
|
2019-09-27 04:28:25 +02:00
|
|
|
{
|
2020-04-30 10:03:14 +02:00
|
|
|
"!control", my_host()
|
2019-09-27 04:28:25 +02:00
|
|
|
};
|
|
|
|
|
2020-04-30 10:03:14 +02:00
|
|
|
return m::membership(control_room_id, user, "join");
|
2019-09-27 04:28:25 +02:00
|
|
|
}
|
|
|
|
|
2020-05-02 21:27:49 +02:00
|
|
|
bool
|
|
|
|
ircd::m::active(const user &user)
|
|
|
|
{
|
|
|
|
const m::user::room user_room
|
|
|
|
{
|
|
|
|
user.user_id
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::event::idx &event_idx
|
|
|
|
{
|
|
|
|
user_room.get(std::nothrow, "ircd.account", "active")
|
|
|
|
};
|
|
|
|
|
|
|
|
return m::query(std::nothrow, event_idx, "content", []
|
|
|
|
(const json::object &content)
|
|
|
|
{
|
|
|
|
return content.get<bool>("value", false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-09-27 04:28:25 +02:00
|
|
|
bool
|
|
|
|
ircd::m::exists(const user &user)
|
|
|
|
{
|
|
|
|
return exists(user.user_id);
|
|
|
|
}
|
|
|
|
|
2020-04-30 10:03:14 +02:00
|
|
|
bool
|
|
|
|
ircd::m::exists(const user::id &user)
|
|
|
|
{
|
|
|
|
// The way we know a user exists is testing if their room exists.
|
|
|
|
const user::room user_room
|
|
|
|
{
|
|
|
|
user
|
|
|
|
};
|
|
|
|
|
|
|
|
return m::exists(user_room);
|
|
|
|
}
|
|
|
|
|
2019-09-27 04:28:25 +02:00
|
|
|
bool
|
|
|
|
ircd::m::my(const user &user)
|
|
|
|
{
|
|
|
|
return my(user.user_id);
|
|
|
|
}
|
|
|
|
|
2019-07-15 19:09:26 +02:00
|
|
|
ircd::m::user
|
|
|
|
ircd::m::create(const m::user::id &user_id,
|
|
|
|
const json::members &contents)
|
2018-10-23 22:22:08 +02:00
|
|
|
{
|
|
|
|
const m::user user
|
|
|
|
{
|
|
|
|
user_id
|
|
|
|
};
|
|
|
|
|
2019-08-12 07:22:57 +02:00
|
|
|
const m::room::id::buf room_id
|
2018-10-23 22:22:08 +02:00
|
|
|
{
|
|
|
|
user.room_id()
|
|
|
|
};
|
|
|
|
|
2019-08-12 07:22:57 +02:00
|
|
|
const m::room room
|
2018-10-23 22:22:08 +02:00
|
|
|
{
|
2019-08-12 07:22:57 +02:00
|
|
|
create_user_room(user_id, room_id, contents)
|
2018-10-23 22:22:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return user;
|
|
|
|
}
|
2019-08-12 07:22:57 +02:00
|
|
|
|
|
|
|
ircd::m::room
|
|
|
|
ircd::m::create_user_room(const user::id &user_id,
|
|
|
|
const room::id &room_id,
|
|
|
|
const json::members &contents)
|
|
|
|
try
|
|
|
|
{
|
2019-10-01 05:50:58 +02:00
|
|
|
return create(room_id, me(), "user");
|
2019-08-12 07:22:57 +02:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
if(m::exists(room_id))
|
|
|
|
return room_id;
|
|
|
|
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "Failed to create user %s room %s :%s",
|
|
|
|
string_view{user_id},
|
|
|
|
string_view{room_id},
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
2019-09-16 20:02:50 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// user::user
|
|
|
|
//
|
|
|
|
|
2019-09-27 04:28:25 +02:00
|
|
|
/// Generates a user-room ID into buffer; see room_id() overload.
|
|
|
|
ircd::m::id::room::buf
|
|
|
|
ircd::m::user::room_id()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
ircd::m::id::room::buf buf;
|
|
|
|
return buf.assigned(room_id(buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This generates a room mxid for the "user's room" essentially serving as
|
|
|
|
/// a database mechanism for this specific user. This room_id is a hash of
|
|
|
|
/// the user's full mxid.
|
|
|
|
///
|
|
|
|
ircd::m::id::room
|
|
|
|
ircd::m::user::room_id(const mutable_buffer &buf)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(!empty(user_id));
|
|
|
|
const ripemd160::buf hash
|
|
|
|
{
|
|
|
|
ripemd160{user_id}
|
|
|
|
};
|
|
|
|
|
|
|
|
char b58[size(hash) * 2];
|
|
|
|
return
|
|
|
|
{
|
2020-08-10 12:51:09 +02:00
|
|
|
buf, b58::encode(b58, hash), origin(my())
|
2019-09-27 04:28:25 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::user::activate()
|
|
|
|
{
|
2019-10-03 03:30:14 +02:00
|
|
|
const m::user::room user_room
|
2019-09-27 04:28:25 +02:00
|
|
|
{
|
2019-10-03 03:30:14 +02:00
|
|
|
user_id
|
2019-09-27 04:28:25 +02:00
|
|
|
};
|
|
|
|
|
2019-10-03 03:30:14 +02:00
|
|
|
return send(user_room, m::me(), "ircd.account", "active", json::members
|
|
|
|
{
|
|
|
|
{ "value", true }
|
|
|
|
});
|
2019-09-27 04:28:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::user::deactivate()
|
|
|
|
{
|
2019-10-03 03:30:14 +02:00
|
|
|
const m::user::room user_room
|
2019-09-27 04:28:25 +02:00
|
|
|
{
|
2019-10-03 03:30:14 +02:00
|
|
|
user_id
|
2019-09-27 04:28:25 +02:00
|
|
|
};
|
|
|
|
|
2019-10-03 03:30:14 +02:00
|
|
|
return send(user_room, m::me(), "ircd.account", "active", json::members
|
|
|
|
{
|
|
|
|
{ "value", false }
|
|
|
|
});
|
2019-09-27 04:28:25 +02:00
|
|
|
}
|
|
|
|
|
2019-09-16 20:02:50 +02:00
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::user::password(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 }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::user::is_password(const string_view &password)
|
2019-09-28 10:19:25 +02:00
|
|
|
const try
|
2019-09-16 20:02:50 +02:00
|
|
|
{
|
|
|
|
char buf[64];
|
|
|
|
const auto supplied
|
|
|
|
{
|
|
|
|
gen_password_hash(buf, password)
|
|
|
|
};
|
|
|
|
|
|
|
|
bool ret{false};
|
|
|
|
const m::user::room user_room
|
|
|
|
{
|
|
|
|
user_id
|
|
|
|
};
|
|
|
|
|
|
|
|
const ctx::uninterruptible::nothrow ui;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::m::gen_password_hash(const mutable_buffer &out,
|
|
|
|
const string_view &supplied_password)
|
|
|
|
{
|
|
|
|
//TODO: ADD SALT
|
|
|
|
const sha256::buf hash
|
|
|
|
{
|
|
|
|
sha256{supplied_password}
|
|
|
|
};
|
|
|
|
|
2020-08-10 12:51:09 +02:00
|
|
|
return b64::encode_unpadded(out, hash);
|
2019-09-16 20:02:50 +02:00
|
|
|
}
|
2019-09-27 04:28:25 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// user::room
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::m::user::room::room(const m::user::id &user_id,
|
|
|
|
const vm::copts *const &copts,
|
|
|
|
const event::fetch::opts *const &fopts)
|
|
|
|
:room
|
|
|
|
{
|
|
|
|
m::user{user_id}, copts, fopts
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::user::room::room(const m::user &user,
|
|
|
|
const vm::copts *const &copts,
|
|
|
|
const event::fetch::opts *const &fopts)
|
|
|
|
:user{user}
|
|
|
|
,room_id{user.room_id()}
|
|
|
|
{
|
|
|
|
static_cast<m::room &>(*this) =
|
|
|
|
{
|
|
|
|
room_id, copts, fopts
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::user::room::is(const room::id &room_id,
|
|
|
|
const user::id &user_id)
|
|
|
|
{
|
|
|
|
const user::room user_room{user_id};
|
|
|
|
return user_room.room_id == room_id;
|
|
|
|
}
|