1- use mongodb:: { bson:: doc, options:: { ClientOptions , Credential , AuthMechanism } , Client } ;
21use mongodb:: options:: oidc:: { self , CallbackContext , IdpServerResponse } ;
2+ use mongodb:: {
3+ bson:: doc,
4+ bson:: Document ,
5+ options:: { ClientOptions , Credential , AuthMechanism } ,
6+ Client ,
7+ } ;
8+ use std:: error:: Error ;
9+ use futures:: FutureExt ;
310
411#[ tokio:: main]
512async fn main ( ) -> mongodb:: error:: Result < ( ) > {
@@ -19,15 +26,16 @@ async fn main() -> mongodb::error::Result<()> {
1926 // end-ldap
2027
2128 // start-azure-imds
22- client_options . credential = Credential :: builder ( )
29+ let credential = Credential :: builder ( )
2330 . username ( "<username>" . to_owned ( ) )
2431 . mechanism ( AuthMechanism :: MongoDbOidc )
2532 . mechanism_properties (
2633 doc ! { "ENVIRONMENT" : "azure" , "TOKEN_RESOURCE" : "<audience>" }
2734 )
2835 . build ( )
29- . into ( ) ; // Convert the builder into a Credential object
36+ . into ( ) ;
3037
38+ client_options. credential = Some ( credential) ;
3139 let client = Client :: with_options ( client_options) ?;
3240 let res = client
3341 . database ( "test" )
@@ -37,14 +45,16 @@ async fn main() -> mongodb::error::Result<()> {
3745 // end-azure-imds
3846
3947 // start-gcp-imds
40- opts . credential = Credential :: builder ( )
48+ let credential = Credential :: builder ( )
4149 . mechanism ( AuthMechanism :: MongoDbOidc )
4250 . mechanism_properties (
4351 doc ! { "ENVIRONMENT" : "gcp" , "TOKEN_RESOURCE" : "<audience>" }
4452 )
4553 . build ( )
4654 . into ( ) ;
47- let client = Client :: with_options ( opts) ?;
55+
56+ client_options. credential = Some ( credential) ;
57+ let client = Client :: with_options ( client_options) ?;
4858 let res = client
4959 . database ( "test" )
5060 . collection :: < Document > ( "test" )
@@ -53,11 +63,11 @@ async fn main() -> mongodb::error::Result<()> {
5363 // end-gcp-imds
5464
5565 // start-custom-callback-machine
56- opts . credential = Credential :: builder ( )
66+ let credential = Credential :: builder ( )
5767 . mechanism ( AuthMechanism :: MongoDbOidc )
5868 . oidc_callback ( oidc:: Callback :: machine ( move |_| {
5969 async move {
60- let token_file_path = std:: env:: var ( "AWS_WEB_IDENTITY_TOKEN_FILE" ) ?;
70+ let token_file_path = std:: env:: var ( "AWS_WEB_IDENTITY_TOKEN_FILE" ) . map_err ( mongodb :: error :: Error :: custom ) ?;
6171 let access_token = tokio:: fs:: read_to_string ( token_file_path) . await ?;
6272 Ok ( IdpServerResponse {
6373 access_token,
@@ -70,26 +80,27 @@ async fn main() -> mongodb::error::Result<()> {
7080 . build ( )
7181 . into ( ) ;
7282
73- let client = Client :: with_options ( opts) ?;
83+ credential_options. credentials = Some ( credential) ;
84+ let client = Client :: with_options ( client_options) ?;
7485
7586 let res = client
7687 . database ( "test" )
77- . collection :: < bson :: Document > ( "test" )
78- . find_one ( doc ! { } , None )
88+ . collection :: < Document > ( "test" )
89+ . find_one ( doc ! { } )
7990 . await ?;
8091 // end-custom-callback-machine
8192
8293 // start-custom-callback-user
8394 async fn cb ( params : CallbackContext ) -> mongodb:: error:: Result < IdpServerResponse > {
84- idp_info : = params. idp_info . ok_or ( Error :: NoIDPInfo ) ?;
95+ let idp_info = params. idp_info . ok_or ( Error :: NoIDPInfo ) ?;
8596 let ( access_token, expires, refresh_token) = negotiate_with_idp ( ctx, idpInfo. Issuer ) . await ?;
8697 Ok ( oidc:: IdpServerResponse {
8798 access_token,
8899 expires : Some ( expires) ,
89100 refresh_token : Some ( refresh_token) ,
90101 } )
91102 }
92- opts . credential = Credential :: builder ( )
103+ client_options . credential = Credential :: builder ( )
93104 . mechanism ( AuthMechanism :: MongoDbOidc )
94105 . oidc_callback ( oidc:: Callback :: human ( move |c| {
95106 async move { cb ( c) . await } . boxed ( )
0 commit comments