简体   繁体   中英

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: mymail@mail.com
  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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM