-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add --auth-key CLI flag and wire auth into HTTP server #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |
| /// GET /context/:col smart context discovery (q, limit query params) | ||
| const std = @import("std"); | ||
| const activity = @import("activity.zig"); | ||
| const auth = @import("auth.zig"); | ||
| const collection = @import("collection.zig"); | ||
| const Database = collection.Database; | ||
|
|
||
|
|
@@ -233,6 +234,14 @@ fn dispatch(srv: *Server, raw: []const u8, alloc: std.mem.Allocator) usize { | |
| return ok(getBodyBuf()[0..fbs.pos]); | ||
| } | ||
|
|
||
| // ── Auth gate — public endpoints above, protected endpoints below ──── | ||
| if (srv.db.auth.isEnabled()) { | ||
| const api_key = auth.AuthStore.extractHttpKey(raw) orelse | ||
| return err(401, "unauthorized — missing X-Api-Key header"); | ||
|
Comment on lines
+239
to
+240
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This auth gate depends on Useful? React with 👍 / 👎. |
||
| if (srv.db.auth.verify(api_key) == null) | ||
| return err(401, "unauthorized — invalid API key"); | ||
| } | ||
|
|
||
| if (std.mem.eql(u8, path, "/billing") and std.mem.eql(u8, method, "GET")) | ||
| return handleBillingLog(srv); | ||
|
|
||
|
|
@@ -760,6 +769,7 @@ fn err(code: u16, msg: []const u8) usize { | |
| const body = std.fmt.bufPrint(&scratch, "{{\"error\":\"{s}\"}}", .{msg}) catch msg; | ||
| const status = switch (code) { | ||
| 400 => "Bad Request", | ||
| 401 => "Unauthorized", | ||
| 429 => "Too Many Requests", | ||
| 404 => "Not Found", | ||
| else => "Internal Server Error", | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding the CLI key here enables auth state globally, but only the HTTP path checks it;
WireServerstill executesINSERT/GET/UPDATE/DELETE/SCANwithout any auth handshake or key verification. In the default--wiremode (or--bothfor wire clients), users can still read/write data anonymously even after starting with--auth-key, which violates the flag contract and leaves production deployments unintentionally exposed.Useful? React with 👍 / 👎.