33"""
44import sys
55
6- from .gssapi_ import REQUIRED , HTTPSPNEGOAuth # noqa
6+ import gssapi
7+
8+ from .gssapi_ import REQUIRED , HTTPSPNEGOAuth , SPNEGOExchangeError , log
79
810# python 2.7 introduced a NullHandler which we want to use, but to support
911# older versions, we implement our own if needed.
@@ -22,18 +24,34 @@ class HTTPKerberosAuth(HTTPSPNEGOAuth):
2224 def __init__ (self , mutual_authentication = REQUIRED , service = "HTTP" ,
2325 delegate = False , force_preemptive = False , principal = None ,
2426 hostname_override = None , sanitize_mutual_error_response = True ):
27+ # put this here for later
28+ self .principal = principal
29+
2530 HTTPSPNEGOAuth .__init__ (
2631 self ,
2732 mutual_authentication = mutual_authentication ,
2833 service = service ,
2934 delegate = delegate ,
3035 opportunistic_auth = force_preemptive ,
31- principal = principal ,
36+ creds = None ,
3237 hostname_override = hostname_override ,
3338 sanitize_mutual_error_response = sanitize_mutual_error_response )
3439
3540 def generate_request_header (self , response , host , is_preemptive = False ):
3641 # This method needs to be shimmed because `host` isn't exposed to
37- # __init__() and we need to derive things from it
38- return HTTPSPNEGOAuth .generate_request_header (self , response , host ,
39- is_preemptive )
42+ # __init__() and we need to derive things from it. Also, __init__()
43+ # can't fail, in the strictest compatability sense.
44+ try :
45+ if self .principal is not None :
46+ gss_stage = "acquiring credentials"
47+ name = gssapi .Name (self .principal )
48+ self .creds = gssapi .Credentials (name = name , usage = "initiate" )
49+
50+ return HTTPSPNEGOAuth .generate_request_header (self , response ,
51+ host , is_preemptive )
52+ except gssapi .exceptions .GSSError as error :
53+ msg = error .gen_message ()
54+ log .exception (
55+ "generate_request_header(): {0} failed:" .format (gss_stage ))
56+ log .exception (msg )
57+ raise SPNEGOExchangeError ("%s failed: %s" % (gss_stage , msg ))
0 commit comments