4242def _negotiate_value (response : Response ) -> Optional [bytes ]:
4343 """Extracts the gssapi authentication token from the appropriate header"""
4444 authreq = response .headers .get ('www-authenticate' , None )
45- if authreq :
46- match_obj = _find_auth (authreq )
47- if match_obj :
48- return b64decode (match_obj .group (1 ))
45+ if authreq and (match := _find_auth (authreq )):
46+ return b64decode (match .group (1 ))
47+ return None
4948
5049
5150def _sanitize_response (response : Response ):
@@ -139,10 +138,10 @@ class HTTPSPNEGOAuth(Auth):
139138
140139 def __init__ (self ,
141140 mutual_authentication : int = DISABLED ,
142- target_name : Optional [str ] = "HTTP" ,
141+ target_name : Optional [Union [ str , gssapi . Name ] ] = "HTTP" ,
143142 delegate : bool = False ,
144143 opportunistic_auth : bool = False ,
145- creds : gssapi .Credentials = None ,
144+ creds : Optional [ gssapi .Credentials ] = None ,
146145 mech : Optional [Union [bytes , gssapi .OID ]] = SPNEGO ,
147146 sanitize_mutual_error_response : bool = True ):
148147 self .mutual_authentication = mutual_authentication
@@ -165,7 +164,7 @@ def auth_flow(self, request: Request) -> FlowGen:
165164
166165 def handle_response (self ,
167166 response : Response ,
168- ctx : SecurityContext = None ) -> FlowGen :
167+ ctx : Optional [ SecurityContext ] = None ) -> FlowGen :
169168 num_401s = 0
170169 while response .status_code == 401 and num_401s < 2 :
171170 num_401s += 1
@@ -235,7 +234,7 @@ def handle_mutual_auth(self, response: Response, ctx: SecurityContext):
235234 @_handle_gsserror (gss_stage = 'stepping' , result = _gss_to_spnego_error )
236235 def set_auth_header (self ,
237236 request : Request ,
238- response : Response = None ) -> SecurityContext :
237+ response : Optional [ Response ] = None ) -> SecurityContext :
239238 """
240239 Create a new security context, generate the GSSAPI authentication
241240 token, and insert it into the request header. The new security context
@@ -286,17 +285,17 @@ def _make_context(self, request: Request) -> SecurityContext:
286285 used if it isn't included in :py:attr:`target_name`.
287286 """
288287 name = self .target_name
289- if type (name ) != gssapi .Name : # type(name) is str
290- if '@' not in name :
288+ if type (name ) != gssapi .Name : # None/ str
289+ if isinstance ( name , str ) and '@' not in name :
291290 name += f"@{ request .url .host } "
292291 name = gssapi .Name (name , gssapi .NameType .hostbased_service )
293292
294293 return SecurityContext (
295294 usage = "initiate" ,
296- flags = self ._gssflags ,
295+ flags = self ._gssflags , # type: ignore[arg-type] # list[int] is fine
297296 name = name ,
298297 creds = self .creds ,
299- mech = self .mech ,
298+ mech = self .mech , # type: ignore[arg-type] # bytes are fine
300299 )
301300
302301 @property
0 commit comments