-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Support Multiple API Keys #3450
Changes from all commits
fb0c441
6fc514c
86bae2c
e0e441c
c30515e
effb929
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 |
---|---|---|
|
@@ -249,19 +249,21 @@ func (a *{{{classname}}}Service) {{{nickname}}}(ctx context.Context{{#hasParams} | |
{{^isKeyInCookie}} | ||
if ctx != nil { | ||
// API Key Authentication | ||
if auth, ok := ctx.Value(ContextAPIKey).(APIKey); ok { | ||
var key string | ||
if auth.Prefix != "" { | ||
key = auth.Prefix + " " + auth.Key | ||
} else { | ||
key = auth.Key | ||
if auth, ok := ctx.Value(ContextAPIKeys).(map[string]APIKey); ok { | ||
if auth, ok := auth["{{keyParamName}}"]; ok { | ||
var key string | ||
if auth.Prefix != "" { | ||
key = auth.Prefix + " " + auth.Key | ||
} else { | ||
key = auth.Key | ||
} | ||
{{#isKeyInHeader}} | ||
localVarHeaderParams["{{keyParamName}}"] = key | ||
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. @nmuesch can we check to see if the key is not empty before adding to localVarHeaderParams or localVarQueryParams? 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. The reason is that sometimes the key can either be put in the header or the URL query string so the goal is to skip the API key in header or URL query string if the value is not set. We can address it in another PR later when someone reports the issue. |
||
{{/isKeyInHeader}} | ||
{{#isKeyInQuery}} | ||
localVarQueryParams.Add("{{keyParamName}}", key) | ||
{{/isKeyInQuery}} | ||
} | ||
{{#isKeyInHeader}} | ||
localVarHeaderParams["{{keyParamName}}"] = key | ||
{{/isKeyInHeader}} | ||
{{#isKeyInQuery}} | ||
localVarQueryParams.Add("{{keyParamName}}", key) | ||
{{/isKeyInQuery}} | ||
} | ||
} | ||
{{/isKeyInCookie}} | ||
|
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.
@nmuesch minor feedback. Is there a way we can keep the sample code?
It would be great to show the code to use the API key (single or multiple)
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.
Hey, thanks for the feedback. There was a brief discussion of this here #3450 (review) interested in your thoughts. The main reason I changed it was because it made the
example
code incorrect for any apps that have multiple keys. I couldn't figure out a way to show the full multi-key example using the template variables.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.
We can do it in the actual code sample instead and we can do it in another PR instead of this one as I don't want this PR to be opened for too long