From fc80bce82f377bdbc49a2c77ee121d81748a7edc Mon Sep 17 00:00:00 2001 From: Guo Huang Date: Mon, 11 Apr 2016 15:49:13 -0700 Subject: [PATCH 1/4] issue #2535, added authentication header for GO --- .../src/main/resources/go/api.mustache | 15 +++++++ .../main/resources/go/configuration.mustache | 8 +++- .../petstore/go/swagger/Configuration.go | 8 +++- samples/client/petstore/go/swagger/PetApi.go | 44 +++++++++++++++++++ .../client/petstore/go/swagger/StoreApi.go | 5 +++ samples/client/petstore/go/swagger/UserApi.go | 8 ++++ 6 files changed, 86 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 d1cf877cd439..50fd16bd2441 100644 --- a/modules/swagger-codegen/src/main/resources/go/api.mustache +++ b/modules/swagger-codegen/src/main/resources/go/api.mustache @@ -43,6 +43,21 @@ func (a {{classname}}) {{nickname}} ({{#allParams}}{{paramName}} {{{dataType}}}{ _sling := sling.New().{{httpMethod}}(a.Configuration.BasePath) + {{#authMethods}}// authentication ({{name}}) required + {{#isBasic}} + // http basic authentication required + if a.Configuration.Username != "" || a.Configuration.Password != ""{ + _sling.Set("Authorization", "Basic " + a.Configuration.GetBasicAuthEncodedString()) + } + {{/isBasic}} + {{#isOAuth}} + // oauth required + if a.Configuration.AccessToken != ""{ + _sling.Set("Authorization", "Bearer " + a.Configuration.AccessToken) + } + {{/isOAuth}} + {{/authMethods}} + // create path and map variables path := "{{basePathWithoutHost}}{{path}}" {{#pathParams}} path = strings.Replace(path, "{" + "{{baseName}}" + "}", fmt.Sprintf("%v", {{paramName}}), -1) diff --git a/modules/swagger-codegen/src/main/resources/go/configuration.mustache b/modules/swagger-codegen/src/main/resources/go/configuration.mustache index bf3a8f92bed0..d78ba9b5e01a 100644 --- a/modules/swagger-codegen/src/main/resources/go/configuration.mustache +++ b/modules/swagger-codegen/src/main/resources/go/configuration.mustache @@ -1,11 +1,12 @@ package {{packageName}} import ( - + "encoding/base64" ) type Configuration struct { UserName string `json:"userName,omitempty"` + Password string `json:"password,omitempty"` ApiKey string `json:"apiKey,omitempty"` Debug bool `json:"debug,omitempty"` DebugFile string `json:"debugFile,omitempty"` @@ -14,6 +15,7 @@ type Configuration struct { BasePath string `json:"basePath,omitempty"` Host string `json:"host,omitempty"` Scheme string `json:"scheme,omitempty"` + AccessToken string `json:"accessToken,omitempty"` } func NewConfiguration() *Configuration { @@ -22,4 +24,8 @@ func NewConfiguration() *Configuration { UserName: "", Debug: false, } +} + +func (c *Configuration) GetBasicAuthEncodedString() string { + return base64.StdEncoding.EncodeToString([]byte(c.UserName + ":" + c.Password)) } \ 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 7535db5e67f6..1763f4dbd384 100644 --- a/samples/client/petstore/go/swagger/Configuration.go +++ b/samples/client/petstore/go/swagger/Configuration.go @@ -1,11 +1,12 @@ package swagger import ( - + "encoding/base64" ) type Configuration struct { UserName string `json:"userName,omitempty"` + Password string `json:"password,omitempty"` ApiKey string `json:"apiKey,omitempty"` Debug bool `json:"debug,omitempty"` DebugFile string `json:"debugFile,omitempty"` @@ -14,6 +15,7 @@ type Configuration struct { BasePath string `json:"basePath,omitempty"` Host string `json:"host,omitempty"` Scheme string `json:"scheme,omitempty"` + AccessToken string `json:"accessToken,omitempty"` } func NewConfiguration() *Configuration { @@ -22,4 +24,8 @@ func NewConfiguration() *Configuration { UserName: "", Debug: false, } +} + +func (c *Configuration) GetBasicAuthEncodedString() string { + return base64.StdEncoding.EncodeToString([]byte(c.UserName + ":" + c.Password)) } \ 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 41353aac7416..986c01d7d236 100644 --- a/samples/client/petstore/go/swagger/PetApi.go +++ b/samples/client/petstore/go/swagger/PetApi.go @@ -40,6 +40,12 @@ 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) + } + // create path and map variables path := "/v2/pet" @@ -101,6 +107,12 @@ 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) + } + // create path and map variables path := "/v2/pet/{petId}" path = strings.Replace(path, "{" + "petId" + "}", fmt.Sprintf("%v", petId), -1) @@ -162,6 +174,12 @@ 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) + } + // create path and map variables path := "/v2/pet/findByStatus" @@ -224,6 +242,12 @@ 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) + } + // create path and map variables path := "/v2/pet/findByTags" @@ -286,6 +310,8 @@ func (a PetApi) GetPetById (petId int64) (Pet, error) { _sling := sling.New().Get(a.Configuration.BasePath) + // authentication (api_key) required + // create path and map variables path := "/v2/pet/{petId}" path = strings.Replace(path, "{" + "petId" + "}", fmt.Sprintf("%v", petId), -1) @@ -345,6 +371,12 @@ 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) + } + // create path and map variables path := "/v2/pet" @@ -407,6 +439,12 @@ 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) + } + // create path and map variables path := "/v2/pet/{petId}" path = strings.Replace(path, "{" + "petId" + "}", fmt.Sprintf("%v", petId), -1) @@ -473,6 +511,12 @@ 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) + } + // create path and map variables path := "/v2/pet/{petId}/uploadImage" path = strings.Replace(path, "{" + "petId" + "}", fmt.Sprintf("%v", petId), -1) diff --git a/samples/client/petstore/go/swagger/StoreApi.go b/samples/client/petstore/go/swagger/StoreApi.go index 8d3e41e71e16..6da5a5c35e28 100644 --- a/samples/client/petstore/go/swagger/StoreApi.go +++ b/samples/client/petstore/go/swagger/StoreApi.go @@ -39,6 +39,7 @@ func (a StoreApi) DeleteOrder (orderId string) (error) { _sling := sling.New().Delete(a.Configuration.BasePath) + // create path and map variables path := "/v2/store/order/{orderId}" path = strings.Replace(path, "{" + "orderId" + "}", fmt.Sprintf("%v", orderId), -1) @@ -97,6 +98,8 @@ func (a StoreApi) GetInventory () (map[string]int32, error) { _sling := sling.New().Get(a.Configuration.BasePath) + // authentication (api_key) required + // create path and map variables path := "/v2/store/inventory" @@ -155,6 +158,7 @@ func (a StoreApi) GetOrderById (orderId int64) (Order, error) { _sling := sling.New().Get(a.Configuration.BasePath) + // create path and map variables path := "/v2/store/order/{orderId}" path = strings.Replace(path, "{" + "orderId" + "}", fmt.Sprintf("%v", orderId), -1) @@ -214,6 +218,7 @@ func (a StoreApi) PlaceOrder (body Order) (Order, error) { _sling := sling.New().Post(a.Configuration.BasePath) + // create path and map variables path := "/v2/store/order" diff --git a/samples/client/petstore/go/swagger/UserApi.go b/samples/client/petstore/go/swagger/UserApi.go index f8c8581aa3b0..2148336c2285 100644 --- a/samples/client/petstore/go/swagger/UserApi.go +++ b/samples/client/petstore/go/swagger/UserApi.go @@ -39,6 +39,7 @@ func (a UserApi) CreateUser (body User) (error) { _sling := sling.New().Post(a.Configuration.BasePath) + // create path and map variables path := "/v2/user" @@ -99,6 +100,7 @@ func (a UserApi) CreateUsersWithArrayInput (body []User) (error) { _sling := sling.New().Post(a.Configuration.BasePath) + // create path and map variables path := "/v2/user/createWithArray" @@ -159,6 +161,7 @@ func (a UserApi) CreateUsersWithListInput (body []User) (error) { _sling := sling.New().Post(a.Configuration.BasePath) + // create path and map variables path := "/v2/user/createWithList" @@ -219,6 +222,7 @@ func (a UserApi) DeleteUser (username string) (error) { _sling := sling.New().Delete(a.Configuration.BasePath) + // create path and map variables path := "/v2/user/{username}" path = strings.Replace(path, "{" + "username" + "}", fmt.Sprintf("%v", username), -1) @@ -278,6 +282,7 @@ func (a UserApi) GetUserByName (username string) (User, error) { _sling := sling.New().Get(a.Configuration.BasePath) + // create path and map variables path := "/v2/user/{username}" path = strings.Replace(path, "{" + "username" + "}", fmt.Sprintf("%v", username), -1) @@ -338,6 +343,7 @@ func (a UserApi) LoginUser (username string, password string) (string, error) { _sling := sling.New().Get(a.Configuration.BasePath) + // create path and map variables path := "/v2/user/login" @@ -400,6 +406,7 @@ func (a UserApi) LogoutUser () (error) { _sling := sling.New().Get(a.Configuration.BasePath) + // create path and map variables path := "/v2/user/logout" @@ -459,6 +466,7 @@ func (a UserApi) UpdateUser (username string, body User) (error) { _sling := sling.New().Put(a.Configuration.BasePath) + // create path and map variables path := "/v2/user/{username}" path = strings.Replace(path, "{" + "username" + "}", fmt.Sprintf("%v", username), -1) From 4e6ff1a5674c3a1da933f97965746c3da5ee52a5 Mon Sep 17 00:00:00 2001 From: Guo Huang Date: Mon, 11 Apr 2016 22:25:02 -0700 Subject: [PATCH 2/4] 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 a7c09eae4cdc..b1bd9bb0fc63 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 abc3b436d044..e52b77621a85 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 f99c572ee1e2..369932f5572b 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 4858ee14347f..f17b4900d395 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 ed255e008347..6cc425f5cb94 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" From 6154619924b116e9a86c9d5bbdae5334cb7e36c7 Mon Sep 17 00:00:00 2001 From: Guo Huang Date: Mon, 11 Apr 2016 23:28:06 -0700 Subject: [PATCH 3/4] updated code to use query struct --- modules/swagger-codegen/src/main/resources/go/api.mustache | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/modules/swagger-codegen/src/main/resources/go/api.mustache b/modules/swagger-codegen/src/main/resources/go/api.mustache index b1bd9bb0fc63..cb3552acbfe1 100644 --- a/modules/swagger-codegen/src/main/resources/go/api.mustache +++ b/modules/swagger-codegen/src/main/resources/go/api.mustache @@ -49,7 +49,11 @@ func (a {{classname}}) {{nickname}} ({{#allParams}}{{paramName}} {{{dataType}}}{ _sling.Set("{{keyParamName}}", a.Configuration.GetApiKeyWithPrefix("{{keyParamName}}") {{/isKeyInHeader}}{{#isKeyInQuery}} // set key with prefix in querystring - _sling.Set("{{keyParamName}}", a.Configuration.GetApiKeyWithPrefix("{{keyParamName}}") + {{#hasKeyParamName}} type KeyQueryParams struct { + {{keyParamName}} string `url:"{{keyParamName}},omitempty"` + } + _sling = _sling.QueryStruct(&KeyQueryParams{ {{keyParamName}}: a.Configuration.GetApiKeyWithPrefix("{{keyParamName}}") }) + {{/hasKeyParamName}} {{/isKeyInQuery}}{{/isApiKey}} {{#isBasic}} // http basic authentication required From 930756a76a563adb0d776bdc8a2c1a2a092fbc24 Mon Sep 17 00:00:00 2001 From: Guo Huang Date: Tue, 12 Apr 2016 09:21:08 -0700 Subject: [PATCH 4/4] added missing ")" for api key --- modules/swagger-codegen/src/main/resources/go/api.mustache | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/swagger-codegen/src/main/resources/go/api.mustache b/modules/swagger-codegen/src/main/resources/go/api.mustache index cb3552acbfe1..7ed2bebbc87a 100644 --- a/modules/swagger-codegen/src/main/resources/go/api.mustache +++ b/modules/swagger-codegen/src/main/resources/go/api.mustache @@ -46,7 +46,7 @@ func (a {{classname}}) {{nickname}} ({{#allParams}}{{paramName}} {{{dataType}}}{ {{#authMethods}}// authentication ({{name}}) required {{#isApiKey}}{{#isKeyInHeader}} // set key with prefix in header - _sling.Set("{{keyParamName}}", a.Configuration.GetApiKeyWithPrefix("{{keyParamName}}") + _sling.Set("{{keyParamName}}", a.Configuration.GetApiKeyWithPrefix("{{keyParamName}}")) {{/isKeyInHeader}}{{#isKeyInQuery}} // set key with prefix in querystring {{#hasKeyParamName}} type KeyQueryParams struct {