Skip to content

Commit e56466f

Browse files
twosigmajabfrozencemetery
authored andcommitted
Warn instead of raising on opportunistic auth failure
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> [rharwood@redhat.com: restore debug message, fussy style things]
1 parent 492a4ab commit e56466f

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

requests_gssapi/gssapi_.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -306,14 +306,21 @@ 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+
log.debug(
313+
"HTTPSPNEGOAuth: Preemptive Authorization header: {0}"
314+
.format(auth_header))
315+
except SPNEGOExchangeError as exc:
316+
log.warning(
317+
"HTTPSPNEGOAuth: Opportunistic auth failed with %s ->"
318+
" sending request without adding Authorization header."
319+
" Will try again if it results in a 401.",
320+
exc)
321+
else:
322+
log.debug("HTTPSPNEGOAuth: Added opportunistic auth header")
323+
request.headers['Authorization'] = auth_header
317324

318325
request.register_hook('response', self.handle_response)
319326
try:

0 commit comments

Comments
 (0)