Skip to content

Commit 241fe3d

Browse files
authored
RUST-1936: Create public interface for oidc authentication (#1091)
1 parent 42541ff commit 241fe3d

File tree

5 files changed

+201
-111
lines changed

5 files changed

+201
-111
lines changed

src/client/auth.rs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
44
#[cfg(feature = "aws-auth")]
55
pub(crate) mod aws;
6-
pub(crate) mod oidc;
6+
/// Contains the functionality for [`OIDC`](https://openid.net/developers/how-connect-works/) authorization and authentication.
7+
pub mod oidc;
78
mod plain;
89
mod sasl;
910
mod scram;
@@ -227,8 +228,8 @@ impl AuthMechanism {
227228
.map_or(false, |s| s != "$external")
228229
{
229230
return Err(Error::invalid_argument(format!(
230-
"source must be $external for {} authentication",
231-
MONGODB_OIDC_STR
231+
"source must be $external for {} authentication, found: {:?}",
232+
MONGODB_OIDC_STR, credential.source
232233
)));
233234
}
234235
if credential.password.is_some() {
@@ -470,15 +471,32 @@ pub struct Credential {
470471
pub mechanism_properties: Option<Document>,
471472

472473
/// The token callback for OIDC authentication.
473-
// TODO RUST-1497: make this `pub`
474-
// Credential::builder().oidc_callback(oidc::Callback::human(...)).build()
475-
// the name of the field here does not well encompass what this field actually is since
476-
// it contains all the OIDC state information, not just the callback, but it conforms
477-
// to how a user would interact with it.
474+
/// ```
475+
/// use mongodb::{error::Error, Client, options::{ClientOptions, oidc::{Callback, CallbackContext, IdpServerResponse}}};
476+
/// use std::time::{Duration, Instant};
477+
/// use futures::future::FutureExt;
478+
/// async fn do_human_flow(c: CallbackContext) -> Result<(String, Option<Instant>, Option<String>), Error> {
479+
/// // Do the human flow here see: https://auth0.com/docs/authenticate/login/oidc-conformant-authentication/oidc-adoption-auth-code-flow
480+
/// Ok(("some_access_token".to_string(), Some(Instant::now() + Duration::from_secs(60 * 60 * 12)), Some("some_refresh_token".to_string())))
481+
/// }
482+
///
483+
/// async fn setup_client() -> Result<Client, Error> {
484+
/// let mut opts =
485+
/// ClientOptions::parse("mongodb://localhost:27017,localhost:27018/admin?authSource=admin&authMechanism=MONGODB-OIDC").await?;
486+
/// opts.credential.as_mut().unwrap().oidc_callback =
487+
/// Callback::human(move |c: CallbackContext| {
488+
/// async move {
489+
/// let (access_token, expires, refresh_token) = do_human_flow(c).await?;
490+
/// Ok(IdpServerResponse::builder().access_token(access_token).expires(expires).refresh_token(refresh_token).build())
491+
/// }.boxed()
492+
/// });
493+
/// Client::with_options(opts)
494+
/// }
495+
/// ```
478496
#[serde(skip)]
479497
#[derivative(Debug = "ignore", PartialEq = "ignore")]
480498
#[builder(default)]
481-
pub(crate) oidc_callback: oidc::State,
499+
pub oidc_callback: oidc::Callback,
482500
}
483501

484502
impl Credential {

0 commit comments

Comments
 (0)