0
0
Fork 0
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:
Jason Volk 2017-03-20 19:28:00 -07:00
parent ae9b37939e
commit f1c5257201
4 changed files with 82 additions and 195 deletions

View file

@ -19,13 +19,12 @@
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
#include <ircd/socket.h>
using namespace ircd; using namespace ircd;
db::handle logins mods::sym_ref<db::handle> accounts_ref
{ {
"login" "client_register",
"accounts"
}; };
resource login_resource resource login_resource
@ -35,70 +34,66 @@ resource login_resource
"they can use to authorize themself in subsequent requests. (3.2.2)" "they can use to authorize themself in subsequent requests. (3.2.2)"
}; };
resource::method getter resource::response
{login_resource, "POST", [](client &client, login(client &client, const resource::request &request)
resource::request &request) try
-> resource::response
{ {
char head[256], body[256]; const auto user
static const auto headfmt
{
"HTTP/1.1 200 OK\r\n"
"Content-Length: %zu\r\n"
"\r\n"
};
const json::obj in
{ {
json::doc{request.content} unquote(request.at("user"))
}; };
const std::string user(in["user"]); const auto password
if(!logins.has(user))
{ {
log::debug("User [%s] doesn't exist", user.data()); request.at("password")
write(logins, user, in.state); };
}
else const auto type
{ {
log::debug("User [%s] found", user.data()); unquote(request["type"])
logins(user, [](const string_view &foo) };
if(!type.empty() && type != "m.login.password")
{
throw m::error
{ {
printf("dox [%s]\n", std::string(foo.data(), foo.size()).data()); "M_UNSUPPORTED", "Login type is not supported."
}); };
} }
json::obj out; db::handle &accounts(accounts_ref);
out["user_id"] = in["user"]; accounts(user, [&password](const json::doc &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) if(user["password"] != password)
}; throw db::not_found();
});
const int headsz const auto access_token(123456);
const auto home_server("cdc.z");
const auto device_id("ABCDEF");
return resource::response
{ {
snprintf(head, sizeof(head), headfmt, bodysz) client,
{
{ "user_id", user },
{ "access_token", access_token },
{ "home_server", home_server },
{ "device_id", device_id },
}
}; };
}
const const_buffers iov catch(const db::not_found &e)
{
throw m::error
{ {
{ head, size_t(headsz) }, http::FORBIDDEN, "M_FORBIDDEN", "Access denied."
{ body, bodysz },
}; };
}
write(*client.sock, iov); resource::method post
return {}; {
}}; login_resource, "POST", login
};
mapi::header IRCD_MODULE mapi::header IRCD_MODULE
{ {

View file

@ -19,159 +19,46 @@
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
#include <ircd/socket.h> using namespace ircd;
//ircd::db::handle logins("login", db::opt::READ_ONLY); db::handle accounts
ircd::mapi::header IRCD_MODULE
{ {
"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", "/_matrix/client/r0/register",
"Register for an account on this homeserver. (3.3.1)" "Register for an account on this homeserver. (3.3.1)"
}; };
auto generate_access_token([] { }); resource::method post
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
{ {
register_resource, "POST", handle_post
}; };
template<class T> mapi::header IRCD_MODULE
struct out
{ {
"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};
}

View file

@ -23,7 +23,7 @@ using namespace ircd;
resource versions_resource resource versions_resource
{ {
"/_matrix/client/r0/versions", "/_matrix/client/versions",
"Gets the versions of the specification supported by the server (2.1)" "Gets the versions of the specification supported by the server (2.1)"
}; };

View file

@ -60,6 +60,7 @@ struct log::log listener::log
}; };
listener::listener(const std::string &name) listener::listener(const std::string &name)
try
:name :name
{ {
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_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); 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 bool
listener::configured() listener::configured()