Parsee/src/AS.c

186 lines
4.1 KiB
C

#include <AS.h>
#include <Cytoplasm/Memory.h>
#include <Cytoplasm/Str.h>
#include <Cytoplasm/Log.h>
#include <string.h>
#include <Matrix.h>
HashMap *
ASVerifyRequest(ParseeHttpArg *arg)
{
HashMap *ret = NULL;
HashMap *params;
char *authorisation;
if (!arg)
{
return NULL;
}
params = HttpRequestHeaders(arg->ctx);
authorisation = HashMapGet(params, "authorization");
if (!authorisation || strncmp(authorisation, "Bearer ", 7))
{
Log(LOG_INFO, "Bad auth %s", authorisation);
HttpResponseStatus(arg->ctx, HTTP_FORBIDDEN);
ret = MatrixCreateError("M_MISSING_TOKEN", "No 'hs_token' given in.");
goto end;
}
authorisation += 7;
if (!StrEquals(authorisation, arg->data->config->hs_token))
{
HttpResponseStatus(arg->ctx, HTTP_FORBIDDEN);
ret = MatrixCreateError("M_FORBIDDEN","Incorrect authorisation given");
goto end;
}
end:
return ret;
}
void
ASAuthenticateRequest(ParseeConfig *data, HttpClientContext *ctx)
{
char *bearer;
if (!data || !ctx)
{
return;
}
bearer = StrConcat(2, "Bearer ", data->as_token);
HttpRequestHeader(ctx, "Authorization", bearer);
Free(bearer);
}
bool
ASRegisterUser(ParseeConfig *conf, char *user)
{
HttpClientContext *ctx = NULL;
HashMap *json = NULL;
HttpStatus status;
if (!conf || !user)
{
return false;
}
/* Create user. We don't actually care about the value as we can
* masquerade, as long as it exists. */
ctx = ParseeCreateRequest(
conf,
HTTP_POST, "/_matrix/client/v3/register"
);
json = HashMapCreate();
HashMapSet(json,"type",JsonValueString("m.login.application_service"));
HashMapSet(json,"username",JsonValueString(user));
ASAuthenticateRequest(conf, ctx);
status = ParseeSetRequestJSON(ctx, json);
HttpClientContextFree(ctx);
JsonFree(json);
return status == HTTP_OK;
}
void
ASPing(ParseeConfig *conf)
{
HttpClientContext *ctx = NULL;
HashMap *json = NULL;
char *path;
if (!conf)
{
return;
}
Log(LOG_NOTICE, "Pinging...");
path = StrConcat(3,
"/_matrix/client/v1/appservice/",
"Parsee%20XMPP",
"/ping"
);
ctx = ParseeCreateRequest(
conf,
HTTP_POST, path
);
Free(path);
json = HashMapCreate();
ASAuthenticateRequest(conf, ctx);
ParseeSetRequestJSON(ctx, json);
HttpClientContextFree(ctx);
JsonFree(json);
}
void
ASJoin(ParseeConfig *conf, char *id, char *masquerade)
{
HttpClientContext *ctx = NULL;
HashMap *json = NULL, *params_obj;
char *path, *params;
if (!conf || !id)
{
return;
}
params_obj = HashMapCreate();
if (masquerade)
{
HashMapSet(params_obj, "user_id", masquerade);
}
params = HttpParamEncode(params_obj);
HashMapFree(params_obj);
path = StrConcat(4,
"/_matrix/client/v3/rooms/", id, "/join?",
params
);
Free(params);
ctx = ParseeCreateRequest(
conf,
HTTP_POST, path
);
Free(path);
json = HashMapCreate();
ASAuthenticateRequest(conf, ctx);
ParseeSetRequestJSON(ctx, json);
HttpClientContextFree(ctx);
JsonFree(json);
}
void
ASSend(ParseeConfig *conf, char *id, char *user, char *type, HashMap *c)
{
HttpClientContext *ctx = NULL;
HashMap *json = NULL, *params_obj;
char *path, *params;
char *txn;
if (!conf || !id || !type || !c)
{
return;
}
txn = StrRandom(16);
params_obj = HashMapCreate();
if (user)
{
HashMapSet(params_obj, "user_id", user);
}
params = HttpParamEncode(params_obj);
HashMapFree(params_obj);
path = StrConcat(8,
"/_matrix/client/v3/rooms/",
id, "/send/", type, "/", txn, "?",
params
);
Log(LOG_INFO, "Sending %s", path);
Free(params);
Free(txn);
ctx = ParseeCreateRequest(conf, HTTP_PUT, path);
Free(path);
json = HashMapCreate();
ASAuthenticateRequest(conf, ctx);
Log(LOG_INFO, "%d", ParseeSetRequestJSON(ctx, c));
HttpClientContextFree(ctx);
JsonFree(c);
}