dittolive_ditto::identity

Struct DittoAuthenticator

Source
pub struct DittoAuthenticator { /* private fields */ }
Expand description

Use ditto.auth() to manage authentication when using applicable identites.

The DittoAuthenticator is available when using the OnlinePlayground and OnlineWithAuthentication identities.

Implementations§

Source§

impl DittoAuthenticator

Source

pub fn set_expiration_handler<F>(&self, handler: F)

Set a callback to notify the client when the authentication is expiring.

When using DittoConfigConnect::Server { .. } mode, Ditto requires you to register an “expiration handler” for authentication. This handler is called for an initial authentication and periodically thereafter when the current authentication is near to expiration.

For more details about authentication, see the Ditto Auth and Authorization docs.

For more details about the expiration handler, see the DittoAuthExpirationHandler trait.

§Example
async fn sample_get_token() -> anyhow::Result<String> {
    // e.g. reqwest::get("https://example.com/token").await?.text().await?;
    Ok("token".to_string())
}

let auth = ditto
    .auth()
    .expect("Auth is available in Server connect mode");
auth.set_expiration_handler(async |ditto: &Ditto, duration_remaining| {
    let auth = ditto
        .auth()
        .expect("Auth is available in Server connect mode");

    // Call your auth service to get a new token
    let token = sample_get_token().await.unwrap();

    // Where "my-provider" is the name of the Authentication Webhook
    // you've configured on the Ditto Portal
    let result = auth.login(&token, "my-provider");

    if let Err(login_error) = result {
        // Handle login error, e.g.:
        error!("Failed to login: {}", login_error);
    }
});
Source

pub fn clear_expiration_handler(&self)

Clear the expiration handler, if set.

Source§

impl DittoAuthenticator

Source

pub fn login( &self, token: &str, provider: &str, ) -> Result<AuthenticationClientFeedback, DittoError>

Asks the Ditto instance to make an auth request to the configured [Identity]’s auth URL with a single token parameter.

  • token: An auth or API token you have configured.
  • provider: The name of an authentication provider web hook you have configured in Ditto. Cloud, which will accept the token and contact your Auth service
Source

pub fn logout<R>( &self, cleanup: impl FnOnce(Ditto) -> R, ) -> Result<R, DittoError>

Log out of Ditto.

Shutdown all replication sessions and remove any cached authentication credentials. This does not remove the local data store.

Source

pub fn is_authenticated(&self) -> bool

Query whether Ditto has a valid authentication token.

This will only be true when using an OnlineWithAuthentication identity, after a successful login. If the authentication token is allowed to expire then it will return false instead.

Source

pub fn user_id(&self) -> Option<String>

The currently logged-in user ID.

This will return None if there is no valid authentication or an OnlineWithAuthentication identity is not being used.

Trait Implementations§

Source§

impl Clone for DittoAuthenticator

Source§

fn clone(&self) -> DittoAuthenticator

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
§

impl<T> CompatExt for T

§

fn compat(self) -> Compat<T>

Applies the [Compat] adapter by value. Read more
§

fn compat_ref(&self) -> Compat<&T>

Applies the [Compat] adapter by shared reference. Read more
§

fn compat_mut(&mut self) -> Compat<&mut T>

Applies the [Compat] adapter by mutable reference. Read more
§

impl<T> FitForCArc for T

§

type CArcWrapped = ThinArc<T>

§

impl<T> FitForCBox for T

§

type CBoxWrapped = ThinBox<T>

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> ManuallyDropMut for T

§

type Ret = ManuallyDrop<T>

§

fn manually_drop_mut<'__>(&'__ mut self) -> &'__ mut ManuallyDrop<T>

§

impl<T> To for T
where T: ?Sized,

§

fn to<T>(self) -> T
where Self: Into<T>,

Converts to T by calling Into<T>::into.
§

fn try_to<T>(self) -> Result<T, Self::Error>
where Self: TryInto<T>,

Tries to convert to T by calling TryInto<T>::try_into.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> ErasedDestructor for T
where T: 'static,

§

impl<F> ZeroSizedElseWrathOfTheGඞds for F