Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hard Coding Api Key value in header when using a Swagger Generated Client

I have written many APIs in C# and created a "Swagger" documentation website using Swashbuckle.

For Authenticate REST calls I use an API Key in the header.

I created a page that permits to download a specific client for any programming language as documented here: https://generator.swagger.io

I would like to enable the user to generate a client with his own API Key so he does not need to manually set the API Key in the code anymore.

In my Swagger JSON I have this security definition:

"securityDefinitions": {
    "apiKey": {
        "type": "apiKey",
        "description": "API Key Authentication",
        "name": "X-ApiKey",
        "in": "header"
    }
}

In Swagger Client Generator's page I have found this model that permit to set up the clients options but I cannot find how (and if) the API key can be hard coded (or any other kind of authorization) in the client code.

GeneratorInput {
    spec (object, optional),
    options (object, optional),
    swaggerUrl (string, optional),
    authorizationValue (AuthorizationValue, optional),
    securityDefinition (SecuritySchemeDefinition, optional)
}
AuthorizationValue {
    value (string, optional),
    type (string, optional),
    keyName (string, optional)
}
SecuritySchemeDefinition {
    description (string, optional),
    type (string, optional)
}

I suppose I must set AuthorizationValue object but there is no documentation about that (or I can not find it).

It would be sufficient to be able to be able to have the generated client lib add an arbitrary HTTP header to all requests.

In which case we could just have it add:

X-ApiKey:{whatever the key is}

Anyone has an idea ?

Many Thanks!

like image 499
Luca Natali Avatar asked May 25 '16 12:05

Luca Natali


People also ask

How do I pass a header key in API postman?

API key. With API key auth, you send a key-value pair to the API either in the request headers or query parameters. In the request Authorization tab, select API Key from the Type list. Enter your key name and value, and select either Header or Query Params from the Add to dropdown list.


1 Answers

It seems to work to simply add it as a parameter on each call with a default value - so the JSON would have something like this:

"post": {
                "tags": [ "user" ],
                "summary": "Creates list of users with given input array",
                "description": "",
                "operationId": "createUsersWithListInput",
                "produces": [ "application/xml", "application/json" ],
                "parameters": [
                    {
                        "in": "body",
                        "name": "body",
                        "description": "List of user object",
                        "required": true,
                        "schema": {
                            "type": "array",
                            "items": { "$ref": "#/definitions/User" }
                        }
                    },
                    {
                        "in": "header",
                        "name": "X-ApiKey",
                        "required": false,
                        "type": "string",
                        "format": "string",
                        "default": "abcdef12345"
                    }
                ],
                "responses": { "default": { "description": "successful operation" } }
            }
like image 84
Mel Clemens Avatar answered Sep 20 '22 08:09

Mel Clemens