Skip to content

Commit

Permalink
Add --auth-plugin option that allows using custom authentication hand…
Browse files Browse the repository at this point in the history
…lers
  • Loading branch information
Fedor Biryukov committed May 16, 2020
1 parent f34f8d5 commit 80a251c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions news/4475.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support for custom authentication handlers via --auth-plugin option.
32 changes: 32 additions & 0 deletions src/pip/_internal/cli/cmdoptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
from optparse import SUPPRESS_HELP, Option, OptionGroup
from textwrap import dedent

from pip._vendor.pkg_resources import iter_entry_points

from pip._internal.cli.progress_bars import BAR_TYPES
from pip._internal.exceptions import CommandError
from pip._internal.locations import USER_CACHE_DIR, get_src_prefix
Expand Down Expand Up @@ -920,6 +922,35 @@ def check_list_path_option(options):
) # type: Callable[..., Option]


discovered_auth_plugins = {
entry_point.name: entry_point
for entry_point
in iter_entry_points('pip.auth_plugins')
}


def _handle_auth_plugin(option, opt_str, value, parser):
# type: (Option, str, str, OptionParser) -> None
plugin = discovered_auth_plugins[value].load()
setattr(parser.values, option.dest, plugin.Auth)


auth_plugin = partial(
PipOption,
'--auth', '--auth-plugin',
dest='auth_class',
type='choice',
metavar='plugin',
default=None,
choices=list(discovered_auth_plugins.keys()),
action='callback',
callback=_handle_auth_plugin,
help='Specify authentication plugin to be used: {}.'.format(
', '.join(discovered_auth_plugins.keys()) or 'no plugins found'
),
) # type: Callable[..., Option]


##########
# groups #
##########
Expand Down Expand Up @@ -948,6 +979,7 @@ def check_list_path_option(options):
no_color,
no_python_version_warning,
unstable_feature,
auth_plugin,
]
} # type: Dict[str, Any]

Expand Down
1 change: 1 addition & 0 deletions src/pip/_internal/cli/req_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def _build_session(self, options, retries=None, timeout=None):
retries=retries if retries is not None else options.retries,
trusted_hosts=options.trusted_hosts,
index_urls=self._get_index_urls(options),
auth_class=options.auth_class,
)

# Handle custom ca-bundles from the user
Expand Down
5 changes: 4 additions & 1 deletion src/pip/_internal/network/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ def __init__(self, *args, **kwargs):
cache = kwargs.pop("cache", None)
trusted_hosts = kwargs.pop("trusted_hosts", []) # type: List[str]
index_urls = kwargs.pop("index_urls", None)
auth_class = kwargs.pop("auth_class", None)
if auth_class is None:
auth_class = MultiDomainBasicAuth

super(PipSession, self).__init__(*args, **kwargs)

Expand All @@ -249,7 +252,7 @@ def __init__(self, *args, **kwargs):
self.headers["User-Agent"] = user_agent()

# Attach our Authentication handler to the session
self.auth = MultiDomainBasicAuth(index_urls=index_urls)
self.auth = auth_class(index_urls=index_urls)

# Create our urllib3.Retry instance which will allow us to customize
# how we handle retries.
Expand Down

0 comments on commit 80a251c

Please sign in to comment.