liveactivity 0.16.0

Framework-neutral Live Activities: one interface over ActivityKit, Android Live Updates and other backends
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
//! The sender of the feature `server`: it signs in to APNs with the team's key, sends a message's request over
//! HTTP/2, and says in one vocabulary what came back.

mod fcm;

pub use fcm::{Fcm, FcmAccount};

use super::{Application, Message, Platform, Request, Target, apns::valid_bundle_id};
use crate::{Error, Result};
use jsonwebtoken::{Algorithm, EncodingKey, Header, crypto::CryptoProvider};
use serde::Serialize;
use std::{
    fmt,
    sync::{Arc, Mutex, PoisonError},
    time::{Duration, SystemTime, UNIX_EPOCH},
};

const PRODUCTION: &str = "https://api.push.apple.com";
const SANDBOX: &str = "https://api.sandbox.push.apple.com";
/// How long a provider token is used. APNs takes one for an hour and refuses a new one more often than every 20
/// minutes.
const TOKEN_LIFETIME: Duration = Duration::from_secs(50 * 60);
/// The longest key id and team id taken: Apple's are ten characters.
const MAX_ID: usize = 64;
/// How long one request may take unless the builder says otherwise.
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(30);
/// How long the sender's own client waits for a connection.
const CONNECT_TIMEOUT: Duration = Duration::from_secs(10);

/// A team's APNs key: the `.p8` file's PEM text, its key id and the team's id. Its text never shows: `Debug` names
/// the two ids alone, and jsonwebtoken wipes its copy of the key when it is dropped. The PEM that the caller read
/// stays the caller's to drop.
#[derive(Clone)]
pub struct ApnsKey {
    encoding: EncodingKey,
    key_id: String,
    team_id: String,
}

impl ApnsKey {
    /// The key of a `.p8` file, signed with once to know that it signs. A key that does not, or an id that is not
    /// 1 to 64 letters and digits, is `INVALID_INPUT`; the message never holds the key's text.
    pub fn from_pem(
        pem: impl AsRef<[u8]>,
        key_id: impl Into<String>,
        team_id: impl Into<String>,
    ) -> Result<Self> {
        let refused = || Error::new("INVALID_INPUT", "The APNs key is not a P-256 key in PEM.");
        let (key_id, team_id) = (key_id.into(), team_id.into());
        if !valid_id(&key_id) || !valid_id(&team_id) {
            return Err(Error::new(
                "INVALID_INPUT",
                "The key id and the team id are 1 to 64 letters and digits.",
            ));
        }
        let encoding = EncodingKey::from_ec_pem(pem.as_ref()).map_err(|_| refused())?;
        let key = Self {
            encoding,
            key_id,
            team_id,
        };
        key.sign().map_err(|_| refused())?;
        Ok(key)
    }

    /// A provider token made now.
    fn sign(&self) -> std::result::Result<String, jsonwebtoken::errors::Error> {
        self.sign_at(SystemTime::now())
    }

    /// A provider token made at `at`, which is its `iat`.
    fn sign_at(&self, at: SystemTime) -> std::result::Result<String, jsonwebtoken::errors::Error> {
        #[derive(Serialize)]
        struct Claims<'a> {
            iss: &'a str,
            iat: u64,
        }
        crypto_provider();
        // The header APNs documents: the algorithm and the key's id.
        let mut header = Header::new(Algorithm::ES256);
        header.typ = None;
        header.kid = Some(self.key_id.clone());
        let iat = at
            .duration_since(UNIX_EPOCH)
            .map_or(0, |since| since.as_secs());
        jsonwebtoken::encode(
            &header,
            &Claims {
                iss: &self.team_id,
                iat,
            },
            &self.encoding,
        )
    }
}

impl fmt::Debug for ApnsKey {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("ApnsKey")
            .field("key_id", &self.key_id)
            .field("team_id", &self.team_id)
            .finish_non_exhaustive()
    }
}

/// What the sender needs of APNs: the key, the application's bundle id and the host, production unless
/// [`sandbox`](Self::sandbox) or [`host`](Self::host) says otherwise.
#[derive(Debug, Clone)]
pub struct Apns {
    key: ApnsKey,
    bundle_id: String,
    host: String,
}

impl Apns {
    /// APNs's production host, which an application from the App Store or TestFlight gets its tokens from.
    pub fn new(key: ApnsKey, bundle_id: impl Into<String>) -> Self {
        Self {
            key,
            bundle_id: bundle_id.into(),
            host: PRODUCTION.into(),
        }
    }

    /// The sandbox, which a development build gets its tokens from.
    pub fn sandbox(self) -> Self {
        self.host(SANDBOX)
    }

    /// Another host, such as a test server: a URL without a path.
    pub fn host(self, url: impl Into<String>) -> Self {
        Self {
            host: url.into().trim_end_matches('/').to_owned(),
            ..self
        }
    }

    /// The host the requests go to.
    pub fn host_url(&self) -> &str {
        &self.host
    }
}

/// What a platform said when it took a push.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Delivery {
    /// The platform's id of the push: APNs's `apns-id`.
    pub id: Option<String>,
}

/// Why a push was not delivered, in one vocabulary for every platform. None holds a key.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum SendError {
    /// The message, or its target, is not one that the device or the sender takes: nothing was sent.
    #[error("the push was not sent: {0}")]
    Invalid(Error),
    /// The target is gone: the application was removed, or the activity ended. Forget the target.
    #[error("the target is no longer registered")]
    Unregistered,
    /// The target is not one the platform takes for this application: a token of another bundle id, or of the other
    /// host, since APNs refuses a development build's token in production the same way. Check the host before
    /// forgetting the target.
    #[error("the platform does not take the target: {reason}")]
    BadTarget {
        /// The platform's reason.
        reason: String,
    },
    /// Too many pushes: send again later, after `retry_after` when the platform says.
    #[error("too many pushes")]
    TooManyRequests {
        /// How long the platform asked to wait.
        retry_after: Option<Duration>,
    },
    /// The platform refused the push for another reason.
    #[error("the platform refused the push ({status}): {reason}")]
    Rejected {
        /// The HTTP status.
        status: u16,
        /// The platform's reason, empty without one.
        reason: String,
    },
    /// The platform could not be reached, or was unavailable: send again later.
    #[error("the platform is unavailable: {reason}")]
    Unavailable {
        /// The HTTP status, none when no answer came.
        status: Option<u16>,
        /// What happened.
        reason: String,
    },
}

/// Sends messages to the platforms it was given; its clones share the client and the sign-in.
#[derive(Clone)]
pub struct Sender {
    shared: Arc<Shared>,
}

struct Shared {
    client: reqwest::Client,
    timeout: Duration,
    apns: Option<ApnsSender>,
    fcm: Option<fcm::FcmSender>,
}

struct ApnsSender {
    apns: Apns,
    /// The provider token and the wall-clock time it was made at, which APNs reads as its `iat`.
    token: Mutex<Option<(String, SystemTime)>>,
}

impl fmt::Debug for Sender {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("Sender")
            .field("apns", &self.shared.apns.as_ref().map(|apns| &apns.apns))
            .field("fcm", &self.shared.fcm.as_ref().map(fcm::FcmSender::fcm))
            .field("timeout", &self.shared.timeout)
            .finish_non_exhaustive()
    }
}

/// Builds a [`Sender`]: each platform optional, at least one.
#[must_use]
#[derive(Debug, Default)]
pub struct SenderBuilder {
    apns: Option<Apns>,
    fcm: Option<Fcm>,
    client: Option<reqwest::Client>,
    timeout: Option<Duration>,
}

impl SenderBuilder {
    /// Sends to APNs with this key.
    pub fn apns(self, apns: Apns) -> Self {
        Self {
            apns: Some(apns),
            ..self
        }
    }

    /// Sends to FCM with this service account.
    pub fn fcm(self, fcm: Fcm) -> Self {
        Self {
            fcm: Some(fcm),
            ..self
        }
    }

    /// The service's own HTTP client; otherwise the sender makes one, with HTTP/2 and rustls.
    pub fn client(self, client: reqwest::Client) -> Self {
        Self {
            client: Some(client),
            ..self
        }
    }

    /// How long one request may take: 30 seconds unless set.
    pub fn timeout(self, timeout: Duration) -> Self {
        Self {
            timeout: Some(timeout),
            ..self
        }
    }

    /// The sender, or `INVALID_INPUT` without a platform, with a bundle id that is none, or with a host that is
    /// not an `https` or `http` URL.
    pub fn build(self) -> Result<Sender> {
        if self.apns.is_none() && self.fcm.is_none() {
            return Err(Error::new(
                "INVALID_INPUT",
                "A sender needs a platform to send to: give it APNs or FCM.",
            ));
        }
        if let Some(apns) = &self.apns {
            if !valid_bundle_id(&apns.bundle_id) {
                return Err(Error::new(
                    "INVALID_INPUT",
                    "The bundle id is letters, digits, `.` and `-`.",
                ));
            }
            if !web_host(&apns.host) {
                return Err(Error::new(
                    "INVALID_INPUT",
                    "The APNs host is an https URL without a path.",
                ));
            }
        }
        if let Some(fcm) = &self.fcm
            && !web_host(fcm.host_url())
        {
            return Err(Error::new(
                "INVALID_INPUT",
                "The FCM host is an https URL without a path.",
            ));
        }
        let client = match self.client {
            Some(client) => client,
            None => reqwest::Client::builder()
                .connect_timeout(CONNECT_TIMEOUT)
                .build()
                .map_err(|_| Error::native())?,
        };
        Ok(Sender {
            shared: Arc::new(Shared {
                client,
                timeout: self.timeout.unwrap_or(DEFAULT_TIMEOUT),
                apns: self.apns.map(|apns| ApnsSender {
                    apns,
                    token: Mutex::new(None),
                }),
                fcm: self.fcm.map(fcm::FcmSender::new),
            }),
        })
    }
}

impl Sender {
    /// A builder with no platform yet.
    pub fn builder() -> SenderBuilder {
        SenderBuilder::default()
    }

    /// Sends `message` to `target`, or says why it was not delivered. A message that the device would refuse, and
    /// a target of a platform that the sender was not given, are [`SendError::Invalid`] and send nothing.
    pub async fn send(
        &self,
        message: &Message,
        target: &Target,
    ) -> std::result::Result<Delivery, SendError> {
        match target.platform() {
            Platform::Apns => {
                let Some(apns) = &self.shared.apns else {
                    return Err(SendError::Invalid(Error::unsupported_feature(
                        "This sender was not given APNs.",
                    )));
                };
                let application = Application::new().bundle_id(apns.apns.bundle_id.clone());
                let request = message
                    .request(target, &application)
                    .map_err(SendError::Invalid)?;
                self.send_apns(apns, &request).await
            }
            Platform::Fcm => {
                let Some(fcm) = &self.shared.fcm else {
                    return Err(SendError::Invalid(Error::unsupported_feature(
                        "This sender was not given FCM.",
                    )));
                };
                let application = Application::new().fcm_project(fcm.project());
                let request = message
                    .request(target, &application)
                    .map_err(SendError::Invalid)?;
                fcm.send(&self.shared.client, self.shared.timeout, &request)
                    .await
            }
        }
    }

    async fn send_apns(
        &self,
        apns: &ApnsSender,
        request: &Request,
    ) -> std::result::Result<Delivery, SendError> {
        let (first, token) = self.post(apns, request, None).await?;
        // A token that APNs says has expired is made again, once: the clock of the server may have drifted, or the
        // process slept. Pushes that failed with the same token make one new token between them.
        let answer = if first.status == 403 && first.reason == "ExpiredProviderToken" {
            self.post(apns, request, Some(&token)).await?.0
        } else {
            first
        };
        answer.outcome()
    }

    /// One request with the provider token, and the token it was sent with. `expired` is a token that APNs said
    /// has expired: the sender makes a new one unless another push already has.
    async fn post(
        &self,
        apns: &ApnsSender,
        request: &Request,
        expired: Option<&str>,
    ) -> std::result::Result<(Answer, String), SendError> {
        let token = apns.token(expired)?;
        let mut post = self
            .shared
            .client
            .post(format!("{}{}", apns.apns.host, request.path))
            .header(reqwest::header::AUTHORIZATION, bearer("bearer", &token)?)
            .timeout(self.shared.timeout)
            .body(request.body.clone());
        for (name, value) in &request.headers {
            post = post.header(*name, value);
        }
        let response = post.send().await.map_err(unreachable)?;
        let status = response.status().as_u16();
        let header = |name: &str| {
            response
                .headers()
                .get(name)
                .and_then(|value| value.to_str().ok())
                .map(str::to_owned)
        };
        // A broadcast's answer names its request by `apns-request-id`.
        let id = header("apns-id").or_else(|| header("apns-request-id"));
        let retry_after = header("retry-after")
            .and_then(|seconds| seconds.trim().parse().ok())
            .map(Duration::from_secs);
        let text = response.text().await.unwrap_or_default();
        let reason = serde_json::from_str::<serde_json::Value>(&text)
            .ok()
            .and_then(|body| body.get("reason")?.as_str().map(str::to_owned))
            .unwrap_or_default();
        Ok((
            Answer {
                status,
                id,
                retry_after,
                reason,
            },
            token,
        ))
    }
}

impl ApnsSender {
    /// The provider token: made once, then again [`TOKEN_LIFETIME`] after its `iat` by the wall clock, which APNs
    /// reads, or when it is the token that APNs said has `expired`.
    fn token(&self, expired: Option<&str>) -> std::result::Result<String, SendError> {
        let mut held = self.token.lock().unwrap_or_else(PoisonError::into_inner);
        let now = SystemTime::now();
        if let Some((token, made)) = held.as_ref()
            && expired != Some(token.as_str())
            && now
                .duration_since(*made)
                .is_ok_and(|age| age < TOKEN_LIFETIME)
        {
            return Ok(token.clone());
        }
        let token = self.apns.key.sign_at(now).map_err(|_| {
            SendError::Invalid(Error::new(
                "INVALID_INPUT",
                "The APNs key could not sign a provider token.",
            ))
        })?;
        *held = Some((token.clone(), now));
        Ok(token)
    }
}

/// One answer of a push service.
struct Answer {
    status: u16,
    id: Option<String>,
    retry_after: Option<Duration>,
    reason: String,
}

impl Answer {
    fn outcome(self) -> std::result::Result<Delivery, SendError> {
        match self.status {
            200 => Ok(Delivery { id: self.id }),
            410 => Err(SendError::Unregistered),
            400 if matches!(
                self.reason.as_str(),
                "BadDeviceToken" | "DeviceTokenNotForTopic" | "MissingDeviceToken"
            ) =>
            {
                Err(SendError::BadTarget {
                    reason: self.reason,
                })
            }
            429 => Err(SendError::TooManyRequests {
                retry_after: self.retry_after,
            }),
            500..=599 => Err(SendError::Unavailable {
                status: Some(self.status),
                reason: self.reason,
            }),
            status => Err(SendError::Rejected {
                status,
                reason: self.reason,
            }),
        }
    }
}

/// Signs with aws-lc-rs where the process has no provider of jsonwebtoken yet: a service whose own jsonwebtoken
/// turns on the other provider leaves the choice to the process, and jsonwebtoken would panic without one.
fn crypto_provider() {
    if CryptoProvider::try_get_default().is_none() {
        // Another thread may have installed one in between: either is a provider.
        let _ = jsonwebtoken::crypto::aws_lc::DEFAULT_PROVIDER.install_default();
    }
}

/// A key id or a team id: letters and digits.
fn valid_id(id: &str) -> bool {
    !id.is_empty() && id.len() <= MAX_ID && id.bytes().all(|byte| byte.is_ascii_alphanumeric())
}

/// Whether `url` is a host the sender can send to: an `https` or `http` URL without a path.
fn web_host(url: &str) -> bool {
    reqwest::Url::parse(url).is_ok_and(|url| {
        matches!(url.scheme(), "https" | "http") && url.has_host() && url.path() == "/"
    })
}

/// What a request that got no answer says.
fn unreachable(error: reqwest::Error) -> SendError {
    if error.is_builder() {
        SendError::Invalid(Error::new(
            "INVALID_INPUT",
            "The request could not be made: check the host.",
        ))
    } else {
        SendError::Unavailable {
            status: None,
            reason: if error.is_timeout() {
                "the request timed out".into()
            } else {
                "the host could not be reached".into()
            },
        }
    }
}

/// A bearer token as a header value that a dump of the request at trace level does not show.
fn bearer(
    scheme: &str,
    token: &str,
) -> std::result::Result<reqwest::header::HeaderValue, SendError> {
    let mut value = reqwest::header::HeaderValue::from_str(&format!("{scheme} {token}"))
        .map_err(|_| SendError::Invalid(Error::native()))?;
    value.set_sensitive(true);
    Ok(value)
}

/// Whether `url` is an `https` or `http` URL with a host, such as a service account's `token_uri`.
fn web_host_or_path(url: &str) -> bool {
    reqwest::Url::parse(url)
        .is_ok_and(|url| matches!(url.scheme(), "https" | "http") && url.has_host())
}