dittolive_ditto/identity/auth/
login_provider.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use std::{
    os::raw::{c_uint, c_void},
    sync::Arc,
    time::Duration,
};

use extern_c::extern_c;
use ffi_sdk::BoxedLoginProvider;

use crate::{identity::DittoAuthenticator, utils::prelude::*};

/// Implement this trait for a type in order to construct an
/// [`OnlineWithAuthentication`] identity.
// You can find an example in tests::common::mod
pub trait DittoAuthenticationEventHandler: Send + Sync {
    /// This will be called when you need to authenticate.
    /// Usually it will involve a call to `auth.login_with_token`
    fn authentication_required(&self, auth: DittoAuthenticator);

    /// Allows for custom behavior hooks when authentication is expiring
    fn authentication_expiring_soon(&self, auth: DittoAuthenticator, seconds_remaining: Duration);
}

pub(crate) struct LoginProvider(pub(crate) BoxedLoginProvider);

// this inner type allows us to get a reference/ptr before the outer type is
// constructed
pub(crate) struct LoginProviderCtx {
    // The user-provided auth logic (the `.authentication_…()` callbacks above).
    // Also called an `auth_delegate` in other SDKs.
    auth_event_handler: Arc<dyn DittoAuthenticationEventHandler + 'static>,
    // A fancy wrapper around a back-reference to Ditto.
    authenticator: DittoAuthenticator,
}

impl LoginProvider {
    pub fn new(
        auth_event_handler: Arc<dyn 'static + DittoAuthenticationEventHandler>,
        authenticator: DittoAuthenticator,
    ) -> Self {
        let login_provider_ctx = Arc::new(LoginProviderCtx {
            auth_event_handler,
            authenticator,
        });

        let c_provider = unsafe {
            ffi_sdk::ditto_auth_client_make_login_provider(
                // legacy non-preincremented convention.
                Arc::as_ptr(&login_provider_ctx) as *mut c_void,
                Some(extern_c(|ctx: *mut c_void| {
                    Arc::<LoginProviderCtx>::increment_strong_count(ctx.cast())
                })),
                Some(extern_c(|ctx: *mut c_void| {
                    Arc::<LoginProviderCtx>::decrement_strong_count(ctx.cast())
                })),
                extern_c(|ctx: *mut c_void, secs_remaining: c_uint| {
                    let LoginProviderCtx {
                        auth_event_handler,
                        authenticator,
                    }: &LoginProviderCtx = &*ctx.cast();
                    let authenticator = authenticator.retain();
                    if let Some(duration) =
                        (secs_remaining != 0).then(|| Duration::from_secs(secs_remaining.into()))
                    {
                        auth_event_handler.authentication_expiring_soon(authenticator, duration);
                    } else {
                        auth_event_handler.authentication_required(authenticator);
                    }
                }),
            )
        };
        LoginProvider(c_provider)
    }
}