Skip to main content

junobuild_auth/state/
store.rs

1use crate::errors::{
2    JUNO_AUTH_ERROR_AUTOMATION_NOT_CONFIGURED, JUNO_AUTH_ERROR_NOT_CONFIGURED,
3    JUNO_AUTH_ERROR_OPENID_DISABLED,
4};
5use crate::state::asserts::{assert_set_authentication_config, assert_set_automation_config};
6use crate::state::heap::{get_automation, get_config};
7use crate::state::heap::{insert_automation, insert_config};
8use crate::state::types::automation::{AutomationConfig, OpenIdAutomationProviders};
9use crate::state::types::config::{AuthenticationConfig, OpenIdAuthProviders};
10use crate::state::types::interface::{SetAuthenticationConfig, SetAutomationConfig};
11use crate::state::{get_salt, insert_salt};
12use crate::strategies::AuthHeapStrategy;
13use junobuild_shared::ic::api::print;
14use junobuild_shared::random::raw_rand;
15
16pub fn set_authentication_config(
17    auth_heap: &impl AuthHeapStrategy,
18    proposed_config: &SetAuthenticationConfig,
19) -> Result<AuthenticationConfig, String> {
20    let current_config = get_config(auth_heap);
21
22    assert_set_authentication_config(proposed_config, &current_config)?;
23
24    let config = AuthenticationConfig::prepare(&current_config, proposed_config);
25
26    insert_config(auth_heap, &config);
27
28    Ok(config)
29}
30
31pub fn set_automation_config(
32    auth_heap: &impl AuthHeapStrategy,
33    proposed_config: &SetAutomationConfig,
34) -> Result<AutomationConfig, String> {
35    let current_config = get_automation(auth_heap);
36
37    assert_set_automation_config(proposed_config, &current_config)?;
38
39    let config = AutomationConfig::prepare(&current_config, proposed_config);
40
41    insert_automation(auth_heap, &config);
42
43    Ok(config)
44}
45
46pub async fn init_salt(auth_heap: &impl AuthHeapStrategy) -> Result<(), String> {
47    let existing_salt = get_salt(auth_heap);
48
49    // Salt should be initialized only once.
50    if existing_salt.is_some() {
51        #[allow(clippy::disallowed_methods)]
52        print("Authentication salt exists. Skipping initialization.");
53        return Ok(());
54    }
55
56    let salt = raw_rand()
57        .await
58        .map_err(|e| format!("Failed to obtain authentication seed: {:?}", e))?;
59
60    insert_salt(auth_heap, &salt);
61
62    #[allow(clippy::disallowed_methods)]
63    print("Authentication salt initialized.");
64
65    Ok(())
66}
67
68pub fn get_auth_providers(
69    auth_heap: &impl AuthHeapStrategy,
70) -> Result<OpenIdAuthProviders, String> {
71    let config = get_config(auth_heap).ok_or(JUNO_AUTH_ERROR_NOT_CONFIGURED.to_string())?;
72    let openid = config
73        .openid
74        .ok_or(JUNO_AUTH_ERROR_OPENID_DISABLED.to_string())?;
75
76    Ok(openid.providers.clone())
77}
78
79pub fn get_automation_providers(
80    auth_heap: &impl AuthHeapStrategy,
81) -> Result<OpenIdAutomationProviders, String> {
82    let config =
83        get_automation(auth_heap).ok_or(JUNO_AUTH_ERROR_AUTOMATION_NOT_CONFIGURED.to_string())?;
84    let openid = config
85        .openid
86        .ok_or(JUNO_AUTH_ERROR_OPENID_DISABLED.to_string())?;
87
88    Ok(openid.providers.clone())
89}