Skip to main content

alibi_plugins/sso/
provider_delete.rs

1//! Retire a provider's identity namespace under the caller's real authority.
2use super::{
3    AuthContext, AuthResult, AuthSchema, EndpointResponse, JsValue, ServerEndpoint, SessionRequest,
4    SsoPlugin, Value, api, json,
5};
6impl SsoPlugin {
7    pub fn delete_sso_provider_endpoint(provider_id: &str) -> AuthResult<ServerEndpoint<Value>> {
8        ServerEndpoint::new("sso", "deleteSSOProvider")
9            .with_body(&json!({"providerId":provider_id}))
10    }
11    pub(super) async fn delete<S: AuthSchema>(
12        &self,
13        input: &JsValue,
14        request: &impl SessionRequest,
15        ctx: &AuthContext<S>,
16    ) -> AuthResult<EndpointResponse> {
17        let provider = self.authorized_provider(input, request, ctx).await?;
18        let guard = self
19            .config
20            .guard_provider_mutation
21            .as_ref()
22            .map(super::SsoMutationGuard::resolve::<S>)
23            .transpose()?;
24        let deleted = if let Some(guard) = guard {
25            let verified = self.config.domain_verification;
26            alibi_core::store::transaction(ctx.database.as_ref(), move |database| {
27                Box::pin(async move {
28                    let existing = database
29                        .lock_sso_provider(&provider.id, &provider.provider_id)
30                        .await?
31                        .ok_or_else(|| api(404, "Provider not found"))?;
32                    super::mutation_guard::run(
33                        guard.as_ref(),
34                        &existing,
35                        super::SsoMutationAction::Delete,
36                        None,
37                        verified,
38                        database,
39                    )
40                    .await?;
41                    database
42                        .delete_sso_provider_and_accounts(&existing.id, &existing.provider_id)
43                        .await
44                })
45            })
46            .await?
47        } else {
48            ctx.database
49                .delete_sso_provider_and_accounts(&provider.id, &provider.provider_id)
50                .await?
51        };
52        if !deleted {
53            return Err(api(404, "Provider not found"));
54        }
55        EndpointResponse::json(&json!({"success":true}))
56    }
57    pub(super) fn validate_delete(input: Option<&JsValue>) -> AuthResult<JsValue> {
58        super::super::endpoint::validate_fields(
59            input,
60            "body",
61            &[super::super::authentication_helpers::JsonField::string(
62                "providerId",
63                true,
64            )],
65        )
66    }
67}