Skip to content

Commit

Permalink
Add auth_override_provider mechanism pyapa/pip#4475
Browse files Browse the repository at this point in the history
  • Loading branch information
jpedrick-athenacr committed Jun 12, 2023
1 parent 2d168e6 commit 0205e2e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/pip/_internal/cli/cmdoptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,21 @@ class PipOption(Option):
),
)

auth_override_provider: Callable[..., Option] = partial(
Option,
"--auth-override-provider",
dest="auth_override_provider",
type="str",
default=None,
help=(
"Python module which can replace MultiDomainBasicAuth."
"The module will be imported using importlib.import_module"
" expects a python file, with a function: "
" create_auth_override_provider(auth_module, index_urls)."
" (default: None)"
),
)

proxy: Callable[..., Option] = partial(
Option,
"--proxy",
Expand Down Expand Up @@ -1060,6 +1075,7 @@ def check_list_path_option(options: Values) -> None:
no_python_version_warning,
use_new_feature,
use_deprecated_feature,
auth_override_provider,
],
}

Expand Down
13 changes: 12 additions & 1 deletion src/pip/_internal/network/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import email.utils
import importlib
import io
import ipaddress
import json
Expand Down Expand Up @@ -326,6 +327,7 @@ def __init__(
trusted_hosts: Sequence[str] = (),
index_urls: Optional[List[str]] = None,
ssl_context: Optional["SSLContext"] = None,
auth_override_provider = None,
**kwargs: Any,
) -> None:
"""
Expand All @@ -342,7 +344,16 @@ def __init__(
self.headers["User-Agent"] = user_agent()

# Attach our Authentication handler to the session
self.auth = MultiDomainBasicAuth(index_urls=index_urls)
if auth_override_provider:
import pip._internal.network.auth
m = importlib.import_module(auth_override_provider)
# pass the module so users can implement AuthBase and/or
# provide fallback to MultiDomainBasicAuth
self.auth = m.create_auth_override_provider(
index_urls=index_urls,
auth_module=pip._internal.network.auth)
else:
self.auth = MultiDomainBasicAuth(index_urls=index_urls)

# Create our urllib3.Retry instance which will allow us to customize
# how we handle retries.
Expand Down
24 changes: 24 additions & 0 deletions tests/lib/auth_override_provider_dummy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@


def create_auth_override_provider(auth_module, index_urls):
class DummyAuthOverrideProvider(auth_module.AuthBase):
def __init__(self, index_urls):
self.multi_domain_basic_auth = auth_module.MultiDomainBasicAuth(
index_urls=index_urls)


@property
def index_urls(self):
return self.multi_domain_basic_auth.index_urls


@index_urls.setter
def set_index_urls(self, index_urls):
self.multi_domain_basic_auth.index_urls = index_urls


def __call__(self, r):
return self.multi_domain_basic_auth(r)


return DummyAuthOverrideProvider(index_urls=index_urls)
8 changes: 8 additions & 0 deletions tests/unit/test_network_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ def test_cache_is_enabled(self, tmpdir: Path) -> None:

assert session.adapters["https://"].cache.directory == cache_directory

def test_auth_override_provider(self, tmpdir: Path) -> None:
cache_directory = os.fspath(tmpdir.joinpath("test-cache"))
session = PipSession(
cache=cache_directory,
auth_override_provider='tests.lib.auth_override_provider_dummy')

assert session.auth.__class__.__name__ == "DummyAuthOverrideProvider"

def test_http_cache_is_not_enabled(self, tmpdir: Path) -> None:
session = PipSession(cache=os.fspath(tmpdir.joinpath("test-cache")))

Expand Down

0 comments on commit 0205e2e

Please sign in to comment.