Skip to main content

stackless_integrations/providers/auth0/
client.rs

1//! `auth0/client` integration.
2
3use std::collections::BTreeMap;
4
5use serde::Serialize;
6use stackless_stripe_projects::catalog::verify::CatalogService;
7use stackless_stripe_projects::provision::ProvisionContext;
8
9use super::FamilyResource;
10use crate::error::IntegrationError;
11use crate::hostable::{ConfigScope, Hostable, IntegrationHosting};
12use crate::registry;
13
14pub const RESOURCE_KIND: &str = "integration-auth0";
15
16#[derive(Debug, Serialize)]
17pub struct Auth0ClientConfig {
18    pub name: String,
19}
20
21impl CatalogService for Auth0ClientConfig {
22    const REFERENCE: &'static str = "auth0/client";
23}
24
25#[derive(Debug)]
26pub struct Auth0Client;
27
28impl Hostable for Auth0Client {
29    const PROVIDER: &'static str = "auth0";
30    const HOSTING: IntegrationHosting = IntegrationHosting::Managed;
31    const CONFIG_SCOPE: ConfigScope = ConfigScope::GlobalOnly;
32    const RESOURCE_KIND: &'static str = RESOURCE_KIND;
33    const OUTPUTS: &'static [&'static str] = &["client_id", "client_secret", "domain"];
34}
35
36impl FamilyResource for Auth0Client {
37    type Config = Auth0ClientConfig;
38    const PROVIDER_PREFIX: &'static str = "AUTH0";
39    const OUTPUT_FIELDS: &'static [(&'static str, &'static str, bool)] = &[
40        ("CLIENT_ID", "client_id", true),
41        ("CLIENT_SECRET", "client_secret", true),
42        ("DOMAIN", "domain", true),
43    ];
44
45    fn build_config(ctx: &ProvisionContext<'_>) -> Result<Auth0ClientConfig, IntegrationError> {
46        let config = super::integration_config(ctx)?;
47        Ok(Auth0ClientConfig {
48            name: super::interp_required(ctx, &config, "name")?,
49        })
50    }
51}
52
53pub fn validate_config(
54    name: &str,
55    config: &BTreeMap<String, toml::Value>,
56) -> Result<(), IntegrationError> {
57    registry::config_string(config, "name").map_err(|err| IntegrationError::ConfigInvalid {
58        location: format!("integrations.{name}.name"),
59        detail: err.to_string(),
60    })?;
61    let _ = (name, config);
62    Ok(())
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68    use crate::ProviderOps;
69    use crate::resource::ResourcePayload;
70    use stackless_core::def::StackDef;
71    use stackless_stripe_projects::stripe::StripeProjects;
72    use stackless_stripe_projects::test_support;
73
74    #[test]
75    fn config_matches_catalog() {
76        const FIXTURE: &str = include_str!(concat!(
77            env!("CARGO_MANIFEST_DIR"),
78            "/../stackless-stripe-projects/tests/fixtures/catalog.json"
79        ));
80        let catalog = stackless_stripe_projects::Catalog::from_json_envelope(FIXTURE).unwrap();
81        let failures = stackless_stripe_projects::verify_service(
82            &catalog,
83            &Auth0ClientConfig {
84                name: "test-name".into(),
85            },
86        );
87        assert!(
88            failures.is_empty(),
89            "auth0/client catalog gaps:\n{}",
90            failures.join("\n")
91        );
92    }
93
94    const CATALOG_ENVELOPE: &str = r##"{"ok":true,"command":"projects catalog","data":{"last_updated":"2026-07-11T00:00:00Z","services":[{"id":"prvsvc_client","object":"v2.provisioning.provider_service_detail","provider_id":"prvdr_auth0","provider_name":"Auth0","service_id":"client","categories":["database"],"kind":"deployable","scope":"project","availability":"available","development":false,"livemode":true,"pricing":{"type":"component"},"configuration_schema":{"properties":{"name":{"minLength":1,"type":"string"}},"required":["name"],"type":"object"}}]}}"##;
95
96    fn test_def() -> StackDef {
97        StackDef::parse(
98            r#"
99[stack]
100name = "atto"
101[stack.projects.stripe]
102project = "project_1"
103[integrations.res]
104provider = "auth0"
105name = "test-name"
106[services.api]
107source = { repo = "r", ref = "main" }
108env = { OUT = "${integrations.res.domain}" }
109health = { path = "/health" }
110[services.api.local]
111run = "true"
112"#,
113        )
114        .unwrap()
115    }
116
117    #[tokio::test]
118    async fn provision_records_outputs() {
119        let runner = test_support::provision_script(
120            CATALOG_ENVELOPE,
121            serde_json::json!({"AUTH0_CLIENT_ID": "val_client_id", "AUTH0_CLIENT_SECRET": "val_client_secret", "AUTH0_DOMAIN": "val_domain"}),
122            0,
123        );
124        let dir = tempfile::tempdir().unwrap();
125        std::fs::write(
126            dir.path().join("stackless.toml"),
127            "[stack]\nname=\"atto\"\n",
128        )
129        .unwrap();
130        let stripe = StripeProjects::new(&runner, dir.path());
131
132        let resource = Auth0Client
133            .provision(
134                &stripe.as_dyn(),
135                &test_def(),
136                dir.path(),
137                "demo",
138                "res",
139                "local",
140                false,
141            )
142            .await
143            .unwrap();
144        assert_eq!(resource.resource_kind, "integration-auth0");
145        let payload: ResourcePayload = serde_json::from_str(&resource.payload).unwrap();
146        assert_eq!(payload.outputs["domain"], "val_domain");
147    }
148}