Skip to content

Commit b23dd03

Browse files
committed
technical feedback
1 parent e11bfcb commit b23dd03

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

source/fundamentals/enterprise-auth.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ If your application runs on an Azure VM, or otherwise uses the
142142
built-in Azure support.
143143

144144
You can configure OIDC for Azure IMDS by setting the ``mechanism`` field of your
145-
``Credential`` struct to ``AuthMechanism::MongoDBOidc``. This example specifies
145+
``Credential`` struct to ``AuthMechanism::MongoDbOidc``. This example specifies
146146
the authentication mechanism by using the following placeholders:
147147

148148
- ``username``: If you're using an Azure managed identity, set this to the
@@ -174,7 +174,7 @@ you can authenticate to MongoDB by using the {+driver-short+}'s built-in GCP
174174
support.
175175

176176
You can configure OIDC for GCP IMDS by setting the ``mechanism`` field of your
177-
``Credential`` struct to ``AuthMechanism::MongoDBOidc``. Then, specify the
177+
``Credential`` struct to ``AuthMechanism::MongoDbOidc``. Then, specify the
178178
authentication mechanism by setting the following values in the
179179
``mechanism_properties`` field:
180180

source/includes/fundamentals/code-snippets/enterprise-auth.rs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
use mongodb::{ bson::doc, options::{ ClientOptions, Credential, AuthMechanism }, Client };
21
use 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]
512
async 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

Comments
 (0)