-
Notifications
You must be signed in to change notification settings - Fork 7
feat(user-api): self-service API keys via scoped Authful provisioning (DEV-950) #2047
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
80ca756
feat(user-api): self-service API keys brokered through Authful (DEV-950)
brunod-e 0ec86b8
Merge branch 'feat/user-drafts-cutover' into feat/user-api-keys
brunod-e 8bd8442
Merge branch 'feat/user-drafts-cutover' into feat/user-api-keys
brunod-e 71111b2
Merge branch 'feat/user-drafts-cutover' into feat/user-api-keys
brunod-e 564170d
Merge branch 'feat/user-drafts-cutover' into feat/user-api-keys
brunod-e 553b07c
Merge branch 'feat/user-drafts-cutover' into feat/user-api-keys
brunod-e 8fb3612
test(user-api): adapt api-keys test to per-host auth resolver
brunod-e bfd6fe5
feat(user-api): surface API key lastUsedAt from Authful
brunod-e 6fd71b9
feat(user-api): mint user API keys unbounded (free, no metering for now)
brunod-e 997d626
Merge branch 'feat/user-drafts-cutover' into feat/user-api-keys
brunod-e 46f5a30
Merge branch 'feat/user-drafts-cutover' into feat/user-api-keys
brunod-e e7866c9
Merge branch 'feat/user-drafts-cutover' into feat/user-api-keys
brunod-e File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "@anticapture/authful": minor | ||
| "@anticapture/user-api": minor | ||
| --- | ||
|
|
||
| Add self-service API keys (DEV-950). Authful gains an optional scoped provisioning key that may only mint/revoke `user:*` tenants and cannot list all tenants (the admin key stays unrestricted). The User API brokers end-user keys through it: `POST/GET/DELETE /me/api-keys` (session-authenticated) mint into Authful under tenant `user:<userId>`, return the plaintext exactly once, and store only ownership (never the secret) — with a per-user quota and Authful-first revocation. Both surfaces stay disabled until their env is configured. |
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import { createHash, timingSafeEqual } from "node:crypto"; | ||
|
|
||
| import { createMiddleware } from "hono/factory"; | ||
|
|
||
| /** | ||
| * Tenant prefix reserved for end-user API keys. The provisioning key (used by | ||
| * the User API to broker keys on behalf of signed-in users) may only mint and | ||
| * revoke tokens under this prefix — never a first-party tenant like "uniswap". | ||
| */ | ||
| export const USER_TENANT_PREFIX = "user:"; | ||
|
|
||
| export type AuthScope = "admin" | "provisioning"; | ||
|
|
||
| // Global context-variable typing so handlers can read c.get("authScope") | ||
| // without threading a custom Env generic through the whole app (which would | ||
| // break controllers that take the default-typed app). | ||
| declare module "hono" { | ||
| interface ContextVariableMap { | ||
| authScope: AuthScope; | ||
| } | ||
| } | ||
|
|
||
| const digest = (value: string) => createHash("sha256").update(value).digest(); | ||
|
|
||
| // Constant-time compare over fixed-length digests (raw strings differ in | ||
| // length, which timingSafeEqual rejects and which itself leaks length). | ||
| const safeEqual = (a: string, b: string) => | ||
| timingSafeEqual(digest(a), digest(b)); | ||
|
|
||
| const bearer = (header: string | undefined): string | undefined => | ||
| header?.startsWith("Bearer ") ? header.slice("Bearer ".length) : undefined; | ||
|
|
||
| /** | ||
| * Authenticates the token-management surface as one of two scopes: | ||
| * - `admin` — the full admin key; unrestricted (mint/list/revoke any tenant). | ||
| * - `provisioning` — the optional provisioning key; restricted to `user:*` | ||
| * tenants and forbidden from listing (enforced by the controller via | ||
| * `c.get("authScope")`). | ||
| */ | ||
| export const scopedTokenAuth = (opts: { | ||
| adminApiKey: string; | ||
| provisioningApiKey?: string; | ||
| }) => | ||
| createMiddleware(async (c, next) => { | ||
| const token = bearer(c.req.header("Authorization")); | ||
| if (!token) return c.json({ error: "unauthorized" }, 401); | ||
|
|
||
| if (safeEqual(token, opts.adminApiKey)) { | ||
| c.set("authScope", "admin"); | ||
| return next(); | ||
| } | ||
| if (opts.provisioningApiKey && safeEqual(token, opts.provisioningApiKey)) { | ||
| c.set("authScope", "provisioning"); | ||
| return next(); | ||
| } | ||
| return c.json({ error: "unauthorized" }, 401); | ||
| }); | ||
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| CREATE TABLE "user_api_keys" ( | ||
| "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, | ||
| "user_id" text NOT NULL, | ||
| "authful_token_id" uuid NOT NULL, | ||
| "label" text NOT NULL, | ||
| "created_at" timestamp with time zone DEFAULT now() NOT NULL, | ||
| "revoked_at" timestamp with time zone | ||
| ); | ||
| --> statement-breakpoint | ||
| ALTER TABLE "user_api_keys" ADD CONSTRAINT "user_api_keys_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint | ||
| CREATE INDEX "user_api_keys_user_id_index" ON "user_api_keys" USING btree ("user_id"); |
Oops, something went wrong.
Oops, something went wrong.
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.
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.
When Authful is deployed with
PROVISIONING_API_KEYaccidentally set to the same value asADMIN_API_KEY, this branch classifies that credential asadminbefore the provisioning check runs, so the User API’s supposedly scoped key can list tokens and mint/revoke first-party tenants. Since the new rollout depends on the provisioning key being restricted touser:*, reject equal admin/provisioning secrets at startup rather than silently granting admin scope.Useful? React with 👍 / 👎.