mirror of
https://github.com/matrix-construct/construct
synced 2024-11-29 10:12:39 +01:00
modules: Add initial: versions, register, login.
This commit is contained in:
parent
ae9b37939e
commit
f1c5257201
4 changed files with 82 additions and 195 deletions
|
@ -19,13 +19,12 @@
|
|||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <ircd/socket.h>
|
||||
|
||||
using namespace ircd;
|
||||
|
||||
db::handle logins
|
||||
mods::sym_ref<db::handle> accounts_ref
|
||||
{
|
||||
"login"
|
||||
"client_register",
|
||||
"accounts"
|
||||
};
|
||||
|
||||
resource login_resource
|
||||
|
@ -35,70 +34,66 @@ resource login_resource
|
|||
"they can use to authorize themself in subsequent requests. (3.2.2)"
|
||||
};
|
||||
|
||||
resource::method getter
|
||||
{login_resource, "POST", [](client &client,
|
||||
resource::request &request)
|
||||
-> resource::response
|
||||
resource::response
|
||||
login(client &client, const resource::request &request)
|
||||
try
|
||||
{
|
||||
char head[256], body[256];
|
||||
static const auto headfmt
|
||||
const auto user
|
||||
{
|
||||
"HTTP/1.1 200 OK\r\n"
|
||||
"Content-Length: %zu\r\n"
|
||||
"\r\n"
|
||||
unquote(request.at("user"))
|
||||
};
|
||||
|
||||
const json::obj in
|
||||
const auto password
|
||||
{
|
||||
json::doc{request.content}
|
||||
request.at("password")
|
||||
};
|
||||
|
||||
const std::string user(in["user"]);
|
||||
if(!logins.has(user))
|
||||
const auto type
|
||||
{
|
||||
log::debug("User [%s] doesn't exist", user.data());
|
||||
write(logins, user, in.state);
|
||||
unquote(request["type"])
|
||||
};
|
||||
|
||||
if(!type.empty() && type != "m.login.password")
|
||||
{
|
||||
throw m::error
|
||||
{
|
||||
"M_UNSUPPORTED", "Login type is not supported."
|
||||
};
|
||||
}
|
||||
else
|
||||
|
||||
db::handle &accounts(accounts_ref);
|
||||
accounts(user, [&password](const json::doc &user)
|
||||
{
|
||||
log::debug("User [%s] found", user.data());
|
||||
logins(user, [](const string_view &foo)
|
||||
{
|
||||
printf("dox [%s]\n", std::string(foo.data(), foo.size()).data());
|
||||
if(user["password"] != password)
|
||||
throw db::not_found();
|
||||
});
|
||||
|
||||
const auto access_token(123456);
|
||||
const auto home_server("cdc.z");
|
||||
const auto device_id("ABCDEF");
|
||||
return resource::response
|
||||
{
|
||||
client,
|
||||
{
|
||||
{ "user_id", user },
|
||||
{ "access_token", access_token },
|
||||
{ "home_server", home_server },
|
||||
{ "device_id", device_id },
|
||||
}
|
||||
|
||||
json::obj out;
|
||||
out["user_id"] = in["user"];
|
||||
out["access_token"] = "{\"la\":\"abc123\"}";
|
||||
out["home_server"] = "wewt";
|
||||
|
||||
const json::obj out2(out);
|
||||
|
||||
char buf[256];
|
||||
const json::doc outd(serialize(out2, buf, buf + sizeof(buf)));
|
||||
for(const auto &member : outd)
|
||||
std::cout << member.first << " => " << member.second << std::endl;
|
||||
|
||||
const size_t bodysz
|
||||
{
|
||||
print(body, sizeof(body), out)
|
||||
};
|
||||
|
||||
const int headsz
|
||||
}
|
||||
catch(const db::not_found &e)
|
||||
{
|
||||
throw m::error
|
||||
{
|
||||
snprintf(head, sizeof(head), headfmt, bodysz)
|
||||
http::FORBIDDEN, "M_FORBIDDEN", "Access denied."
|
||||
};
|
||||
}
|
||||
|
||||
const const_buffers iov
|
||||
{
|
||||
{ head, size_t(headsz) },
|
||||
{ body, bodysz },
|
||||
};
|
||||
|
||||
write(*client.sock, iov);
|
||||
return {};
|
||||
}};
|
||||
resource::method post
|
||||
{
|
||||
login_resource, "POST", login
|
||||
};
|
||||
|
||||
mapi::header IRCD_MODULE
|
||||
{
|
||||
|
|
|
@ -19,159 +19,46 @@
|
|||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <ircd/socket.h>
|
||||
using namespace ircd;
|
||||
|
||||
//ircd::db::handle logins("login", db::opt::READ_ONLY);
|
||||
|
||||
ircd::mapi::header IRCD_MODULE
|
||||
db::handle accounts
|
||||
{
|
||||
"registers the resource 'client/register' to handle requests"
|
||||
"accounts"
|
||||
};
|
||||
|
||||
ircd::resource register_resource
|
||||
resource::response
|
||||
handle_post(client &client,
|
||||
resource::request &request)
|
||||
{
|
||||
const auto access_token("abcdef");
|
||||
const auto home_server("cdc.z");
|
||||
const auto user_id("foo@bar");
|
||||
const auto refresh_token(12345);
|
||||
|
||||
return resource::response
|
||||
{
|
||||
client,
|
||||
{
|
||||
{ "access_token", access_token },
|
||||
{ "home_server", home_server },
|
||||
{ "user_id", user_id },
|
||||
{ "refresh_token", refresh_token },
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
resource register_resource
|
||||
{
|
||||
"/_matrix/client/r0/register",
|
||||
"Register for an account on this homeserver. (3.3.1)"
|
||||
};
|
||||
|
||||
auto generate_access_token([] { });
|
||||
auto generate_refresh_token([] { });
|
||||
void valid_username(const ircd::string_view &s);
|
||||
ircd::resource::response handle_post(ircd::client &client, ircd::resource::request &request);
|
||||
|
||||
template<class T>
|
||||
struct in
|
||||
resource::method post
|
||||
{
|
||||
register_resource, "POST", handle_post
|
||||
};
|
||||
|
||||
template<class T>
|
||||
struct out
|
||||
mapi::header IRCD_MODULE
|
||||
{
|
||||
"registers the resource 'client/register' to handle requests"
|
||||
};
|
||||
|
||||
template<class T>
|
||||
struct query
|
||||
{
|
||||
};
|
||||
|
||||
struct string
|
||||
{
|
||||
};
|
||||
|
||||
struct object
|
||||
{
|
||||
};
|
||||
|
||||
struct boolean
|
||||
{
|
||||
};
|
||||
|
||||
template<>
|
||||
struct in<string>
|
||||
{
|
||||
in(const char *const &name, const std::function<void (const ircd::string_view &)> &valid = nullptr) {}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct in<object>
|
||||
{
|
||||
in(const char *const &name) {}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct in<boolean>
|
||||
{
|
||||
in(const char *const &name) {}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct query<boolean>
|
||||
{
|
||||
template<class A, class B> query(A&&, B&&) {}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct out<string>
|
||||
{
|
||||
template<class T> out(const char *const &name, T&&) {}
|
||||
};
|
||||
|
||||
//
|
||||
// Inputs
|
||||
//
|
||||
in<string> username { "username", valid_username };
|
||||
in<string> password { "password" };
|
||||
in<object> auth { "auth" };
|
||||
in<string> session { "auth.session" };
|
||||
in<string> type { "auth.type" };
|
||||
in<boolean> bind_email { "bind_email" };
|
||||
|
||||
//
|
||||
// Queries
|
||||
//
|
||||
ircd::db::handle users { "users" };
|
||||
query<boolean> username_exists { users, username };
|
||||
|
||||
//
|
||||
// Commitments
|
||||
//
|
||||
|
||||
//
|
||||
// Outputs
|
||||
//
|
||||
out<string> user_id { "user_id", username };
|
||||
out<string> home_server { "home_server", "pitcock's playground" };
|
||||
out<string> access_token { "access_token", generate_access_token };
|
||||
out<string> refresh_token { "refresh_token", generate_refresh_token };
|
||||
|
||||
ircd::resource::method post
|
||||
{
|
||||
register_resource, "POST", handle_post,
|
||||
{
|
||||
//&username,
|
||||
//&bind_email,
|
||||
}
|
||||
};
|
||||
|
||||
ircd::resource::response
|
||||
handle_post(ircd::client &client,
|
||||
ircd::resource::request &request)
|
||||
{
|
||||
using namespace ircd;
|
||||
|
||||
const json::doc in{request.content};
|
||||
|
||||
json::obj hoo;
|
||||
hoo["kaa"] = "\"choo\"";
|
||||
|
||||
json::obj foo;
|
||||
foo["yea"] = "\"pie\"";
|
||||
foo["hoo"] = &hoo;
|
||||
|
||||
json::obj out;
|
||||
out["user_id"] = in["username"];
|
||||
out["access_token"] = "\"ABCDEFG\"";
|
||||
out["home_server"] = "\"dereferenced\"";
|
||||
out["refresh_token"] = "\"tokenizeddd\"";
|
||||
|
||||
return { client, out };
|
||||
}
|
||||
|
||||
void valid_username(const ircd::string_view &s)
|
||||
{
|
||||
using namespace ircd;
|
||||
|
||||
static conf::item<size_t> max_len
|
||||
{
|
||||
"client.register.username.max_len", 15
|
||||
};
|
||||
|
||||
const json::obj err
|
||||
{json::doc{R"({
|
||||
"errcode": "M_INVALID_USERNAME",
|
||||
"error": "Username length exceeds maximum"
|
||||
})"s}};
|
||||
|
||||
if(s.size() > max_len)
|
||||
throw m::error{err};
|
||||
}
|
||||
|
|
|
@ -23,7 +23,7 @@ using namespace ircd;
|
|||
|
||||
resource versions_resource
|
||||
{
|
||||
"/_matrix/client/r0/versions",
|
||||
"/_matrix/client/versions",
|
||||
"Gets the versions of the specification supported by the server (2.1)"
|
||||
};
|
||||
|
||||
|
|
|
@ -60,6 +60,7 @@ struct log::log listener::log
|
|||
};
|
||||
|
||||
listener::listener(const std::string &name)
|
||||
try
|
||||
:name
|
||||
{
|
||||
name
|
||||
|
@ -84,6 +85,10 @@ listener::listener(const std::string &name)
|
|||
ssl.use_certificate_file("/home/jason/cdc.z.cert", asio::ssl::context::pem);
|
||||
ssl.use_private_key_file("/home/jason/cdc.z.key", asio::ssl::context::pem);
|
||||
}
|
||||
catch(const boost::system::system_error &e)
|
||||
{
|
||||
throw error("listener: %s", e.what());
|
||||
}
|
||||
|
||||
bool
|
||||
listener::configured()
|
||||
|
|
Loading…
Reference in a new issue