ory / ory/sdk

Python SDK: BasicAuth not working

Open
#200 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
PHP
Stars
178
Forks
96
Avg merge
6d 23h
Merged PRs (30d)
3

Description

Preflight checklist
Describe the bug

The configuration class allows for basic authentication credentials to be passed, yet, when actually issuing requests, the credentials that are configured are skipped, leading to 401 status codes.

Reproducing the bug

Instantiate a client as follows:

from ory_hydra_client.configuration import Configuration
from ory_hydra_client.api import admin_api
from ory_hydra_client.api_client import ApiClient

configuration = Configuration(host=host, username=username, password=password)
with ApiClient(configuration) as api_client:
    api = admin_api.AdminApi(api_client)

Try and issue a login request on a Hydra instance that is protected with BasicAuth, e.g.:

api.get_login_request(challenge)

Expected result: A properly authenticated request is executed.

Actual result: HTTP 401 due to missing authorization headers.

Why is this happening? Look at the endpoint definition:

        self.get_login_request_endpoint = _Endpoint(
            settings={
                'response_type': (LoginRequest,),
                'auth': [],
                'endpoint_path': '/oauth2/auth/requests/login',
                'operation_id': 'get_login_request',
                'http_method': 'GET',
                'servers': None,
            },

Note the empty auth. Now, the credentials are actually picked up by this piece of code -- self.configuration.auth_settings():

    def update_params_for_auth(self, headers, queries, auth_settings,
                               resource_path, method, body):
        """Updates header and query params based on authentication setting.

        :param headers: Header parameters dict to be updated.
        :param queries: Query parameters tuple list to be updated.
        :param auth_settings: Authentication setting identifiers list.
        :param resource_path: A string representation of the HTTP request resource path.
        :param method: A string representation of the HTTP request method.
        :param body: A object representing the body of the HTTP request.
            The object type is the return value of _encoder.default().
        """
        if not auth_settings:
            return

        for auth in auth_settings:
            auth_setting = self.configuration.auth_settings().get(auth)

But, that code does not kick in if auth_settings is empty, which is the case due to the login request endpoint definition above.

Workaround:

api.api_client.default_headers["authorization"] = api.api_client.configuration.get_basic_auth_token()

Related:

Relevant log output

No response

Relevant configuration

No response

Version

ory-hydra-client 1.11.8

On which operating system are you observing this issue?

Linux

In which environment are you deploying?

Kubernetes

Additional Context

No response

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 endpoint definition for get_login_request and ApiClient.update_params_for_auth, then inspect Configuration.auth_settings() and the related issues 55 and 156. Confirm how BasicAuth is selected for this endpoint and run the Python SDK tests or reproduce the request against a BasicAuth-protected Hydra instance. Done means configured credentials produce an authenticated request instead of HTTP 401.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, authentication
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.