diff --git a/modules/swagger-codegen/src/main/resources/go/api.mustache b/modules/swagger-codegen/src/main/resources/go/api.mustache index d1cf877cd43..50fd16bd244 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 bf3a8f92bed..d78ba9b5e01 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 7535db5e67f..1763f4dbd38 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 41353aac741..986c01d7d23 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 8d3e41e71e1..6da5a5c35e2 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 f8c8581aa3b..2148336c228 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)