Repository navigation
feat: an apiKey security scheme's key, sent where the scheme says - #83
Merged
Merged
Conversation
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
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019QCb1AfnATvpBQCH3PrX7Y
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.
openapi-codegen read only
httpsecurity schemes (bearer and basic). A client of an API that declares anapiKeyscheme, 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
apiKeyfield,WithAPIKey(key), andNewClientreading{TITLE}_KEY(for exampleFINNHUB_API_KEYfor "Finnhub API"), named the same way as the existingX-Api-Keypath.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".NewClientgives 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.domasks the recorded call withcassette.Interaction.Mask(cassette.MaskHeaders(name)), orMaskKeysfor a query key, the moment it records it. So no path writes the key: notkeep,failed,WithRecording, norapi.DecodeLenient. A cookie is already covered, since theCookieheader is masked by default.X-Api-Keyheader parameter, the scheme wins, and the client never gets twoapiKeyfields. The bearer, basic andX-Api-Keypaths are otherwise unchanged; the golden, debug and auth test packages regenerate identically.Tests
testdata/apikey, generated in debug mode: a header scheme with a required, an optional and a key-free operation. Its hand-writtenapikey_test.gochecks:NewClientwith the variable's name;X-Quotes-Token, from the environment orWithAPIKey, on the required and optional operations, never on the key-free one, and never in the query;WithRecordingnever write the key, but do write the header, masked. Removing the generatedia.Maskline makes both cases fail.TestFromDocument_APIKeyinir/auth_test.gocovers header, query and cookie, the env name, the default auth, per-operation auth, and that the scheme replaces anX-Api-Keyparameter.go vetclean.README/features.mddescribes the scheme.make readypasses.Part of the Finnhub API key handover. Next, go-api-libs/finnhub moves its scheme to the
X-Finnhub-Tokenheader and regenerates.🤖 Generated with Claude Code
https://claude.ai/code/session_019QCb1AfnATvpBQCH3PrX7Y