Skip to content

apiKey securityScheme support #600

Description

@jrobbins-LiveData

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}

Activity

  1. scorgn commented on May 15, 2022

    @scorgn

    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).

  2. Nov1kov commented on Aug 15, 2022

    @Nov1kov

    Is it posible to fix by templates?

  3. Nov1kov commented on Aug 15, 2022

    @Nov1kov

    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}

  4. dbanty commented on Aug 17, 2022

    @dbanty
    Collaborator

    Awesome, thanks for providing a workaround @Nov1kov !

  5. philnagel commented on Feb 6, 2023

    @philnagel

    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

  6. dbanty commented on Feb 6, 2023

    @dbanty
    Collaborator

    Sounds like another +1 for #202

  7. locked and limited conversation to collaborators on Aug 13, 2023
  8. converted this issue into a discussion #816 on Aug 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions