#[cfg(feature = "google-services")]
pub mod google_auth;
#[cfg(feature = "google-services")]
pub mod rtdb;
#[cfg(feature = "google-services")]
pub mod service_interface;
pub enum ExternalService {
Rtdb,
}
#[cfg(all(feature = "google-services", not(target_family = "wasm")))]
#[derive(Clone)]
pub struct ServicesHandler {
pub google_auth: Option<crate::external_services::google_auth::GoogleAuth>,
pub rtdb_root_instance: Option<crate::external_services::rtdb::RtdbInstance>,
pub rtdb_config: Option<RtdbConfig>,
}
#[derive(Clone)]
#[cfg(not(feature = "google-services"))]
pub struct ServicesHandler;
#[cfg(all(feature = "google-services", not(target_family = "wasm")))]
#[derive(serde::Deserialize, Debug, Default, Clone)]
pub struct ServicesConfig {
pub google_services_json_path: Option<String>,
pub google_rtdb: Option<RtdbConfig>,
}
#[cfg(not(feature = "google-services"))]
#[derive(Default)]
pub struct ServicesConfig;
#[derive(Default, serde::Serialize, serde::Deserialize, Clone, Debug)]
pub struct ServicesObject {
pub google_auth_jwt: Option<JsonWebToken>,
pub rtdb: Option<RtdbConfig>,
}
#[derive(serde::Deserialize, serde::Serialize, Default, Debug, Clone)]
pub struct RtdbConfig {
pub url: String,
pub api_key: String,
}
#[cfg(all(feature = "google-services", not(target_family = "wasm")))]
pub mod service {
use crate::external_services::ServicesObject;
use crate::misc::AccountError;
use firebase_rtdb::FirebaseRTDB;
use std::path::Path;
impl crate::external_services::ServicesHandler {
pub async fn on_post_login_serverside(
&self,
implicated_cid: u64,
) -> Result<ServicesObject, AccountError> {
let mut ret: ServicesObject = Default::default();
if let Some(auth) = self.google_auth.as_ref() {
ret.google_auth_jwt = Some(auth.sign_new_custom_jwt_auth(implicated_cid)?)
}
ret.rtdb = self.rtdb_config.clone();
Ok(ret)
}
}
impl crate::external_services::ServicesConfig {
pub async fn into_services_handler(
self,
) -> Result<crate::external_services::ServicesHandler, AccountError> {
let (google_auth, rtdb_root_instance) = if let Some(path) =
self.google_services_json_path
{
let path = Path::new(&path);
let auth =
crate::external_services::google_auth::GoogleAuth::load_from_google_services_file(
path,
)
.await?;
let rtdb_root_instance = if let Some(rtdb_config) = self.google_rtdb.as_ref() {
let root_jwt = auth.sign_new_custom_jwt_auth("root")?;
let rtdb_root_instance = FirebaseRTDB::new_from_jwt(
&rtdb_config.url,
root_jwt,
&rtdb_config.api_key,
)
.await
.map_err(|err| AccountError::Generic(err.inner))?;
Some(rtdb_root_instance.into())
} else {
None
};
(Some(auth), rtdb_root_instance)
} else {
(None, None)
};
let rtdb_config = self.google_rtdb;
Ok(crate::external_services::ServicesHandler {
google_auth,
rtdb_config,
rtdb_root_instance,
})
}
}
}
pub type JsonWebToken = String;