From 4e6ff1a5674c3a1da933f97965746c3da5ee52a5 Mon Sep 17 00:00:00 2001 From: Guo Huang Date: Mon, 11 Apr 2016 22:25:02 -0700 Subject: [PATCH] added api prefix for authentication --- .../src/main/resources/go/api.mustache | 7 +++++++ .../main/resources/go/configuration.mustache | 17 ++++++++++++++++- .../client/petstore/go/swagger/Configuration.go | 17 ++++++++++++++++- samples/client/petstore/go/swagger/PetApi.go | 11 +++++++++++ samples/client/petstore/go/swagger/StoreApi.go | 4 ++++ 5 files changed, 54 insertions(+), 2 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/go/api.mustache b/modules/swagger-codegen/src/main/resources/go/api.mustache index a7c09eae4cd..b1bd9bb0fc6 100644 --- a/modules/swagger-codegen/src/main/resources/go/api.mustache +++ b/modules/swagger-codegen/src/main/resources/go/api.mustache @@ -44,6 +44,13 @@ func (a {{classname}}) {{nickname}} ({{#allParams}}{{paramName}} {{{dataType}}}{ _sling := sling.New().{{httpMethod}}(a.Configuration.BasePath) {{#authMethods}}// authentication ({{name}}) required + {{#isApiKey}}{{#isKeyInHeader}} + // set key with prefix in header + _sling.Set("{{keyParamName}}", a.Configuration.GetApiKeyWithPrefix("{{keyParamName}}") + {{/isKeyInHeader}}{{#isKeyInQuery}} + // set key with prefix in querystring + _sling.Set("{{keyParamName}}", a.Configuration.GetApiKeyWithPrefix("{{keyParamName}}") + {{/isKeyInQuery}}{{/isApiKey}} {{#isBasic}} // http basic authentication required if a.Configuration.Username != "" || a.Configuration.Password != ""{ diff --git a/modules/swagger-codegen/src/main/resources/go/configuration.mustache b/modules/swagger-codegen/src/main/resources/go/configuration.mustache index abc3b436d04..e52b77621a8 100644 --- a/modules/swagger-codegen/src/main/resources/go/configuration.mustache +++ b/modules/swagger-codegen/src/main/resources/go/configuration.mustache @@ -7,7 +7,8 @@ import ( type Configuration struct { UserName string `json:"userName,omitempty"` Password string `json:"password,omitempty"` - ApiKey string `json:"apiKey,omitempty"` + ApiKeyPrefix map[string] string `json:"apiKeyPrefix,omitempty"` + ApiKey map[string] string `json:"apiKey,omitempty"` Debug bool `json:"debug,omitempty"` DebugFile string `json:"debugFile,omitempty"` OAuthToken string `json:"oAuthToken,omitempty"` @@ -21,11 +22,15 @@ type Configuration struct { func NewConfiguration() *Configuration { defaultHeader := make(map[string]string) + apiKey := make(map[string]string) + apiKeyPrefix := make(map[string]string) return &Configuration{ BasePath: "{{basePath}}", UserName: "", Debug: false, DefaultHeader: defaultHeader, + ApiKey: apiKey, + ApiKeyPrefix: apiKeyPrefix, } } @@ -35,4 +40,14 @@ func (c *Configuration) GetBasicAuthEncodedString() string { func (c *Configuration) AddDefaultHeader(key string, value string) { c.DefaultHeader[key] = value +} + +func (c *Configuration) GetApiKeyWithPrefix(apiKeyIdentifier string) string { + var returnValue = c.ApiKey[apiKeyIdentifier] + var apiKeyPrefix = c.ApiKeyPrefix[apiKeyIdentifier] + if apiKeyPrefix != ""{ + returnValue = apiKeyPrefix + " " + returnValue + } + + return returnValue } \ No newline at end of file diff --git a/samples/client/petstore/go/swagger/Configuration.go b/samples/client/petstore/go/swagger/Configuration.go index f99c572ee1e..369932f5572 100644 --- a/samples/client/petstore/go/swagger/Configuration.go +++ b/samples/client/petstore/go/swagger/Configuration.go @@ -7,7 +7,8 @@ import ( type Configuration struct { UserName string `json:"userName,omitempty"` Password string `json:"password,omitempty"` - ApiKey string `json:"apiKey,omitempty"` + ApiKeyPrefix map[string] string `json:"apiKeyPrefix,omitempty"` + ApiKey map[string] string `json:"apiKey,omitempty"` Debug bool `json:"debug,omitempty"` DebugFile string `json:"debugFile,omitempty"` OAuthToken string `json:"oAuthToken,omitempty"` @@ -21,11 +22,15 @@ type Configuration struct { func NewConfiguration() *Configuration { defaultHeader := make(map[string]string) + apiKey := make(map[string]string) + apiKeyPrefix := make(map[string]string) return &Configuration{ BasePath: "http://petstore.swagger.io/v2", UserName: "", Debug: false, DefaultHeader: defaultHeader, + ApiKey: apiKey, + ApiKeyPrefix: apiKeyPrefix, } } @@ -35,4 +40,14 @@ func (c *Configuration) GetBasicAuthEncodedString() string { func (c *Configuration) AddDefaultHeader(key string, value string) { c.DefaultHeader[key] = value +} + +func (c *Configuration) GetApiKeyWithPrefix(apiKeyIdentifier string) string { + var returnValue = c.ApiKey[apiKeyIdentifier] + var apiKeyPrefix = c.ApiKeyPrefix[apiKeyIdentifier] + if apiKeyPrefix != ""{ + returnValue = apiKeyPrefix + " " + returnValue + } + + return returnValue } \ No newline at end of file diff --git a/samples/client/petstore/go/swagger/PetApi.go b/samples/client/petstore/go/swagger/PetApi.go index 4858ee14347..f17b4900d39 100644 --- a/samples/client/petstore/go/swagger/PetApi.go +++ b/samples/client/petstore/go/swagger/PetApi.go @@ -41,6 +41,7 @@ func (a PetApi) AddPet (body Pet) (error) { _sling := sling.New().Post(a.Configuration.BasePath) // authentication (petstore_auth) required + // oauth required if a.Configuration.AccessToken != ""{ _sling.Set("Authorization", "Bearer " + a.Configuration.AccessToken) @@ -113,6 +114,7 @@ func (a PetApi) DeletePet (petId int64, apiKey string) (error) { _sling := sling.New().Delete(a.Configuration.BasePath) // authentication (petstore_auth) required + // oauth required if a.Configuration.AccessToken != ""{ _sling.Set("Authorization", "Bearer " + a.Configuration.AccessToken) @@ -185,6 +187,7 @@ func (a PetApi) FindPetsByStatus (status []string) ([]Pet, error) { _sling := sling.New().Get(a.Configuration.BasePath) // authentication (petstore_auth) required + // oauth required if a.Configuration.AccessToken != ""{ _sling.Set("Authorization", "Bearer " + a.Configuration.AccessToken) @@ -258,6 +261,7 @@ func (a PetApi) FindPetsByTags (tags []string) ([]Pet, error) { _sling := sling.New().Get(a.Configuration.BasePath) // authentication (petstore_auth) required + // oauth required if a.Configuration.AccessToken != ""{ _sling.Set("Authorization", "Bearer " + a.Configuration.AccessToken) @@ -331,6 +335,10 @@ func (a PetApi) GetPetById (petId int64) (Pet, error) { _sling := sling.New().Get(a.Configuration.BasePath) // authentication (api_key) required + + // set key with prefix in header + _sling.Set("api_key", a.Configuration.GetApiKeyWithPrefix("api_key") + // create path and map variables path := "/v2/pet/{petId}" @@ -397,6 +405,7 @@ func (a PetApi) UpdatePet (body Pet) (error) { _sling := sling.New().Put(a.Configuration.BasePath) // authentication (petstore_auth) required + // oauth required if a.Configuration.AccessToken != ""{ _sling.Set("Authorization", "Bearer " + a.Configuration.AccessToken) @@ -470,6 +479,7 @@ func (a PetApi) UpdatePetWithForm (petId int64, name string, status string) (err _sling := sling.New().Post(a.Configuration.BasePath) // authentication (petstore_auth) required + // oauth required if a.Configuration.AccessToken != ""{ _sling.Set("Authorization", "Bearer " + a.Configuration.AccessToken) @@ -547,6 +557,7 @@ func (a PetApi) UploadFile (petId int64, additionalMetadata string, file *os.Fil _sling := sling.New().Post(a.Configuration.BasePath) // authentication (petstore_auth) required + // oauth required if a.Configuration.AccessToken != ""{ _sling.Set("Authorization", "Bearer " + a.Configuration.AccessToken) diff --git a/samples/client/petstore/go/swagger/StoreApi.go b/samples/client/petstore/go/swagger/StoreApi.go index ed255e00834..6cc425f5cb9 100644 --- a/samples/client/petstore/go/swagger/StoreApi.go +++ b/samples/client/petstore/go/swagger/StoreApi.go @@ -104,6 +104,10 @@ func (a StoreApi) GetInventory () (map[string]int32, error) { _sling := sling.New().Get(a.Configuration.BasePath) // authentication (api_key) required + + // set key with prefix in header + _sling.Set("api_key", a.Configuration.GetApiKeyWithPrefix("api_key") + // create path and map variables path := "/v2/store/inventory"