From fb0c44149b37e7c19d5696bef39fd62190b3daeb Mon Sep 17 00:00:00 2001 From: Nicholas Muesch Date: Wed, 24 Jul 2019 17:33:06 -0400 Subject: [PATCH 1/5] Support Multiple API Keys --- .../src/main/resources/go-experimental/README.mustache | 4 ++-- .../src/main/resources/go-experimental/api.mustache | 2 +- .../src/main/resources/go-experimental/configuration.mustache | 3 --- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/go-experimental/README.mustache b/modules/openapi-generator/src/main/resources/go-experimental/README.mustache index 869443a26edf..2db400b7fb80 100644 --- a/modules/openapi-generator/src/main/resources/go-experimental/README.mustache +++ b/modules/openapi-generator/src/main/resources/go-experimental/README.mustache @@ -31,7 +31,7 @@ go get github.com/antihax/optional Put the package under your project folder and add the following in import: ```golang -import "./{{packageName}}" +import sw "./{{packageName}}" ``` ## Documentation for API Endpoints @@ -61,7 +61,7 @@ Class | Method | HTTP request | Description Example ```golang -auth := context.WithValue(context.Background(), sw.ContextAPIKey, sw.APIKey{ +auth := context.WithValue(context.Background(), "{{keyParamName}}", sw.APIKey{ Key: "APIKEY", Prefix: "Bearer", // Omit if not necessary. }) diff --git a/modules/openapi-generator/src/main/resources/go-experimental/api.mustache b/modules/openapi-generator/src/main/resources/go-experimental/api.mustache index 2b04681ac91a..26a4688605e2 100644 --- a/modules/openapi-generator/src/main/resources/go-experimental/api.mustache +++ b/modules/openapi-generator/src/main/resources/go-experimental/api.mustache @@ -249,7 +249,7 @@ func (a *{{{classname}}}Service) {{{nickname}}}(ctx context.Context{{#hasParams} {{^isKeyInCookie}} if ctx != nil { // API Key Authentication - if auth, ok := ctx.Value(ContextAPIKey).(APIKey); ok { + if auth, ok := ctx.Value("{{keyParamName}}").(APIKey); ok { var key string if auth.Prefix != "" { key = auth.Prefix + " " + auth.Key diff --git a/modules/openapi-generator/src/main/resources/go-experimental/configuration.mustache b/modules/openapi-generator/src/main/resources/go-experimental/configuration.mustache index 81d3a41b7bdf..fc33d47226c9 100644 --- a/modules/openapi-generator/src/main/resources/go-experimental/configuration.mustache +++ b/modules/openapi-generator/src/main/resources/go-experimental/configuration.mustache @@ -24,9 +24,6 @@ var ( // ContextAccessToken takes a string oauth2 access token as authentication for the request. ContextAccessToken = contextKey("accesstoken") - - // ContextAPIKey takes an APIKey as authentication for the request - ContextAPIKey = contextKey("apikey") ) // BasicAuth provides basic http authentication to a request passed via context using ContextBasicAuth From 6fc514c779a6149e8f9a1fbd9936428f646cceba Mon Sep 17 00:00:00 2001 From: Nicholas Muesch Date: Mon, 19 Aug 2019 17:42:19 -0400 Subject: [PATCH 2/5] Use maps --- .../resources/go-experimental/api.mustache | 26 ++++++++++--------- .../go-experimental/configuration.mustache | 3 +++ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/go-experimental/api.mustache b/modules/openapi-generator/src/main/resources/go-experimental/api.mustache index 26a4688605e2..97d83fb88723 100644 --- a/modules/openapi-generator/src/main/resources/go-experimental/api.mustache +++ b/modules/openapi-generator/src/main/resources/go-experimental/api.mustache @@ -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("{{keyParamName}}").(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 + {{/isKeyInHeader}} + {{#isKeyInQuery}} + localVarQueryParams.Add("{{keyParamName}}", key) + {{/isKeyInQuery}} } - {{#isKeyInHeader}} - localVarHeaderParams["{{keyParamName}}"] = key - {{/isKeyInHeader}} - {{#isKeyInQuery}} - localVarQueryParams.Add("{{keyParamName}}", key) - {{/isKeyInQuery}} } } {{/isKeyInCookie}} diff --git a/modules/openapi-generator/src/main/resources/go-experimental/configuration.mustache b/modules/openapi-generator/src/main/resources/go-experimental/configuration.mustache index fc33d47226c9..36942c742518 100644 --- a/modules/openapi-generator/src/main/resources/go-experimental/configuration.mustache +++ b/modules/openapi-generator/src/main/resources/go-experimental/configuration.mustache @@ -24,6 +24,9 @@ var ( // ContextAccessToken takes a string oauth2 access token as authentication for the request. ContextAccessToken = contextKey("accesstoken") + + // ContextAPIKeys takes a string apikey as authentication for the request + ContextAPIKeys = contextKey("apiKeys") ) // BasicAuth provides basic http authentication to a request passed via context using ContextBasicAuth From e0e441cc18a2a00ae732ed6e1db27353eb65b0a6 Mon Sep 17 00:00:00 2001 From: Nicholas Muesch Date: Mon, 19 Aug 2019 18:10:28 -0400 Subject: [PATCH 3/5] Fix readme template --- .../src/main/resources/go-experimental/README.mustache | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/openapi-generator/src/main/resources/go-experimental/README.mustache b/modules/openapi-generator/src/main/resources/go-experimental/README.mustache index 2db400b7fb80..f1ea96a1d8d8 100644 --- a/modules/openapi-generator/src/main/resources/go-experimental/README.mustache +++ b/modules/openapi-generator/src/main/resources/go-experimental/README.mustache @@ -61,7 +61,7 @@ Class | Method | HTTP request | Description Example ```golang -auth := context.WithValue(context.Background(), "{{keyParamName}}", sw.APIKey{ +auth := context.WithValue(context.Background(), sw.ContextAPIKeys, sw.APIKey{ Key: "APIKEY", Prefix: "Bearer", // Omit if not necessary. }) From c30515e37bbd86fb79f4d5c1abb186183ae8a8fc Mon Sep 17 00:00:00 2001 From: Nicholas Muesch Date: Wed, 21 Aug 2019 12:38:52 -0400 Subject: [PATCH 4/5] Update readme --- .../resources/go-experimental/README.mustache | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/go-experimental/README.mustache b/modules/openapi-generator/src/main/resources/go-experimental/README.mustache index f1ea96a1d8d8..b8c69c3a9457 100644 --- a/modules/openapi-generator/src/main/resources/go-experimental/README.mustache +++ b/modules/openapi-generator/src/main/resources/go-experimental/README.mustache @@ -54,19 +54,12 @@ Class | Method | HTTP request | Description {{/authMethods}}{{#authMethods}}{{#last}} Authentication schemes defined for the API:{{/last}}{{/authMethods}} {{#authMethods}} -## {{{name}}} +### {{{name}}} -{{#isApiKey}}- **Type**: API key - -Example - -```golang -auth := context.WithValue(context.Background(), sw.ContextAPIKeys, sw.APIKey{ - Key: "APIKEY", - Prefix: "Bearer", // Omit if not necessary. -}) -r, err := client.Service.Operation(auth, args) -``` +{{#isApiKey}} +- **Type**: API key +- **API key parameter name**: {{{keyParamName}}} +- **Location**: {{#isKeyInQuery}}URL query string{{/isKeyInQuery}}{{#isKeyInHeader}}HTTP header{{/isKeyInHeader}} {{/isApiKey}} {{#isBasic}}- **Type**: HTTP basic authentication From effb929328e35b67cefc8c0520573536a3a8cd4e Mon Sep 17 00:00:00 2001 From: Nicholas Muesch Date: Thu, 22 Aug 2019 11:51:01 -0400 Subject: [PATCH 5/5] Address readme review --- .../src/main/resources/go-experimental/README.mustache | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modules/openapi-generator/src/main/resources/go-experimental/README.mustache b/modules/openapi-generator/src/main/resources/go-experimental/README.mustache index b8c69c3a9457..2f51e5cbccf0 100644 --- a/modules/openapi-generator/src/main/resources/go-experimental/README.mustache +++ b/modules/openapi-generator/src/main/resources/go-experimental/README.mustache @@ -61,6 +61,8 @@ Class | Method | HTTP request | Description - **API key parameter name**: {{{keyParamName}}} - **Location**: {{#isKeyInQuery}}URL query string{{/isKeyInQuery}}{{#isKeyInHeader}}HTTP header{{/isKeyInHeader}} +Note, each API key must be added to a map of `map[string]APIKey` where the key is: {{keyParamName}} and passed in as the auth context for each request. + {{/isApiKey}} {{#isBasic}}- **Type**: HTTP basic authentication