Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,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(Context{{name}}).(APIKey); ok {
var key string
if auth.Prefix != "" {
key = auth.Prefix + " " + auth.Key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ var (
ContextAccessToken = contextKey("accesstoken")

// ContextAPIKey takes an APIKey as authentication for the request
ContextAPIKey = contextKey("apikey")
{{#authMethods}}
{{#isApiKey}}
Context{{name}} = contextKey("{{name}}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this line is not indented correctly (1 additional tab)

Copy link
Member

@wing328 wing328 Jun 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And what about making ContextAPIKey a map instead as that's what we do in other languages?

{{/isApiKey}}
{{/authMethods}}
)

// BasicAuth provides basic http authentication to a request passed via context using ContextBasicAuth
Expand Down
4 changes: 2 additions & 2 deletions samples/client/petstore/go/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func TestAccessToken(t *testing.T) {
}

func TestAPIKeyNoPrefix(t *testing.T) {
auth := context.WithValue(context.Background(), sw.ContextAPIKey, sw.APIKey{Key: "TEST123"})
auth := context.WithValue(context.Background(), sw.Contextapi_key, sw.APIKey{Key: "TEST123"})

newPet := (sw.Pet{Id: 12992, Name: "gopher",
PhotoUrls: []string{"http://1.com", "http://2.com"}, Status: "pending", Tags: []sw.Tag{sw.Tag{Id: 1, Name: "tag2"}}})
Expand Down Expand Up @@ -170,7 +170,7 @@ func TestAPIKeyNoPrefix(t *testing.T) {
}

func TestAPIKeyWithPrefix(t *testing.T) {
auth := context.WithValue(context.Background(), sw.ContextAPIKey, sw.APIKey{Key: "TEST123", Prefix: "Bearer"})
auth := context.WithValue(context.Background(), sw.Contextapi_key, sw.APIKey{Key: "TEST123", Prefix: "Bearer"})

newPet := (sw.Pet{Id: 12992, Name: "gopher",
PhotoUrls: []string{"http://1.com", "http://2.com"}, Status: "pending", Tags: []sw.Tag{sw.Tag{Id: 1, Name: "tag2"}}})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (a *FakeClassnameTags123ApiService) TestClassname(ctx context.Context, body
localVarPostBody = &body
if ctx != nil {
// API Key Authentication
if auth, ok := ctx.Value(ContextAPIKey).(APIKey); ok {
if auth, ok := ctx.Value(Contextapi_key_query).(APIKey); ok {
var key string
if auth.Prefix != "" {
key = auth.Prefix + " " + auth.Key
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/go/go-petstore/api_pet.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ func (a *PetApiService) GetPetById(ctx context.Context, petId int64) (Pet, *http
}
if ctx != nil {
// API Key Authentication
if auth, ok := ctx.Value(ContextAPIKey).(APIKey); ok {
if auth, ok := ctx.Value(Contextapi_key).(APIKey); ok {
var key string
if auth.Prefix != "" {
key = auth.Prefix + " " + auth.Key
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/go/go-petstore/api_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (a *StoreApiService) GetInventory(ctx context.Context) (map[string]int32, *
}
if ctx != nil {
// API Key Authentication
if auth, ok := ctx.Value(ContextAPIKey).(APIKey); ok {
if auth, ok := ctx.Value(Contextapi_key).(APIKey); ok {
var key string
if auth.Prefix != "" {
key = auth.Prefix + " " + auth.Key
Expand Down
3 changes: 2 additions & 1 deletion samples/client/petstore/go/go-petstore/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ var (
ContextAccessToken = contextKey("accesstoken")

// ContextAPIKey takes an APIKey as authentication for the request
ContextAPIKey = contextKey("apikey")
Contextapi_key = contextKey("api_key")
Contextapi_key_query = contextKey("api_key_query")
)

// BasicAuth provides basic http authentication to a request passed via context using ContextBasicAuth
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.0.0-SNAPSHOT
4.0.3-SNAPSHOT
50 changes: 39 additions & 11 deletions samples/openapi3/client/petstore/go/go-petstore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ This API client was generated by the [OpenAPI Generator](https://openapi-generat
## Installation

Install the following dependencies:
```

```shell
go get github.com/stretchr/testify/assert
go get golang.org/x/oauth2
go get golang.org/x/net/context
go get github.com/antihax/optional
```

Put the package under your project folder and add the following in import:

```golang
import "./petstore"
```
Expand Down Expand Up @@ -79,10 +81,12 @@ Class | Method | HTTP request | Description
- [ArrayTest](docs/ArrayTest.md)
- [Capitalization](docs/Capitalization.md)
- [Cat](docs/Cat.md)
- [CatAllOf](docs/CatAllOf.md)
- [Category](docs/Category.md)
- [ClassModel](docs/ClassModel.md)
- [Client](docs/Client.md)
- [Dog](docs/Dog.md)
- [DogAllOf](docs/DogAllOf.md)
- [EnumArrays](docs/EnumArrays.md)
- [EnumClass](docs/EnumClass.md)
- [EnumTest](docs/EnumTest.md)
Expand All @@ -104,6 +108,7 @@ Class | Method | HTTP request | Description
- [MixedPropertiesAndAdditionalPropertiesClass](docs/MixedPropertiesAndAdditionalPropertiesClass.md)
- [Model200Response](docs/Model200Response.md)
- [Name](docs/Name.md)
- [NullableClass](docs/NullableClass.md)
- [NumberOnly](docs/NumberOnly.md)
- [Order](docs/Order.md)
- [OuterComposite](docs/OuterComposite.md)
Expand All @@ -121,51 +126,71 @@ Class | Method | HTTP request | Description

## Documentation For Authorization



## api_key
- **Type**: API key

- **Type**: API key

Example

```golang
auth := context.WithValue(context.Background(), sw.ContextAPIKey, sw.APIKey{
Key: "APIKEY",
Prefix: "Bearer", // Omit if not necessary.
Key: "APIKEY",
Prefix: "Bearer", // Omit if not necessary.
})
r, err := client.Service.Operation(auth, args)
```


## api_key_query
- **Type**: API key

- **Type**: API key

Example

```golang
auth := context.WithValue(context.Background(), sw.ContextAPIKey, sw.APIKey{
Key: "APIKEY",
Prefix: "Bearer", // Omit if not necessary.
Key: "APIKEY",
Prefix: "Bearer", // Omit if not necessary.
})
r, err := client.Service.Operation(auth, args)
```


## bearer_test

- **Type**: HTTP basic authentication

Example

```golang
auth := context.WithValue(context.Background(), sw.ContextBasicAuth, sw.BasicAuth{
UserName: "username",
Password: "password",
UserName: "username",
Password: "password",
})
r, err := client.Service.Operation(auth, args)
```


## http_basic_test

- **Type**: HTTP basic authentication

Example

```golang
auth := context.WithValue(context.Background(), sw.ContextBasicAuth, sw.BasicAuth{
UserName: "username",
Password: "password",
UserName: "username",
Password: "password",
})
r, err := client.Service.Operation(auth, args)
```


## petstore_auth


- **Type**: OAuth
- **Flow**: implicit
- **Authorization URL**: http://petstore.swagger.io/api/oauth/dialog
Expand All @@ -174,12 +199,14 @@ r, err := client.Service.Operation(auth, args)
- **read:pets**: read your pets

Example

```golang
auth := context.WithValue(context.Background(), sw.ContextAccessToken, "ACCESSTOKENSTRING")
r, err := client.Service.Operation(auth, args)
```

Or via OAuth2 module to automatically refresh tokens and perform user authentication.

```golang
import "golang.org/x/oauth2"

Expand All @@ -190,6 +217,7 @@ auth := context.WithValue(oauth2.NoContext, sw.ContextOAuth2, tokenSource)
r, err := client.Service.Operation(auth, args)
```


## Author


Expand Down
Loading