Skip to main content

alibi_sqlx/store/
sso.rs

1use super::{SqlxStore, entities::sso_provider::Model};
2use crate::{
3    model::{self, ActiveRow, SqlxModel},
4    schema::AuthSchema,
5};
6use alibi_core::{
7    AuthResult,
8    store::{SsoProvider, SsoProviderStore},
9};
10use async_trait::async_trait;
11#[async_trait]
12impl<S: AuthSchema> SsoProviderStore for SqlxStore<S> {
13    async fn list_sso_providers(&self) -> AuthResult<Vec<SsoProvider>> {
14        let mut sql = model::select_model::<Model>(self.exec());
15        sql.push(" LIMIT ");
16        sql.bind(self.find_many_limit());
17        self.exec()
18            .fetch_all::<Model>(sql)
19            .await
20            .map(|rows| rows.into_iter().map(Into::into).collect())
21    }
22    async fn create_sso_provider(&self, row: SsoProvider) -> AuthResult<SsoProvider> {
23        let mut active = ActiveRow::new();
24        active.set("id", row.id);
25        active.set("issuer", row.issuer);
26        active.set("domain", row.domain);
27        active.set("providerId", row.provider_id);
28        active.set("userId", row.user_id);
29        active.set("organizationId", row.organization_id);
30        active.set("oidcConfig", row.oidc_config);
31        active.set("samlConfig", row.saml_config);
32        active.set("domainVerified", row.domain_verified);
33        model::insert::<Model>(self.exec(), &active)
34            .await
35            .map(Into::into)
36    }
37    async fn get_sso_provider(&self, provider_id: &str) -> AuthResult<Option<SsoProvider>> {
38        let mut sql = model::select_model::<Model>(self.exec());
39        sql.push(" WHERE ");
40        sql.compare(Model::TABLE, "providerId", " = ", provider_id);
41        self.exec()
42            .fetch_optional::<Model>(sql)
43            .await
44            .map(|row| row.map(Into::into))
45    }
46    async fn list_sso_providers_by_user(&self, user_id: &str) -> AuthResult<Vec<SsoProvider>> {
47        let mut sql = model::select_model::<Model>(self.exec());
48        sql.push(" WHERE ");
49        sql.compare(Model::TABLE, "userId", " = ", user_id);
50        sql.push(" LIMIT ");
51        sql.bind(self.find_many_limit());
52        self.exec()
53            .fetch_all::<Model>(sql)
54            .await
55            .map(|rows| rows.into_iter().map(Into::into).collect())
56    }
57}