Skip to main content

smart_id_rust_client/
config.rs

1use crate::error::Result;
2use crate::error::SmartIdClientError;
3use crate::models::common::SchemeName;
4use std::env;
5use std::str::FromStr;
6
7/// Smart ID Client Configuration
8///
9/// This struct holds the configuration details required to interact with the Smart ID service.
10/// It includes the root URL, API path, relying party UUID, relying party name, and an optional client request timeout.
11///
12/// This can be loaded from environment variables using the `load_from_env` method.
13///
14/// # Properties
15///
16/// * `root_url` - The base URL of the Smart ID service, e.g., `https://sid.sk.ee`.
17/// * `api_path` - The API path to be appended to the root URL, e.g., `/v3`.
18/// * `scheme_version` - The scheme/environment to use, e.g smart-id (production) or smart-id-demo (demo).
19/// * `relying_party_uuid` - The UUID of the relying party, obtained from Smart ID.
20/// * `relying_party_name` - The name of the relying party, obtained from Smart ID.
21/// * `client_request_timeout` - An optional timeout for client requests, in milliseconds. This is not used for the long-polling status request.
22#[derive(Debug, Clone)]
23pub struct SmartIDConfig {
24    pub root_url: String,
25    pub api_path: String,
26    pub scheme_name: SchemeName,
27    pub relying_party_uuid: String,
28    pub relying_party_name: String,
29    pub client_request_timeout: Option<u64>,
30    pub long_polling_timeout: u64,
31}
32
33impl SmartIDConfig {
34    /// Loads the Smart ID configuration from environment variables.
35    ///
36    /// # Returns
37    ///
38    /// * `Ok(SmartIDConfig)` - If all required environment variables are present and valid.
39    /// * `Err(anyhow::Error)` - If any required environment variable is missing or invalid.
40    pub fn load_from_env() -> Result<SmartIDConfig> {
41        Ok(SmartIDConfig {
42            root_url: get_env("SMART_ID_ROOT_URL")?,
43            api_path: get_env("SMART_ID_V3_API_PATH")?,
44            scheme_name: SchemeName::from_str(&get_env("SMART_ID_SCHEME_NAME")?)
45                .map_err(|_| SmartIdClientError::ConfigMissingException("SMART_ID_SCHEME_NAME"))?,
46            relying_party_uuid: get_env("RELYING_PARTY_UUID")?,
47            relying_party_name: get_env("RELYING_PARTY_NAME")?,
48            client_request_timeout: get_env_u64("CLIENT_REQ_NETWORK_TIMEOUT_MILLIS").ok(),
49            long_polling_timeout: get_env_u64("CLIENT_LONG_POLLING_TIMEOUT_MILLIS")
50                .unwrap_or(120000),
51        })
52    }
53
54    /// Constructs the full API URL using the root URL and API path.
55    ///
56    /// # Returns
57    ///
58    /// * `String` - The full API URL.
59    pub fn api_url(&self) -> String {
60        format!("{}{}", self.root_url, self.api_path)
61    }
62
63    pub(crate) fn is_demo(&self) -> bool {
64        self.root_url == "https://sid.demo.sk.ee"
65    }
66}
67
68fn get_env(name: &'static str) -> Result<String> {
69    env::var(name).map_err(|_| SmartIdClientError::ConfigMissingException(name))
70}
71
72fn get_env_u64(name: &'static str) -> Result<u64> {
73    env::var(name)
74        .map_err(|_| SmartIdClientError::ConfigMissingException(name))
75        .and_then(|val| {
76            val.parse()
77                .map_err(|_| SmartIdClientError::ConfigMissingException(name))
78        })
79}