Skip to content

Commit be0cee4

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

File tree

1 file changed

+15
-23
lines changed

1 file changed

+15
-23
lines changed

src/requests_gssapi/gssapi_.py

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -155,33 +155,25 @@ 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("channel_bindings were requested, but are unavailable for opportunistic authentication")
159159
# The 'connection' attribute on raw is a public urllib3 API
160160
# and can be None if the connection has been released.
161161
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-
)
162+
# Defer import so it's not a hard dependency.
163+
from cryptography import x509
164+
165+
sock = response.raw.connection.sock
166+
167+
der_cert = sock.getpeercert(binary_form=True)
168+
cert = x509.load_der_x509_certificate(der_cert)
169+
hash = cert.signature_hash_algorithm
170+
cert_hash = cert.fingerprint(hash)
171+
172+
app_data = b"tls-server-end-point:" + cert_hash
173+
gss_cb = gssapi.raw.ChannelBindings(application_data=app_data)
174+
log.debug("generate_request_header(): Successfully retrieved channel bindings")
183175
else:
184-
log.warning("channel_bindings were requested, but a socket could not be retrieved from the response")
176+
raise SPNEGOExchangeError("channel_bindings were requested, but a socket could not be retrieved from the response")
185177

186178
try:
187179
gss_stage = "initiating context"

0 commit comments

Comments
 (0)