feat(lander): re-add authentication using LNM
All checks were successful
ci/woodpecker/push/build Pipeline was successful

This commit is contained in:
Jef Roosens 2023-12-06 18:16:52 +01:00
parent 8dc8ef8e2d
commit 1a7686003c
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
4 changed files with 41 additions and 6 deletions

View file

@ -83,6 +83,8 @@ lnm_err lnm_http_loop_route_add(lnm_http_loop *hl, lnm_http_route *route);
lnm_err lnm_http_loop_run(lnm_http_loop *hl, uint16_t port);
void lnm_http_loop_set_api_key(lnm_http_loop *hl, const char *api_key);
/**
* Represents what state an HTTP loop request is currently in.
*/
@ -115,6 +117,7 @@ typedef struct lnm_http_loop_gctx {
lnm_http_ctx_init_fn ctx_init;
lnm_http_ctx_reset_fn ctx_reset;
lnm_http_ctx_free_fn ctx_free;
const char *api_key;
void *c;
} lnm_http_loop_gctx;
@ -130,4 +133,6 @@ typedef struct lnm_http_loop_ctx {
lnm_http_step_err lnm_http_loop_step_body_to_buf(lnm_http_conn *conn);
lnm_http_step_err lnm_http_loop_step_auth(lnm_http_conn *conn);
#endif

View file

@ -126,3 +126,8 @@ lnm_err lnm_http_loop_run(lnm_http_loop *hl, uint16_t port) {
LNM_RES(lnm_loop_setup(hl, port));
return lnm_loop_run(hl);
}
void lnm_http_loop_set_api_key(lnm_http_loop *hl, const char *api_key) {
lnm_http_loop_gctx *gctx = hl->gctx;
gctx->api_key = api_key;
}

View file

@ -1,5 +1,6 @@
#include <string.h>
#include "lnm/http/consts.h"
#include "lnm/http/loop.h"
#include "lnm/loop.h"
@ -22,3 +23,22 @@ lnm_http_step_err lnm_http_loop_step_body_to_buf(lnm_http_conn *conn) {
? lnm_http_step_err_done
: lnm_http_step_err_io_needed;
}
lnm_http_step_err lnm_http_loop_step_auth(lnm_http_conn *conn) {
lnm_http_loop_ctx *ctx = conn->ctx;
// If there's no API key, requests are always authorized
bool authorized = ctx->g->api_key == NULL;
const char *value;
size_t value_len;
if (!authorized && lnm_http_req_header_get_s(&value, &value_len, &ctx->req,
"X-Api-Key") == lnm_err_ok) {
authorized = (value_len == strlen(ctx->g->api_key)) &&
(memcmp(value, ctx->g->api_key, value_len) == 0);
}
ctx->res.status = authorized ? ctx->res.status : lnm_http_status_unauthorized;
return authorized ? lnm_http_step_err_done : lnm_http_step_err_res;
}