Skip to content

Add support for basic auth #525

@johnthagen

Description

@johnthagen

Is your feature request related to a problem? Please describe.

OpenAPI 3 supports expressing basic auth support: https://swagger.io/docs/specification/authentication/basic-authentication/

While basic auth is often not ideal for production, during development basic auth can be quite handy. Currently it is not possible to directly use basic auth with the generated Python client.

Describe the solution you'd like

Detect if an API supports basic auth and provide it as an alternative AuthenticatedClient.

An example implementation:

from base64 import b64encode
from typing import Dict

import attr

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

    username: str
    password: str

    def get_headers(self) -> Dict[str, str]:
        """Get headers to be used in authenticated endpoints"""
        encoded_credentials = b64encode(f"{self.username}:{self.password}".encode()).decode()
        return {"Authorization": f"Basic {encoded_credentials}", **self.headers}

Describe alternatives you've considered

The AuthenticatedClient could be made to take either a token or username/password.

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