feat: add auth step

This commit is contained in:
Jef Roosens 2023-05-29 23:37:23 +02:00
parent 16103f9b24
commit 1e42dc04dc
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
4 changed files with 27 additions and 1 deletions

View file

@ -97,3 +97,21 @@ bool http_loop_step_body_to_buf(event_loop_conn *conn) {
return ctx->req.body_received == ctx->req.body_len;
}
bool http_loop_step_auth(event_loop_conn *conn) {
http_loop_ctx *ctx = conn->ctx;
for (size_t i = 0; i < ctx->req.num_headers; i++) {
struct phr_header *header = &ctx->req.headers[i];
if ((strncmp("X-Api-Key", header->name, header->name_len) == 0) &&
(strncmp(header->value, ctx->g->api_key, header->value_len) == 0)) {
return true;
}
}
ctx->res.status = http_unauthorized;
conn->state = event_loop_conn_state_res;
return true;
}