dittolive_ditto/identity/auth/
authenticator.rs1use crate::{
2 ditto::{Ditto, DittoFields},
3 error::{DittoError, ErrorKind},
4 utils::prelude::*,
5};
6
7#[derive(Clone, Debug)]
12pub struct AuthenticationClientFeedback {
13 pub feedback: Option<serde_json::Value>,
15}
16
17impl RefCounted for DittoAuthenticator {}
18
19#[derive(Clone)]
25pub struct DittoAuthenticator {
26 pub(crate) ditto_fields: std::sync::Weak<DittoFields>,
27}
28
29impl DittoAuthenticator {
30 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 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 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 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}