dittolive_ditto/identity/auth/
authenticator.rs

1use crate::{
2    ditto::{Ditto, DittoFields},
3    error::{DittoError, ErrorKind},
4    utils::prelude::*,
5};
6
7/// Feedback provided by an authentication provider.
8///
9/// This feedback may include arbitrary JSON, and may be used to
10/// give details about an authentication success or explain why it was rejected.
11#[derive(Clone, Debug)]
12pub struct AuthenticationClientFeedback {
13    /// If present, this object contains feedback from the authentication provider.
14    pub feedback: Option<serde_json::Value>,
15}
16
17impl RefCounted for DittoAuthenticator {}
18
19/// Use [`ditto.auth()`] to manage authentication when using applicable identities.
20///
21/// The `DittoAuthenticator` is available when using [`DittoConfigConnect::Server`] mode.
22///
23/// [`ditto.auth()`]: crate::Ditto::auth
24#[derive(Clone)]
25pub struct DittoAuthenticator {
26    pub(crate) ditto_fields: std::sync::Weak<DittoFields>,
27}
28
29impl DittoAuthenticator {
30    /// Asks the [`Ditto`] instance to make an auth request to the auth URL
31    /// configured in [`DittoConfigConnect::Server`] with a single token parameter.
32    ///
33    /// - `token`: An auth or API token you have configured.
34    /// - `provider`: The name of an authentication provider web hook you have configured in Ditto
35    ///   Cloud, which will accept the `token` and contact your Auth service.
36    pub fn login(
37        &self,
38        token: &str,
39        provider: &str,
40    ) -> Result<AuthenticationClientFeedback, DittoError> {
41        let fields = self
42            .ditto_fields
43            .upgrade()
44            .ok_or(ErrorKind::ReleasedDittoInstance)?;
45        let c_token = char_p::new(token);
46        let c_provider = char_p::new(provider);
47        let result = ffi_sdk::ditto_auth_client_login_with_token_and_feedback(
48            &fields.ditto,
49            c_token.as_ref(),
50            c_provider.as_ref(),
51        );
52        fn parse_client_info(c: Option<char_p::Box>) -> AuthenticationClientFeedback {
53            AuthenticationClientFeedback {
54                feedback: c.map(|it| serde_json::from_str(it.to_str()).unwrap()),
55            }
56        }
57
58        match result.status_code {
59            0 => Ok(parse_client_info(result.c_string)),
60            _ => Err(DittoError::from_authentication_feedback(parse_client_info(
61                result.c_string,
62            ))),
63        }
64    }
65
66    /// Log out of Ditto.
67    ///
68    /// Shutdown all replication sessions and remove any cached authentication credentials. This
69    /// does *not* remove the local data store.
70    pub fn logout<R>(&self, cleanup: impl FnOnce(Ditto) -> R) -> Result<R, DittoError> {
71        let fields = self
72            .ditto_fields
73            .upgrade()
74            .ok_or(ErrorKind::ReleasedDittoInstance)?;
75        let status = ffi_sdk::ditto_auth_client_logout(&fields.ditto);
76        if status != 0 {
77            return Err(DittoError::from_ffi(ErrorKind::Authentication));
78        }
79        let ditto = Ditto::new_temp(fields);
80        ditto.sync().stop();
81        let ret = cleanup(ditto);
82        Ok(ret)
83    }
84
85    /// Query whether Ditto has a valid authentication token.
86    ///
87    /// This will only be `true` when using [`DittoConfigConnect::Server`] mode, after a
88    /// successful login. If the authentication token is allowed to expire then it will return
89    /// `false` instead.
90    pub fn is_authenticated(&self) -> bool {
91        match self.ditto_fields.upgrade() {
92            None => false,
93            Some(fields) => ffi_sdk::ditto_auth_client_is_web_valid(&fields.ditto) != 0,
94        }
95    }
96
97    /// The currently logged-in user ID.
98    ///
99    /// This will return `None` if there is no valid authentication or
100    /// [`DittoConfigConnect::Server`] mode is not being used.
101    pub fn user_id(&self) -> Option<String> {
102        let fields = self.ditto_fields.upgrade()?;
103        let c_msg = ffi_sdk::ditto_auth_client_user_id(&fields.ditto)?;
104        Some(c_msg.into_string())
105    }
106}