Add optional API key authentication via --api-key flag#53
Open
zhaopengme wants to merge 1 commit intoantirez:mainfrom
Open
Add optional API key authentication via --api-key flag#53zhaopengme wants to merge 1 commit intoantirez:mainfrom
zhaopengme wants to merge 1 commit intoantirez:mainfrom
Conversation
There was a problem hiding this comment.
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_keytoserver_configandserver, and wired--api-keythrough 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 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 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 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 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 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; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 Unauthorizedresponse.Implementation details:
api_keytoserver_configandserverstructs.client_main()before routing requests.