Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

apiKey securityScheme support #600

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

Closed
jrobbins-LiveData opened this issue Apr 20, 2022 · 6 comments
Closed

apiKey securityScheme support #600

jrobbins-LiveData opened this issue Apr 20, 2022 · 6 comments
Labels
✨ enhancement New feature or improvement 🍭 OpenAPI Compliance Supporting a new bit of the OpenAPI spec

Comments

@jrobbins-LiveData
Copy link

I'm using a third-party API which has this securityScheme:

components:
  securitySchemes:
    jwtAuth:
      type: apiKey
      in: header
      name: Auth

and this path (redacted as it is not my API):

  /auth/login:
    put:
      security:
        - {}
      tags:
        - Authentication
      requestBody:
        content:
           .
           .
           .

      responses:
        '200':
          headers:
            auth:
              schema:
                type: string
                description: 
                format: jwt

The generated code for this API is:

@attr.s(auto_attribs=True)
class AuthenticatedClient(Client):
    """A Client which has been authenticated for use on secured endpoints"""

    token: str

    def get_headers(self) -> Dict[str, str]:
        """Get headers to be used in authenticated endpoints"""
        return {"Authorization": f"Bearer {self.token}", **self.headers}

The desired code is:

@attr.s(auto_attribs=True)
class AuthenticatedClient(Client):
    """A Client which has been authenticated for use on secured endpoints"""

    token: str

    def get_headers(self) -> Dict[str, str]:
        """Get headers to be used in authenticated endpoints"""
        return {"Auth": self.token, **self.headers}
@jrobbins-LiveData jrobbins-LiveData added the ✨ enhancement New feature or improvement label Apr 20, 2022
@scorgn
Copy link

scorgn commented May 15, 2022

I also am dealing with openapi docs that use Basic HTTP Authentication

  securitySchemes:
    basicAuth:
      type: http
      scheme: basic

I'm then adding to my request...

      security:
        - basicAuth: []

openapi-python-client does understand that the request should be an authenticated request, since it defines client parameter in the signature of sync/async/sync_detailed/async_detailed methods to be of the type AuthenticatedClient as opposed to just Client. But, the AuthenticatedClient seems to default to a bearer token (as @jrobbins-LiveData shows).

@Nov1kov
Copy link

Nov1kov commented Aug 15, 2022

Is it posible to fix by templates?

@Nov1kov
Copy link

Nov1kov commented Aug 15, 2022

Yes, it is.

Workaround:

Please follow this https://github.com/openapi-generators/openapi-python-client#using-custom-templates
Edit this line in your custom template as you want.

return {"Authorization": f"Bearer {self.token}", **self.headers}

@dbanty
Copy link
Collaborator

dbanty commented Aug 17, 2022

Awesome, thanks for providing a workaround @Nov1kov !

@philnagel
Copy link

Maybe it would be a good idea to just pass through the auth parameter to httpx. This would allow one to easily customize authentication, or use existing "plugins", without modifying the client or template.
Here's the documentation on what is possible using that parameter, and an example of a plugin would be httpx-ntlm

@dbanty
Copy link
Collaborator

dbanty commented Feb 6, 2023

Sounds like another +1 for #202

@dbanty dbanty added the 🍭 OpenAPI Compliance Supporting a new bit of the OpenAPI spec label Aug 13, 2023
@openapi-generators openapi-generators locked and limited conversation to collaborators Aug 13, 2023
@dbanty dbanty converted this issue into discussion #816 Aug 13, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
✨ enhancement New feature or improvement 🍭 OpenAPI Compliance Supporting a new bit of the OpenAPI spec
Projects
None yet
Development

No branches or pull requests

5 participants