swagger-api / swagger-api/swagger-codegen

[Go] Authentication with multiple API keys doesn't work in generated Go client

Open
#8,824 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

When using a spec file with multiple API keys defined, authentication can't be done in the generated Go code. The README.md file will state that you should set the same variable twice and that is exactly how the go code is set up as well. The authentication code found within every function in files like server_api.go and it looks like this:

	// body params
	localVarPostBody = &payload
	if ctx != nil {
		// API Key Authentication
		if auth, ok := ctx.Value(ContextAPIKey).(APIKey); ok {
			var key string
			if auth.Prefix != "" {
				key = auth.Prefix + " " + auth.Key
			} else {
				key = auth.Key
			}
			localVarHeaderParams["X-Auth-Token"] = key
		}
	}
	if ctx != nil {
		// API Key Authentication
		if auth, ok := ctx.Value(ContextAPIKey).(APIKey); ok {
			var key string
			if auth.Prefix != "" {
				key = auth.Prefix + " " + auth.Key
			} else {
				key = auth.Key
			}
			localVarHeaderParams["X-Auth-UserId"] = key
		}
	}

As expected, this doesn't work. This is what the readme looks like:

ApiKeyAuth

  • Type: API key

Example

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

ApiUserID

  • Type: API key

Example

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

2.3.1.

I haven't tried other versions.

Swagger declaration file content or url

This is the relevant part:

swagger: '2.0'
schemes:
- https
host: api.host
info:
  description: example API
  version: 1.0.0
  title: API specification
securityDefinitions:
  ApiKeyAuth:
    type: apiKey
    in: header
    name: X-Auth-Token
  ApiUserID:
    type: apiKey
    in: header
    name: X-Auth-UserId
security:
  - ApiKeyAuth: []
    ApiUserID: []
Command line used for generation

swagger-codegen generate -l go -i spec.yaml -o swagger

Steps to reproduce

Add more than one API key to a a swagger file and generate a go client.

Related issues/PRs

None that I know of.

Suggest a fix/enhancement

I think changes are required to the mustache template for go to solve this, but I don't have the skill yet to do this.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the generated server_api.go authentication blocks and the README.md examples, using the supplied spec.yaml and Go generation command to reproduce the issue. Trace these outputs back to the Go mustache template mentioned in the report. Done means multiple API keys can be supplied independently and the generated README documents the working usage.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.