Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added code to force authentication on first request instead of authentication-challenge #238

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions python2/httplib2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1404,6 +1404,7 @@ def __init__(
ca_certs=None,
disable_ssl_certificate_validation=False,
ssl_version=None,
forceauth=False,
):
"""If 'cache' is a string then it is used as a directory name for
a disk cache. Otherwise it must be an object that supports the
Expand Down Expand Up @@ -1453,6 +1454,9 @@ def __init__(
# authorization objects
self.authorizations = []

# Whether to use auth on first try
self.forceauth = forceauth

# If set to False then no redirects are followed, even safe ones.
self.follow_redirects = True

Expand Down Expand Up @@ -1511,6 +1515,13 @@ def _auth_from_challenge(self, host, request_uri, headers, response, content):
if scheme in challenges:
yield AUTH_SCHEME_CLASSES[scheme](cred, host, request_uri, headers, response, content, self)

def _auth_from_definition(self, host, request_uri, headers, scheme):
"""A generator that creates an Authorization object
based on a scheme specification.
"""
for cred in self.credentials.iter(host):
yield AUTH_SCHEME_CLASSES[scheme](cred, host, request_uri, headers, '', '', self)

def add_credentials(self, name, password, domain=""):
"""Add a name and password that will be used
any time a request requires authentication."""
Expand Down Expand Up @@ -1610,6 +1621,10 @@ def _request(
"""Do the actual request using the connection object
and also follow one level of redirects if necessary"""

if self.forceauth:
for authorization in self._auth_from_definition(host, request_uri, headers, self.forceauth):
authorization.request(method, request_uri, headers, body)

auths = [(auth.depth(request_uri), auth) for auth in self.authorizations if auth.inscope(host, request_uri)]
auth = auths and sorted(auths)[0][1] or None
if auth:
Expand Down
16 changes: 16 additions & 0 deletions python3/httplib2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,7 @@ def __init__(
tls_maximum_version=None,
tls_minimum_version=None,
key_password=None,
forceauth=False,
):

self.disable_ssl_certificate_validation = disable_ssl_certificate_validation
Expand Down Expand Up @@ -1282,6 +1283,9 @@ def __init__(
# authorization objects
self.authorizations = []

# Whether to use auth on first try
self.forceauth = forceauth

# If set to False then no redirects are followed, even safe ones.
self.follow_redirects = True

Expand Down Expand Up @@ -1340,6 +1344,13 @@ def _auth_from_challenge(self, host, request_uri, headers, response, content):
if scheme in challenges:
yield AUTH_SCHEME_CLASSES[scheme](cred, host, request_uri, headers, response, content, self)

def _auth_from_definition(self, host, request_uri, headers, scheme):
"""A generator that creates an Authorization object
based on a scheme specification.
"""
for cred in self.credentials.iter(host):
yield AUTH_SCHEME_CLASSES[scheme](cred, host, request_uri, headers, '', '', self)

def add_credentials(self, name, password, domain=""):
"""Add a name and password that will be used
any time a request requires authentication."""
Expand Down Expand Up @@ -1436,6 +1447,11 @@ def _request(
"""Do the actual request using the connection object
and also follow one level of redirects if necessary"""

if self.forceauth:
for authorization in self._auth_from_definition(
host, request_uri, headers, self.forceauth):
authorization.request(method, request_uri, headers, body)

auths = [(auth.depth(request_uri), auth) for auth in self.authorizations if auth.inscope(host, request_uri)]
auth = auths and sorted(auths)[0][1] or None
if auth:
Expand Down