forked from Telodendria/Telodendria
Begin implementing a few helper functions
This commit is contained in:
parent
9a31b1f350
commit
836b61dc42
2 changed files with 64 additions and 1 deletions
59
src/Matrix.c
59
src/Matrix.c
|
@ -25,6 +25,7 @@
|
||||||
#include <Matrix.h>
|
#include <Matrix.h>
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
#include <Memory.h>
|
#include <Memory.h>
|
||||||
#include <HttpServer.h>
|
#include <HttpServer.h>
|
||||||
|
@ -402,5 +403,61 @@ MatrixUserInteractiveAuth(HttpServerContext * context, Db * db,
|
||||||
DbUnlock(db, ref);
|
DbUnlock(db, ref);
|
||||||
DbDelete(db, 2, "user_interactive", sessionStr);
|
DbDelete(db, 2, "user_interactive", sessionStr);
|
||||||
|
|
||||||
return NULL; /* All good, auth successful */
|
return NULL; /* All good, auth successful */
|
||||||
|
}
|
||||||
|
|
||||||
|
HashMap *
|
||||||
|
MatrixAuthenticate(HttpServerContext * context, Db * db)
|
||||||
|
{
|
||||||
|
HashMap *params;
|
||||||
|
char *token;
|
||||||
|
|
||||||
|
(void) db; /* Silence warning about unused var;
|
||||||
|
* we'll use it eventually. */
|
||||||
|
|
||||||
|
params = HttpRequestHeaders(context);
|
||||||
|
token = HashMapGet(params, "authorization");
|
||||||
|
|
||||||
|
if (token)
|
||||||
|
{
|
||||||
|
/* If the header was provided but it's not given correctly,
|
||||||
|
* that's an error */
|
||||||
|
if (strncmp(token, "Bearer ", 7) != 0)
|
||||||
|
{
|
||||||
|
HttpResponseStatus(context, HTTP_UNAUTHORIZED);
|
||||||
|
return MatrixErrorCreate(M_MISSING_TOKEN);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Seek past "Bearer" */
|
||||||
|
token += 8;
|
||||||
|
|
||||||
|
/* Seek past any spaces between "Bearer" and the token */
|
||||||
|
while (*token && isspace(*token))
|
||||||
|
{
|
||||||
|
token++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Header was not provided, we must check for ?access_token */
|
||||||
|
params = HttpRequestParams(context);
|
||||||
|
token = HashMapGet(params, "access_token");
|
||||||
|
|
||||||
|
if (!token)
|
||||||
|
{
|
||||||
|
HttpResponseStatus(context, HTTP_UNAUTHORIZED);
|
||||||
|
return MatrixErrorCreate(M_MISSING_TOKEN);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* TODO: Check that "token" is actually valid */
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
HashMap *
|
||||||
|
MatrixRateLimit(HttpServerContext *context, Db *db)
|
||||||
|
{
|
||||||
|
/* TODO: Implement rate limiting */
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
|
@ -83,4 +83,10 @@ extern HashMap *
|
||||||
extern HashMap *
|
extern HashMap *
|
||||||
MatrixUserInteractiveAuth(HttpServerContext *, Db *, HashMap *);
|
MatrixUserInteractiveAuth(HttpServerContext *, Db *, HashMap *);
|
||||||
|
|
||||||
|
extern HashMap *
|
||||||
|
MatrixAuthenticate(HttpServerContext *, Db *);
|
||||||
|
|
||||||
|
extern HashMap *
|
||||||
|
MatrixRateLimit(HttpServerContext *, Db *);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue