3535
3636if TYPE_CHECKING :
3737 from pymongo .hello import Hello
38- from pymongo .pool import SocketInfo
38+ from pymongo .pool import Connection
3939
4040HAVE_KERBEROS = True
4141_USE_PRINCIPAL = False
@@ -220,9 +220,7 @@ def _authenticate_scram_start(
220220 return nonce , first_bare , cmd
221221
222222
223- def _authenticate_scram (
224- credentials : MongoCredential , sock_info : SocketInfo , mechanism : str
225- ) -> None :
223+ def _authenticate_scram (credentials : MongoCredential , conn : Connection , mechanism : str ) -> None :
226224 """Authenticate using SCRAM."""
227225 username = credentials .username
228226 if mechanism == "SCRAM-SHA-256" :
@@ -239,13 +237,13 @@ def _authenticate_scram(
239237 # Make local
240238 _hmac = hmac .HMAC
241239
242- ctx = sock_info .auth_ctx
240+ ctx = conn .auth_ctx
243241 if ctx and ctx .speculate_succeeded ():
244242 nonce , first_bare = ctx .scram_data
245243 res = ctx .speculative_authenticate
246244 else :
247245 nonce , first_bare , cmd = _authenticate_scram_start (credentials , mechanism )
248- res = sock_info .command (source , cmd )
246+ res = conn .command (source , cmd )
249247
250248 server_first = res ["payload" ]
251249 parsed = _parse_scram_response (server_first )
@@ -285,7 +283,7 @@ def _authenticate_scram(
285283 ("payload" , Binary (client_final )),
286284 ]
287285 )
288- res = sock_info .command (source , cmd )
286+ res = conn .command (source , cmd )
289287
290288 parsed = _parse_scram_response (res ["payload" ])
291289 if not hmac .compare_digest (parsed [b"v" ], server_sig ):
@@ -301,7 +299,7 @@ def _authenticate_scram(
301299 ("payload" , Binary (b"" )),
302300 ]
303301 )
304- res = sock_info .command (source , cmd )
302+ res = conn .command (source , cmd )
305303 if not res ["done" ]:
306304 raise OperationFailure ("SASL conversation failed to complete." )
307305
@@ -345,7 +343,7 @@ def _canonicalize_hostname(hostname: str) -> str:
345343 return name [0 ].lower ()
346344
347345
348- def _authenticate_gssapi (credentials : MongoCredential , sock_info : SocketInfo ) -> None :
346+ def _authenticate_gssapi (credentials : MongoCredential , conn : Connection ) -> None :
349347 """Authenticate using GSSAPI."""
350348 if not HAVE_KERBEROS :
351349 raise ConfigurationError (
@@ -358,7 +356,7 @@ def _authenticate_gssapi(credentials: MongoCredential, sock_info: SocketInfo) ->
358356 props = credentials .mechanism_properties
359357 # Starting here and continuing through the while loop below - establish
360358 # the security context. See RFC 4752, Section 3.1, first paragraph.
361- host = sock_info .address [0 ]
359+ host = conn .address [0 ]
362360 if props .canonicalize_host_name :
363361 host = _canonicalize_hostname (host )
364362 service = props .service_name + "@" + host
@@ -413,7 +411,7 @@ def _authenticate_gssapi(credentials: MongoCredential, sock_info: SocketInfo) ->
413411 ("autoAuthorize" , 1 ),
414412 ]
415413 )
416- response = sock_info .command ("$external" , cmd )
414+ response = conn .command ("$external" , cmd )
417415
418416 # Limit how many times we loop to catch protocol / library issues
419417 for _ in range (10 ):
@@ -430,7 +428,7 @@ def _authenticate_gssapi(credentials: MongoCredential, sock_info: SocketInfo) ->
430428 ("payload" , payload ),
431429 ]
432430 )
433- response = sock_info .command ("$external" , cmd )
431+ response = conn .command ("$external" , cmd )
434432
435433 if result == kerberos .AUTH_GSS_COMPLETE :
436434 break
@@ -453,7 +451,7 @@ def _authenticate_gssapi(credentials: MongoCredential, sock_info: SocketInfo) ->
453451 ("payload" , payload ),
454452 ]
455453 )
456- sock_info .command ("$external" , cmd )
454+ conn .command ("$external" , cmd )
457455
458456 finally :
459457 kerberos .authGSSClientClean (ctx )
@@ -462,7 +460,7 @@ def _authenticate_gssapi(credentials: MongoCredential, sock_info: SocketInfo) ->
462460 raise OperationFailure (str (exc ))
463461
464462
465- def _authenticate_plain (credentials : MongoCredential , sock_info : SocketInfo ) -> None :
463+ def _authenticate_plain (credentials : MongoCredential , conn : Connection ) -> None :
466464 """Authenticate using SASL PLAIN (RFC 4616)"""
467465 source = credentials .source
468466 username = credentials .username
@@ -476,52 +474,50 @@ def _authenticate_plain(credentials: MongoCredential, sock_info: SocketInfo) ->
476474 ("autoAuthorize" , 1 ),
477475 ]
478476 )
479- sock_info .command (source , cmd )
477+ conn .command (source , cmd )
480478
481479
482- def _authenticate_x509 (credentials : MongoCredential , sock_info : SocketInfo ) -> None :
480+ def _authenticate_x509 (credentials : MongoCredential , conn : Connection ) -> None :
483481 """Authenticate using MONGODB-X509."""
484- ctx = sock_info .auth_ctx
482+ ctx = conn .auth_ctx
485483 if ctx and ctx .speculate_succeeded ():
486484 # MONGODB-X509 is done after the speculative auth step.
487485 return
488486
489- cmd = _X509Context (credentials , sock_info .address ).speculate_command ()
490- sock_info .command ("$external" , cmd )
487+ cmd = _X509Context (credentials , conn .address ).speculate_command ()
488+ conn .command ("$external" , cmd )
491489
492490
493- def _authenticate_mongo_cr (credentials : MongoCredential , sock_info : SocketInfo ) -> None :
491+ def _authenticate_mongo_cr (credentials : MongoCredential , conn : Connection ) -> None :
494492 """Authenticate using MONGODB-CR."""
495493 source = credentials .source
496494 username = credentials .username
497495 password = credentials .password
498496 # Get a nonce
499- response = sock_info .command (source , {"getnonce" : 1 })
497+ response = conn .command (source , {"getnonce" : 1 })
500498 nonce = response ["nonce" ]
501499 key = _auth_key (nonce , username , password )
502500
503501 # Actually authenticate
504502 query = SON ([("authenticate" , 1 ), ("user" , username ), ("nonce" , nonce ), ("key" , key )])
505- sock_info .command (source , query )
503+ conn .command (source , query )
506504
507505
508- def _authenticate_default (credentials : MongoCredential , sock_info : SocketInfo ) -> None :
509- if sock_info .max_wire_version >= 7 :
510- if sock_info .negotiated_mechs :
511- mechs = sock_info .negotiated_mechs
506+ def _authenticate_default (credentials : MongoCredential , conn : Connection ) -> None :
507+ if conn .max_wire_version >= 7 :
508+ if conn .negotiated_mechs :
509+ mechs = conn .negotiated_mechs
512510 else :
513511 source = credentials .source
514- cmd = sock_info .hello_cmd ()
512+ cmd = conn .hello_cmd ()
515513 cmd ["saslSupportedMechs" ] = source + "." + credentials .username
516- mechs = sock_info .command (source , cmd , publish_events = False ).get (
517- "saslSupportedMechs" , []
518- )
514+ mechs = conn .command (source , cmd , publish_events = False ).get ("saslSupportedMechs" , [])
519515 if "SCRAM-SHA-256" in mechs :
520- return _authenticate_scram (credentials , sock_info , "SCRAM-SHA-256" )
516+ return _authenticate_scram (credentials , conn , "SCRAM-SHA-256" )
521517 else :
522- return _authenticate_scram (credentials , sock_info , "SCRAM-SHA-1" )
518+ return _authenticate_scram (credentials , conn , "SCRAM-SHA-1" )
523519 else :
524- return _authenticate_scram (credentials , sock_info , "SCRAM-SHA-1" )
520+ return _authenticate_scram (credentials , conn , "SCRAM-SHA-1" )
525521
526522
527523_AUTH_MAP : Mapping [str , Callable ] = {
@@ -606,12 +602,12 @@ def speculate_command(self) -> Optional[MutableMapping[str, Any]]:
606602
607603
608604def authenticate (
609- credentials : MongoCredential , sock_info : SocketInfo , reauthenticate : bool = False
605+ credentials : MongoCredential , conn : Connection , reauthenticate : bool = False
610606) -> None :
611- """Authenticate sock_info ."""
607+ """Authenticate connection ."""
612608 mechanism = credentials .mechanism
613609 auth_func = _AUTH_MAP [mechanism ]
614610 if mechanism == "MONGODB-OIDC" :
615- _authenticate_oidc (credentials , sock_info , reauthenticate )
611+ _authenticate_oidc (credentials , conn , reauthenticate )
616612 else :
617- auth_func (credentials , sock_info )
613+ auth_func (credentials , conn )
0 commit comments