Skip to content

Commit c6a2420

Browse files
committed
Warn, don't raise, on opportunistic auth failure
When opportunistic_auth is enabled but an Authorization header could not be generated opportunistically, log a warning rather than raising an exception. If the request results in a 401, this reduces to the non-opportunistic case, whose behavior remains the same as before. This allows callers for whom enabling opportunistic auth is a more appropriate default to do so, without needing to write additional exception handling code to ensure they recover in the case that opportunistic auth failed. In other words, take "opportunistic" to mean "try early, but if that fails, don't just give up right away." Signed-off-by: Joshua Bronson <jab@twosigma.com>
1 parent 492a4ab commit c6a2420

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

Diff for: requests_gssapi/gssapi_.py

+13-8
Original file line numberDiff line numberDiff line change
@@ -306,14 +306,19 @@ def __call__(self, request):
306306
# by the 401 handler
307307
host = urlparse(request.url).hostname
308308

309-
auth_header = self.generate_request_header(None, host,
310-
is_preemptive=True)
311-
312-
log.debug(
313-
"HTTPSPNEGOAuth: Preemptive Authorization header: {0}"
314-
.format(auth_header))
315-
316-
request.headers['Authorization'] = auth_header
309+
try:
310+
auth_header = self.generate_request_header(None, host,
311+
is_preemptive=True)
312+
except SPNEGOExchangeError as exc:
313+
log.warning(
314+
"HTTPSPNEGOAuth: Opportunistic auth failed with %s ->"
315+
" sending request without adding Authorization header."
316+
" Will try again if it results in a 401.",
317+
exc,
318+
)
319+
else:
320+
log.debug("HTTPSPNEGOAuth: Adding opportunistic auth header")
321+
request.headers['Authorization'] = auth_header
317322

318323
request.register_hook('response', self.handle_response)
319324
try:

0 commit comments

Comments
 (0)