swagger-api / swagger-api/swagger-codegen

oauth issue: access_token is never set

Open
#10,968 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

I've had trouble getting Oauth working. The generated code wipes the token within the Authorization header and leaves just bearer as the value.

To expand further, within the generated configuration.py file, the auth_settings method is responsible for returning the respective auth method we use for a endpoint. You can see that for oauth, the value of value is set to "Bearer" + access_token. What happens though when access_token does not exist? It sets it to just Bearer. When I looked further into how access_token gets set, I found out that it doesn't even get set. It is set to "" by default and there is no code that ever attempts to update that. I also looked at the source code and it looks like "" is hard-coded for access_token.

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

        :return: The Auth Settings information dict.
        """
        return {
            'oauth':
                {
                    'type': 'oauth2',
                    'in': 'header',
                    'key': 'Authorization',
                    'value': 'Bearer' + access_token
                },
            'token':
                {
                    'type': 'api_key',
                    'in': 'header',
                    'key': 'Authorization',
                    'value': self.get_api_key_with_prefix('Authorization')
                },

        }
def __init__(self):
        ...
        # access token for OAuth
        self.access_token = ""
        ...
Swagger-codegen version

V2.2.3

Swagger declaration file content or url

https://raw.githubusercontent.com/datadotworld/dwapi-spec/master/src/main/resources/world/data/api/swagger.json

Command line used for generation
Steps to reproduce
Related issues/PRs
Suggest a fix/enhancement

I fixed it by just checking if access_token exists before actually setting it.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the generated configuration.py auth_settings method and the init access_token initialization shown in the report, then trace the source template that produces them. Reproduce the OAuth behavior using the linked Swagger declaration and verify that a configured access token is preserved in the Authorization header rather than producing only Bearer.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, python
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.