Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use bearer authentication in openapi-codegen generated python code

I am using the OpenApi 3.0 specification to document my API. It is basically a REST API which requires a valid Bearer token for each request except for the login request. The OpenAPI spec looks like this:

---
openapi: 3.0.0
info:
  title: MyTitle
  contact:
    email: [email protected]
  version: 0.0.1
servers:
- url: http://localhost/api/v1
components:
  schemas:
    ...
  securitySchemes:
    bearerAuth:
      type: http
      bearerFormat: JWT
      scheme: bearer
security:
- bearerAuth: []

In the automatically generated swagger-ui that works perfectly fine, the same is true for postman when I download the OpenAPI spec and import it into Postman.

However, when using the openapi-generator using the following command:

docker run --user `id -u`:`id -g` --rm -v ${PWD}:/local openapitools/openapi-generator-cli generate -i /local/api.json -g python -o /local/api

However, instead of the bearer token, it tries to use username and password instead:

def get_basic_auth_token(self):
    """Gets HTTP basic authentication header (string).

    :return: The token for basic HTTP authentication.
    """
    return urllib3.util.make_headers(
        basic_auth=self.username + ':' + self.password
    ).get('authorization')

def auth_settings(self):
    """Gets Auth Settings dict for api client.

    :return: The Auth Settings information dict.
    """
    return {
        'bearerAuth':
            {
                'type': 'basic',
                'in': 'header',
                'key': 'Authorization',
                'value': self.get_basic_auth_token()
            },

    }

Note that here a username and password is used, and the auth_settings() function returns a basic auth. What do I have to do to use the Bearer authentication scheme in the generated code instead?

like image 202
waza-ari Avatar asked Jan 16 '19 21:01

waza-ari


1 Answers

This was a bug. It was fixed in openapi-generator v. 4.0.0-beta2 and later.

like image 188
Helen Avatar answered Nov 15 '22 03:11

Helen