Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

1.8 KB
Raw Blame
use crate::{FusionAuthClient, Result, UnauthedClient, error::FusionAuthClientError};
use std::borrow::Cow;

#[derive(serde::Serialize, serde::Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct LogoutRequest<'a> {
    /// The fusionauth client id
    pub client_id: Cow<'a, str>,
    /// The fusionauth tenant id
    pub tenant_id: Cow<'a, str>,
}

async fn logout(client: &UnauthedClient, base_url: &str, request: LogoutRequest<'_>) -> Result<()> {
    let res = client
        .client()
        .post(format!("{base_url}/oauth2/logout"))
        .json(&request)
        .send()
        .await
        .map_err(FusionAuthClientError::transport)?;

    let status_code = res.status();
    match status_code {
        reqwest::StatusCode::OK => {
            tracing::trace!("user logged out");
            Ok(())
        }
        _ => {
            let headers = res.headers().clone();
            let body = res.text().await.map_err(FusionAuthClientError::transport)?;

            if status_code == reqwest::StatusCode::NOT_FOUND {
                return Err(FusionAuthClientError::UserDoesNotExist);
            }

            Err(FusionAuthClientError::unexpected(
                status_code,
                &headers,
                body,
            ))
        }
    }
}

impl FusionAuthClient {
    /// Logs out the user for the given tenant.
    #[tracing::instrument(skip(self), fields(application_id=%self.client_id, fusion_auth_base_url=%self.fusion_auth_base_url))]
    pub async fn logout(&self, tenant_id: &str) -> Result<()> {
        logout(
            &self.unauth_client,
            &self.fusion_auth_base_url,
            LogoutRequest {
                client_id: Cow::Borrowed(&self.client_id),
                tenant_id: Cow::Borrowed(tenant_id),
            },
        )
        .await
    }
}