Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] bearerAuth security scheme not resulting in access_token being used to create Authorization header. #18041

Open
5 of 6 tasks
JonathanNathanson opened this issue Mar 6, 2024 · 5 comments

Comments

@JonathanNathanson
Copy link

JonathanNathanson commented Mar 6, 2024

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Having generated a Python client from an OpenAPI spec including the following securityscheme:

  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT

It is not possible to use the access_token configuration parameter in the resulting client. Passing it to openapi_client.Configuration on instantiation, or adding it to the instantiated object, does not result in the Authorization header being passed in requests.

openapi-generator version

7.3.0

OpenAPI declaration file content or url
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT
Generation Details
docker run --rm \
  -v ${PWD}:/local openapitools/openapi-generator-cli generate \
  -i /local/myspecification.yaml \
  -g python \
  -o /local/client
Steps to reproduce
"""Testing the OpenAPI client."""

import openapi_client
from openapi_client.rest import ApiException
from pprint import pprint


configuration = openapi_client.Configuration(
    host="https://myapi.com/v2.5.6"
    access_token="my_token"
)


configuration.debug = True

configuration.access_token = "my_token"


with openapi_client.ApiClient(
    configuration,
) as api_client:
    api_instance = openapi_client.DefaultApi(api_client)

    try:
        api_response = api_instance.get_building_by_id(20)
        pprint(api_response)
    except ApiException as e:
        print("Exception when calling DefaultApi->get_building_by_id: %s\n" % e)

Debug output:

send: b'GET /v2.5.6/buildings/20 HTTP/1.1\r\nHost: myapi.com\r\nAccept-Encoding: identity\r\nAccept: application/json\r\nUser-Agent: OpenAPI-Generator/1.0.0/python\r\n\r\n'
Related issues/PRs

Seems related:

#8865

But stated fix of setting saccess_token after instantiation of the configuration class doesn't appear to work. And the issue says it was fixed in v6. Indeed, configuration.py in the generated code does seem to include the requisite fixes:

self.access_token = access_token
        """Access token
        """

...

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

        :return: The Auth Settings information dict.
        """
        auth = {}
        if self.access_token is not None:
            auth['bearerAuth'] = {
                'type': 'bearer',
                'in': 'header',
                'format': 'JWT',
                'key': 'Authorization',
                'value': 'Bearer ' + self.access_token
            }
        return auth
Suggest a fix

I can't seem to trace down the code which is ignoring the auth_settings. There appears to be no other code that references auth['bearerAuth'].

@fa0311
Copy link
Contributor

fa0311 commented Mar 11, 2024

Are these included in the schema file?

security:
- bearerAuth: []

I can't seem to trace down the code which is ignoring the auth_settings. There appears to be no other code that references auth['bearerAuth']

Probably does not generate auth['bearerAuth'] even when configured correctly.
Instead, these are generated:

# ./api/default_api.py
def _get_building_by_id_serialize(
        self,
        _request_auth,
        _content_type,
        _headers,
        _host_index,
) -> RequestSerialized:
        ...
        _auth_settings: List[str] = [
            'bearerAuth'
        ]

@JonathanNathanson JonathanNathanson changed the title [BUG] Description [BUG] bearerAuth security scheme not resulting in access_token being used to create Authorization header. Mar 19, 2024
@JonathanNathanson
Copy link
Author

Yes, the scehma includes.

security:
- bearerAuth: []

So is the behaviour I'm witnessing expected? How should I use an access_token? Currently the workaround is to set the header manually.

@lostiniceland
Copy link
Contributor

lostiniceland commented May 14, 2024

We are seing this issue as well.
The client generator for Java - Spring - Webclient is not adding any "authorizations" event though they are defined. Due to this we cannot call apiClient.setBearerTocken(...)

As workaround we have to use apiClient.addDefaultHeader("Authorization", ....)

@mike-gangl
Copy link

I'm also having the exact same issue. Using openapi-generator version 7.7.0.

@mike-gangl
Copy link

For those finding this issue- a work around in the previously mentioned bug (fixed) still works. Namely setting a default header on the api_client.

#8865 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants