Skip to content

Add optional API key authentication via --api-key flag#53

Open
zhaopengme wants to merge 1 commit intoantirez:mainfrom
zhaopengme:main
Open

Add optional API key authentication via --api-key flag#53
zhaopengme wants to merge 1 commit intoantirez:mainfrom
zhaopengme:main

Conversation

@zhaopengme
Copy link
Copy Markdown

This PR adds a lightweight authentication layer to the ds4 server.
When the server is started with --api-key <key>, all incoming HTTP requests must include a valid API key in either:

  • Authorization: Bearer <key>
  • X-Api-Key: <key>

Requests without a valid key receive a 401 Unauthorized response.

Implementation details:

  • Added api_key to server_config and server structs.
  • Updated HTTP request parsing to capture auth headers.
  • Added validation logic in client_main() before routing requests.
  • Updated CLI usage text to document the new flag.

Copilot AI review requested due to automatic review settings May 10, 2026 05:13
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds optional API key authentication to the ds4-server HTTP API, gated by a new --api-key CLI flag, so that requests must provide the key via Authorization: Bearer <key> or X-Api-Key: <key> and receive 401 Unauthorized otherwise.

Changes:

  • Added api_key to server_config and server, and wired --api-key through option parsing into server state.
  • Extended HTTP request parsing to extract an API key from auth headers.
  • Added an auth gate in client_main() that returns 401 on missing/incorrect keys, and updated CLI usage text.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread ds4_server.c
Comment on lines +6719 to +6744
/* Extract auth header for API key verification */
{
const char *hp = b.ptr, *h_end = b.ptr + (size_t)hend;
r->auth_header = NULL;
while (hp < h_end) {
const char *line_start = hp;
while (hp < h_end && *hp != '\n') hp++;
size_t line_len = (size_t)(hp - line_start);
if (line_len && line_start[line_len - 1] == '\r') line_len--;

if (line_len >= 21 && strncasecmp(line_start, "Authorization: Bearer ", 21) == 0) {
const char *v = line_start + 21;
while (v < line_start + line_len && isspace((unsigned char)*v)) v++;
size_t vlen = (size_t)(line_start + line_len - v);
if (vlen > 0) {
r->auth_header = xstrndup(v, vlen);
break;
}
}
if (line_len >= 11 && strncasecmp(line_start, "X-Api-Key: ", 11) == 0) {
const char *v = line_start + 11;
while (v < line_start + line_len && isspace((unsigned char)*v)) v++;
size_t vlen = (size_t)(line_start + line_len - v);
if (vlen > 0) {
r->auth_header = xstrndup(v, vlen);
break;
Comment thread ds4_server.c
Comment on lines +6738 to +6746
if (line_len >= 11 && strncasecmp(line_start, "X-Api-Key: ", 11) == 0) {
const char *v = line_start + 11;
while (v < line_start + line_len && isspace((unsigned char)*v)) v++;
size_t vlen = (size_t)(line_start + line_len - v);
if (vlen > 0) {
r->auth_header = xstrndup(v, vlen);
break;
}
}
Comment thread ds4_server.c
Comment on lines 6845 to +6858
http_request hr = {0};
if (!read_http_request(fd, &hr)) {
http_error(fd, 400, "bad HTTP request");
goto done;
}

/* Check API key if configured */
if (s->api_key) {
if (!hr.auth_header || strcmp(s->api_key, hr.auth_header) != 0) {
http_error(fd, 401, "unauthorized");
http_request_free(&hr);
goto done;
}
}
Comment thread ds4_server.c
Comment on lines +6852 to +6854
if (s->api_key) {
if (!hr.auth_header || strcmp(s->api_key, hr.auth_header) != 0) {
http_error(fd, 401, "unauthorized");
Comment thread ds4_server.c
Comment on lines +6851 to +6858
/* Check API key if configured */
if (s->api_key) {
if (!hr.auth_header || strcmp(s->api_key, hr.auth_header) != 0) {
http_error(fd, 401, "unauthorized");
http_request_free(&hr);
goto done;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants