Skip to content

Commit 7288f99

Browse files
committed
Raise exceptions if CBs are requested but not available
Signed-off-by: Simo Sorce <[email protected]>
1 parent 79ef176 commit 7288f99

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

src/requests_gssapi/gssapi_.py

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -155,33 +155,29 @@ def generate_request_header(self, response, host, is_preemptive=False):
155155
gss_cb = None
156156
if self.channel_bindings == "tls-server-end-point":
157157
if is_preemptive:
158-
log.warning("channel_bindings were requested, but are unavailable for opportunistic authentication")
158+
raise SPNEGOExchangeError(
159+
"channel_bindings were requested, but are unavailable for opportunistic authentication"
160+
)
159161
# The 'connection' attribute on raw is a public urllib3 API
160162
# and can be None if the connection has been released.
161163
elif getattr(response.raw, "connection", None) and getattr(response.raw.connection, "sock", None):
162-
try:
163-
# Defer import so it's not a hard dependency.
164-
from cryptography import x509
165-
166-
sock = response.raw.connection.sock
167-
168-
der_cert = sock.getpeercert(binary_form=True)
169-
cert = x509.load_der_x509_certificate(der_cert)
170-
hash = cert.signature_hash_algorithm
171-
cert_hash = cert.fingerprint(hash)
172-
173-
app_data = b"tls-server-end-point:" + cert_hash
174-
gss_cb = gssapi.raw.ChannelBindings(application_data=app_data)
175-
log.debug("generate_request_header(): Successfully retrieved channel bindings")
176-
except ImportError:
177-
log.warning("Could not import cryptography, python-cryptography is required for this feature.")
178-
except Exception:
179-
log.warning(
180-
"Failed to get channel bindings from socket",
181-
exc_info=True,
182-
)
164+
# Defer import so it's not a hard dependency.
165+
from cryptography import x509
166+
167+
sock = response.raw.connection.sock
168+
169+
der_cert = sock.getpeercert(binary_form=True)
170+
cert = x509.load_der_x509_certificate(der_cert)
171+
hash = cert.signature_hash_algorithm
172+
cert_hash = cert.fingerprint(hash)
173+
174+
app_data = b"tls-server-end-point:" + cert_hash
175+
gss_cb = gssapi.raw.ChannelBindings(application_data=app_data)
176+
log.debug("generate_request_header(): Successfully retrieved channel bindings")
183177
else:
184-
log.warning("channel_bindings were requested, but a socket could not be retrieved from the response")
178+
raise SPNEGOExchangeError(
179+
"channel_bindings were requested, but a socket could not be retrieved from the response"
180+
)
185181

186182
try:
187183
gss_stage = "initiating context"

0 commit comments

Comments
 (0)