Skip to content

feat: an apiKey security scheme's key, sent where the scheme says - #83

Merged
MarkRosemaker merged 2 commits into
mainfrom
feat/api-key-scheme
Nov 24, 2026
Merged

MarkRosemaker merged 2 commits into
mainfrom
feat/api-key-scheme

Conversation

@MarkRosemaker

@MarkRosemaker MarkRosemaker commented Nov 24, 2026 •

Copy link
Copy Markdown
Owner

openapi-codegen read only http security schemes (bearer and basic). A client of an API that declares an apiKey scheme, such as Finnhub's, therefore never sent its key, and every call came back 401.

This builds on MarkRosemaker/cassette#4, now merged; this branch pins cassette at its main.

What's generated now

  • Client: an apiKey field, WithAPIKey(key), and NewClient reading {TITLE}_KEY (for example FINNHUB_API_KEY for "Finnhub API"), named the same way as the existing X-Api-Key path.
  • Operations: each operation that uses the scheme sends the key in the named header (req.Header.Set), query parameter (appended URL-escaped), or cookie (req.AddCookie), and only when the key is set. An operation that requires it and the client lacks it fails before sending, with "API key FINNHUB_API_KEY not provided". NewClient gives that error when the document's default security requires the key, and an operation checks itself when its security differs. An operation whose security makes the key optional sends it only if set.
  • Masking: in debug mode, do masks the recorded call with cassette.Interaction.Mask(cassette.MaskHeaders(name)), or MaskKeys for a query key, the moment it records it. So no path writes the key: not keep, failed, WithRecording, nor api.DecodeLenient. A cookie is already covered, since the Cookie header is masked by default.
  • Legacy path: where a spec has both a scheme and an X-Api-Key header parameter, the scheme wins, and the client never gets two apiKey fields. The bearer, basic and X-Api-Key paths are otherwise unchanged; the golden, debug and auth test packages regenerate identically.

Tests

  • New testdata/apikey, generated in debug mode: a header scheme with a required, an optional and a key-free operation. Its hand-written apikey_test.go checks:
    • a missing key fails NewClient with the variable's name;
    • the key arrives in X-Quotes-Token, from the environment or WithAPIKey, on the required and optional operations, never on the key-free one, and never in the query;
    • debug mode and WithRecording never write the key, but do write the header, masked. Removing the generated ia.Mask line makes both cases fail.
  • TestFromDocument_APIKey in ir/auth_test.go covers header, query and cookie, the env name, the default auth, per-operation auth, and that the scheme replaces an X-Api-Key parameter.
  • I also generated the query and cookie variants as a one-off check; both go vet clean.

README/features.md describes the scheme. make ready passes.

Part of the Finnhub API key handover. Next, go-api-libs/finnhub moves its scheme to the X-Finnhub-Token header and regenerates.

🤖 Generated with Claude Code

https://claude.ai/code/session_019QCb1AfnATvpBQCH3PrX7Y

openapi-codegen read only http schemes, bearer and basic, so a client of
an API that declares an apiKey scheme, such as Finnhub's, never sent its
key and every call was refused. An apiKey scheme now gives the client an
API key, read from {TITLE}_KEY or given with WithAPIKey, which every
operation that takes it sends in the header, query parameter or cookie the
scheme names, and an operation that requires it fails without it, before
sending. Debug mode masks the key in each call it records, with cassette's
new Interaction.Mask, so no recording holds it. A scheme replaces an
X-Api-Key header parameter, so a client never has two keys.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019QCb1AfnATvpBQCH3PrX7Y
@MarkRosemaker
MarkRosemaker merged commit 59168f7 into main Nov 24, 2026
1 check passed
@MarkRosemaker
MarkRosemaker deleted the feat/api-key-scheme branch November 24, 2026 00:58
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