Issuing a key worked; getting it to the person it was issued for did not. The operator had a secret on their terminal and no way to send it that did not leave a bearer credential in a chat log forever. `keys create --claim` prints a link instead. GET shows a button and consumes nothing; POST reveals the key and burns the link. That split is not politeness — chat clients, mail scanners and link previewers fetch URLs the moment they are posted, so a GET that consumed the claim would mean the first thing to unfurl the link destroys the key before anyone reads it. A pending claim has to hold the key somewhere, which would otherwise put a working credential in the database and undo what hashing bought. So the key is sealed with a key derived from the link's own token, and the token is never stored — only its hash. A database leak yields two useless hashes and a blob nothing in the database can open. HKDF rather than the raw token, so the value used for lookup can never become the value that decrypts. Consuming is one UPDATE moving claimed_at off NULL, so two people opening the same link produce one key rather than two copies. A used link, an expired one and a token that was never real all render identically, because saying which confirms that a token was once valid. The page is no-store, no-referrer and noindex: the URL is itself the credential. `GET /me` reports a key's own limits and spend, so nobody has to ask an operator to read a dashboard they cannot reach. Outside /v1, because it is not part of the OpenAI API. docs/onboarding.md is written for whoever operates this, including the parts that are deliberately absent — no self-service, no SSO, and why. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
cd3233ffe8
commit
3ed211b2f4
16 files changed
+1164
-23
No files matched your search
+45
-8
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/base32"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"text/tabwriter"
|
||||
@@ -78,15 +79,21 @@ func newKeyInitCmd(open opener) *cobra.Command {
|
||||
}
|
||||
|
||||
func newKeyCreateCmd(open opener) *cobra.Command {
|
||||
var project, band, profile string
|
||||
var expires time.Duration
|
||||
var project, band, profile, baseURL string
|
||||
var expires, claimTTL time.Duration
|
||||
var claim bool
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "create <name>",
|
||||
Short: "Issue an API key",
|
||||
Long: "The key is printed once and never again: only its hash is stored, so there\n" +
|
||||
"is nothing to recover it from. That is the point — a leaked database hands\n" +
|
||||
"over no working credential.",
|
||||
"over no working credential.\n\n" +
|
||||
"With --claim it prints a one-time link instead. Send the link rather than\n" +
|
||||
"the key: a key pasted into a chat window stays in that chat window forever,\n" +
|
||||
"while a link works once and then expires. The key is sealed with the link's\n" +
|
||||
"own token, which is not stored, so a pending claim is not a usable\n" +
|
||||
"credential sitting in the database either.",
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
s, err := open(cmd.Context())
|
||||
@@ -109,12 +116,32 @@ func newKeyCreateCmd(open opener) *cobra.Command {
|
||||
project, err)
|
||||
}
|
||||
|
||||
// The secret goes to stdout and everything else to stderr, so
|
||||
// `keys create ... > key.txt` captures the key and nothing else.
|
||||
// Cobra's Print writes to stderr, which would have made the one
|
||||
// output that matters the one that cannot be piped.
|
||||
fmt.Fprintln(cmd.OutOrStdout(), secret)
|
||||
// The one output that matters goes to stdout and everything else to
|
||||
// stderr, so `keys create ... > out.txt` captures it and nothing
|
||||
// else. Cobra's Print writes to stderr, which would have made the
|
||||
// useful half the half that cannot be piped.
|
||||
out := cmd.OutOrStdout()
|
||||
|
||||
if claim {
|
||||
if baseURL == "" {
|
||||
return errors.New("--claim needs --base-url, the address users reach this gateway at")
|
||||
}
|
||||
token, err := store.NewClaimToken()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.CreateClaim(cmd.Context(), k.ID, token, secret, claimTTL); err != nil {
|
||||
return err
|
||||
}
|
||||
link := strings.TrimRight(baseURL, "/") + "/claim/" + token
|
||||
fmt.Fprintln(out, link)
|
||||
cmd.PrintErrf("\nid %s\nproject %s\nexpires %s\n",
|
||||
k.ID, k.ProjectID, time.Now().Add(orDefault(claimTTL, store.ClaimTTL)).Format(time.RFC3339))
|
||||
cmd.PrintErrln("\nSend the link. It works once, and the key is shown only to whoever opens it.")
|
||||
return nil
|
||||
}
|
||||
|
||||
fmt.Fprintln(out, secret)
|
||||
cmd.PrintErrf("\nid %s\nproject %s\n", k.ID, k.ProjectID)
|
||||
if !k.ExpiresAt.IsZero() {
|
||||
cmd.PrintErrf("expires %s\n", k.ExpiresAt.Format(time.RFC3339))
|
||||
@@ -127,9 +154,19 @@ func newKeyCreateCmd(open opener) *cobra.Command {
|
||||
cmd.Flags().StringVar(&band, "band", "", "admission band; empty inherits")
|
||||
cmd.Flags().StringVar(&profile, "profile", "", "routing profile; empty inherits")
|
||||
cmd.Flags().DurationVar(&expires, "expires-in", 0, "lifetime; 0 never expires")
|
||||
cmd.Flags().BoolVar(&claim, "claim", false, "print a one-time link instead of the key")
|
||||
cmd.Flags().StringVar(&baseURL, "base-url", "", "public address of the gateway, for --claim")
|
||||
cmd.Flags().DurationVar(&claimTTL, "claim-ttl", store.ClaimTTL, "how long the link stays usable")
|
||||
return cmd
|
||||
}
|
||||
|
||||
func orDefault(d, fallback time.Duration) time.Duration {
|
||||
if d <= 0 {
|
||||
return fallback
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
func newKeyListCmd(open opener) *cobra.Command {
|
||||
var project string
|
||||
cmd := &cobra.Command{
|
||||
|
||||
Reference in new issue
Block a user