Skip to main content

matrix_ui_serializable/init/
mod.rs

1use anyhow::anyhow;
2use matrix_sdk::{Client, authentication::oauth::error::OAuthDiscoveryError};
3use serde::Serialize;
4
5use crate::init::singletons::TEMP_CLIENT;
6
7pub(crate) mod login;
8pub(crate) mod oauth;
9pub(crate) mod session;
10pub mod singletons;
11mod sync;
12pub(crate) mod workers;
13
14#[derive(Debug, Serialize)]
15#[serde(rename_all = "camelCase", rename_all_fields = "camelCase")]
16pub enum FrontendAuthTypeResponse {
17    Matrix,
18    Oauth,
19    WrongUrl,
20}
21
22pub async fn check_homeserver_auth_type() -> anyhow::Result<(FrontendAuthTypeResponse, Client)> {
23    let client = {
24        let mut guard = TEMP_CLIENT.lock.lock().unwrap();
25        while guard.is_none() {
26            guard = TEMP_CLIENT.cvar.wait(guard).unwrap();
27        }
28        guard.clone().ok_or(anyhow!("No temp client set yet"))?
29    };
30    match client.oauth().server_metadata().await {
31        Ok(_) => Ok((FrontendAuthTypeResponse::Oauth, client)),
32        Err(e) => match e {
33            OAuthDiscoveryError::NotSupported => Ok((FrontendAuthTypeResponse::Matrix, client)),
34            OAuthDiscoveryError::Url(_) => Ok((FrontendAuthTypeResponse::WrongUrl, client)),
35            _ => Err(anyhow!("Unknown error when checking available auth types")),
36        },
37    }
38}