Skip to main content

autoendpoint/routers/apns/
settings.rs

1use std::collections::HashMap;
2
3/// Settings for `ApnsRouter`
4#[derive(Clone, Debug, serde::Deserialize)]
5#[serde(default)]
6//#[serde(deny_unknown_fields)] // Allow unknown fields so we can add comments to the secrets.
7pub struct ApnsSettings {
8    /// A JSON dict of `ApnsChannel`s. This must be a `String` because
9    /// environment variables cannot encode a `HashMap<String, ApnsChannel>`
10    pub channels: String,
11    /// The max size of notification data in bytes
12    pub max_data: usize,
13    // These values correspond to the a2 library ClientConfig struct.
14    // https://github.com/WalletConnect/a2/blob/master/src/client.rs#L65-L71.
15    // Utilized by apns router config in creating the client.
16    pub request_timeout_secs: Option<u64>,
17    pub pool_idle_timeout_secs: Option<u64>,
18}
19
20/// Settings for a specific APNS release channel
21///
22/// Two authentication modes are supported:
23/// - Token-based auth (preferred): set `key` to the APNS provider auth key
24///   (`.p8`), and set both `key_id` and `team_id`. `cert` is unused.
25/// - Certificate-based auth: set `cert` and `key` to the provider
26///   certificate/key pair, and leave `key_id`/`team_id` unset.
27#[derive(Clone, Debug, Default, serde::Deserialize)]
28#[serde(default)]
29//#[serde(deny_unknown_fields)] // Allow unknown fields so we can add comments to the secrets.
30pub struct ApnsChannel {
31    /// the cert and key are either paths
32    /// or an inline value that starts with "-"
33    /// e.g. `-----BEGIN PRIVATE KEY-----\n`
34    pub cert: String,
35    pub key: String,
36    /// The 10-character Apple key ID for the `.p8` auth key (token-based auth)
37    pub key_id: Option<String>,
38    /// The 10-character Apple Developer team ID (token-based auth)
39    pub team_id: Option<String>,
40    pub topic: Option<String>,
41    pub sandbox: bool,
42}
43
44impl Default for ApnsSettings {
45    fn default() -> ApnsSettings {
46        ApnsSettings {
47            channels: "{}".to_string(),
48            max_data: 4096,
49            request_timeout_secs: Some(20),
50            pool_idle_timeout_secs: Some(600),
51        }
52    }
53}
54
55impl ApnsSettings {
56    /// Read the channels from the JSON string
57    pub fn channels(&self) -> serde_json::Result<HashMap<String, ApnsChannel>> {
58        serde_json::from_str(&self.channels)
59    }
60}