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

Bearer auth seems to not work with python codegen #8865

Closed
4c74356b41 opened this issue Mar 2, 2021 · 7 comments
Closed

Bearer auth seems to not work with python codegen #8865

4c74356b41 opened this issue Mar 2, 2021 · 7 comments

Comments

@4c74356b41
Copy link

4c74356b41 commented Mar 2, 2021

Bug Report Checklist

my spec has this:

components:
  securitySchemes:
    xxx:
      type: http
      scheme: bearer

  responses:
    GenericApiError:
      description: this object is returned in the cases when an API throws an exception
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/JsonSerializedException'

security:
 - xxx: []

after generating the code, i see this in the code:

configuration.py:
    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 {
        }
Description

code doesn't use bearer token in the requests

openapi-generator version
java -jar openapi-generator-cli.jar  version           
5.0.1
OpenAPI declaration file content or url

https://github.com/Telecominfraproject/wlan-cloud-services/blob/master/portal-services/src/main/resources/portal-services-openapi.yaml

Generation Details
java -jar openapi-generator-cli.jar generate -i portal-services-openapi.yaml -g python -o portal_client
Related issues/PRs

#1577

@auto-labeler
Copy link

auto-labeler bot commented Mar 2, 2021

👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

@AlfredoCubitos
Copy link

AlfredoCubitos commented Mar 30, 2021

Hi, I can confirm that this issue exists
When I set the configuration values

configuration.api_key['Token'] = f"{token}"
configuration.api_key_prefix['Authorization'] = 'Bearer '

this values are not available in any request
Also trying to set the values with
api_instance.api_client.update_params_for_auth({ "Authorization": f"Bearer {token} "},None,None)
nothing happened.
It seems that this headers are overwritten somewhere

finally I solved the problem by adding this lines to the def __call_api function in api_client.py

if config.api_key:
            header_params["Authorization"] = f"{config.api_key_prefix['Authorization']} {config.api_key['Token']}"

@4c74356b41
Copy link
Author

we decided against modifying the generated code itself, we are just passing extra headers to the calls, that way we can still use the generated code. @AlfredoCubitos

self.api_client = swagger_client.ApiClient(self.configuration)
self.api_client.default_headers['Authorization'] = "Bearer " + self.bearer._access_token

@AlfredoCubitos
Copy link

@4c74356b41 Yes, this is a better solution 👍

@spacether
Copy link
Contributor

spacether commented Oct 10, 2022

apiKey auth is working in the v6.2.0 python client and onward. One can see it working in this test:
https://github.com/OpenAPITools/openapi-generator/blob/master/samples/openapi3/client/petstore/python/tests_manual/test_paths/test_pet_pet_id/test_get.py#L55
which is generated from this spec

@spacether
Copy link
Contributor

Re-opening this because api key auth is different than bearer auth

@spacether spacether reopened this Nov 5, 2022
@spacether
Copy link
Contributor

spacether commented Nov 6, 2022

This has been working from v5.0.0 (Dec 20, 2020) and onward in the 'python' client
One can see the bearer access_token property here:
https://github.com/OpenAPITools/openapi-generator/blob/v5.0.0/samples/openapi3/client/petstore/python/petstore_api/configuration.py#L213
Which must be set after Configuration is instantiated.
After that, it will be used in spec defined oath/bearer endpoint calls.
For the v6.2.0 and onward python client, which will long term be moving to a new repo I have fixed the Configuration class where access_token can be passed into Configuration init in the new repo. Fix and auth verification in the new repo is here

Even earlier in v4.0.3 (June 28, 2019) I see this working:
https://github.com/OpenAPITools/openapi-generator/blob/v4.0.3/samples/openapi3/client/petstore/python/petstore_api/configuration.py#L76

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

3 participants