@@ -16,7 +16,7 @@ mod x509;
1616
1717use std:: { borrow:: Cow , fmt:: Debug , str:: FromStr } ;
1818
19- use crate :: { bson:: RawDocumentBuf , bson_compat:: cstr} ;
19+ use crate :: { bson:: RawDocumentBuf , bson_compat:: cstr, options :: ClientOptions } ;
2020use derive_where:: derive_where;
2121use hmac:: { digest:: KeyInit , Mac } ;
2222use rand:: Rng ;
@@ -287,12 +287,11 @@ impl AuthMechanism {
287287 & self ,
288288 stream : & mut Connection ,
289289 credential : & Credential ,
290- server_api : Option < & ServerApi > ,
291- #[ cfg( feature = "aws-auth" ) ] http_client : & crate :: runtime:: HttpClient ,
292- #[ cfg( feature = "gssapi-auth" ) ] resolver_config : Option < & ResolverConfig > ,
290+ opts : & AuthOptions ,
293291 ) -> Result < ( ) > {
294292 self . validate_credential ( credential) ?;
295293
294+ let server_api = opts. server_api . as_ref ( ) ;
296295 match self {
297296 AuthMechanism :: ScramSha1 => {
298297 ScramVersion :: Sha1
@@ -309,14 +308,20 @@ impl AuthMechanism {
309308 }
310309 #[ cfg( feature = "gssapi-auth" ) ]
311310 AuthMechanism :: Gssapi => {
312- gssapi:: authenticate_stream ( stream, credential, server_api, resolver_config) . await
311+ gssapi:: authenticate_stream (
312+ stream,
313+ credential,
314+ server_api,
315+ opts. resolver_config . as_ref ( ) ,
316+ )
317+ . await
313318 }
314319 AuthMechanism :: Plain => {
315320 plain:: authenticate_stream ( stream, credential, server_api) . await
316321 }
317322 #[ cfg( feature = "aws-auth" ) ]
318323 AuthMechanism :: MongoDbAws => {
319- aws:: authenticate_stream ( stream, credential, server_api, http_client) . await
324+ aws:: authenticate_stream ( stream, credential, server_api, & opts . http_client ) . await
320325 }
321326 AuthMechanism :: MongoDbCr => Err ( ErrorKind :: Authentication {
322327 message : "MONGODB-CR is deprecated and not supported by this driver. Use SCRAM \
@@ -409,6 +414,28 @@ impl FromStr for AuthMechanism {
409414 }
410415}
411416
417+ #[ derive( Clone , Debug , Default ) ]
418+ // Auxiliary information needed by authentication mechanisms.
419+ pub ( crate ) struct AuthOptions {
420+ server_api : Option < ServerApi > ,
421+ #[ cfg( feature = "aws-auth" ) ]
422+ http_client : crate :: runtime:: HttpClient ,
423+ #[ cfg( feature = "gssapi-auth" ) ]
424+ resolver_config : Option < ResolverConfig > ,
425+ }
426+
427+ impl From < & ClientOptions > for AuthOptions {
428+ fn from ( opts : & ClientOptions ) -> Self {
429+ Self {
430+ server_api : opts. server_api . clone ( ) ,
431+ #[ cfg( feature = "aws-auth" ) ]
432+ http_client : crate :: runtime:: HttpClient :: default ( ) ,
433+ #[ cfg( feature = "gssapi-auth" ) ]
434+ resolver_config : opts. resolver_config . clone ( ) ,
435+ }
436+ }
437+ }
438+
412439/// A struct containing authentication information.
413440///
414441/// Some fields (mechanism and source) may be omitted and will either be negotiated or assigned a
@@ -495,10 +522,8 @@ impl Credential {
495522 pub ( crate ) async fn authenticate_stream (
496523 & self ,
497524 conn : & mut Connection ,
498- server_api : Option < & ServerApi > ,
499525 first_round : Option < FirstRound > ,
500- #[ cfg( feature = "aws-auth" ) ] http_client : & crate :: runtime:: HttpClient ,
501- #[ cfg( feature = "gssapi-auth" ) ] resolver_config : Option < & ResolverConfig > ,
526+ opts : & AuthOptions ,
502527 ) -> Result < ( ) > {
503528 let stream_description = conn. stream_description ( ) ?;
504529
@@ -510,6 +535,7 @@ impl Credential {
510535 // If speculative authentication returned a response, then short-circuit the authentication
511536 // logic and use the first round from the handshake.
512537 if let Some ( first_round) = first_round {
538+ let server_api = opts. server_api . as_ref ( ) ;
513539 return match first_round {
514540 FirstRound :: Scram ( version, first_round) => {
515541 version
@@ -530,17 +556,7 @@ impl Credential {
530556 Some ( ref m) => Cow :: Borrowed ( m) ,
531557 } ;
532558 // Authenticate according to the chosen mechanism.
533- mechanism
534- . authenticate_stream (
535- conn,
536- self ,
537- server_api,
538- #[ cfg( feature = "aws-auth" ) ]
539- http_client,
540- #[ cfg( feature = "gssapi-auth" ) ]
541- resolver_config,
542- )
543- . await
559+ mechanism. authenticate_stream ( conn, self , opts) . await
544560 }
545561
546562 #[ cfg( test) ]
0 commit comments