Parsee/src/AS.c

59 lines
1.2 KiB
C

#include <AS.h>
#include <Cytoplasm/Memory.h>
#include <Cytoplasm/Str.h>
#include <Cytoplasm/Log.h>
#include <Cytoplasm/Uri.h>
#include <string.h>
#include <stdlib.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))
{
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(const ParseeConfig *data, HttpClientContext *ctx)
{
char *bearer;
if (!data || !ctx)
{
return;
}
bearer = StrConcat(2, "Bearer ", data->as_token);
HttpRequestHeader(ctx, "Authorization", bearer);
Free(bearer);
}