ibridge_auth/credentials.rs
1// SPDX-License-Identifier: Apache-2.0
2
3//! Sign-in validation and the app-specific password.
4
5use ibridge_core::AppleId;
6use secrecy::{ExposeSecret, SecretString};
7
8/// An Apple ID and its app-specific password.
9///
10/// The password is a [`SecretString`]: it is not `Serialize`, and `Debug` prints
11/// a redaction instead of the characters.
12#[derive(Debug)]
13pub struct Credentials {
14 /// The primary iCloud address.
15 pub apple_id: AppleId,
16 secret: SecretString,
17}
18
19impl Credentials {
20 /// Builds credentials. The password is read back only through [`Self::with_secret`].
21 #[must_use]
22 pub fn new(apple_id: AppleId, secret: impl Into<SecretString>) -> Self {
23 Self {
24 apple_id,
25 secret: secret.into(),
26 }
27 }
28
29 /// A second copy, for a second client.
30 #[must_use]
31 pub fn duplicate(&self) -> Self {
32 self.with_secret(|secret| Self::new(self.apple_id.clone(), secret.to_owned()))
33 }
34
35 /// Passes the password to `read` and ends the borrow before returning.
36 pub fn with_secret<T>(&self, read: impl FnOnce(&str) -> T) -> T {
37 read(self.secret.expose_secret())
38 }
39}
40
41/// How a caller proves it may use an iCloud account.
42///
43/// OAuth stays reserved: this enum is non-exhaustive and has no unused variant.
44#[derive(Debug)]
45#[non_exhaustive]
46pub enum AuthMethod {
47 /// An app-specific password for the primary address.
48 AppPassword(Credentials),
49}
50
51/// Why a service was not accepted.
52#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
53#[non_exhaustive]
54pub enum AuthError {
55 /// Validation could not be run.
56 #[error("validation failed")]
57 Validation,
58 /// A credential source could not supply an Apple ID and password.
59 #[error("credentials are unavailable")]
60 Unavailable,
61}
62
63/// Result of checking one iCloud service.
64#[derive(Debug, Clone, PartialEq, Eq)]
65#[non_exhaustive]
66pub enum ServiceCheck {
67 /// The service accepted the credentials.
68 Ok,
69 /// The service rejected them, or this check does not cover the service.
70 Failed {
71 /// A safe reason to show the user. Never a secret.
72 reason: String,
73 },
74 /// The check could not run (network failure, rate limit, outage), so the service
75 /// neither accepted nor refused the credentials.
76 Unreachable {
77 /// A safe reason to show the user. Never a secret.
78 reason: String,
79 },
80}
81
82/// Per-service results for one sign-in attempt.
83///
84/// Sign-in succeeds when [`ValidationReport::any_service_ok`] is true, so an
85/// account with only calendars can sign in.
86#[derive(Debug, Clone, PartialEq, Eq)]
87pub struct ValidationReport {
88 /// IMAP login.
89 pub mail: ServiceCheck,
90 /// `CalDAV` principal lookup.
91 pub calendar: ServiceCheck,
92 /// `CardDAV` principal lookup.
93 pub contacts: ServiceCheck,
94}
95
96impl ValidationReport {
97 /// Returns true when mail, calendar or contacts accepted the credentials.
98 #[must_use]
99 pub fn any_service_ok(&self) -> bool {
100 matches!(self.mail, ServiceCheck::Ok)
101 || matches!(self.calendar, ServiceCheck::Ok)
102 || matches!(self.contacts, ServiceCheck::Ok)
103 }
104}
105
106/// Checks mail, calendar and contacts for a sign-in.
107///
108/// The implementor holds the credentials. Login and the hosted sign-in page
109/// both call this and accept the attempt when any service succeeds.
110#[trait_variant::make(Send)]
111pub trait Validator {
112 /// Runs the checks.
113 ///
114 /// # Errors
115 ///
116 /// Returns [`AuthError`] when validation cannot be run at all.
117 async fn validate(&self) -> Result<ValidationReport, AuthError>;
118}
119
120#[cfg(test)]
121mod tests {
122 use ibridge_core::AppleId;
123
124 use super::Credentials;
125
126 #[test]
127 fn duplicate_carries_the_apple_id_and_password() {
128 let original = Credentials::new(
129 AppleId::try_from("ada@icloud.com").expect("primary address"),
130 "abcd-efgh-ijkl-mnop",
131 );
132 let copy = original.duplicate();
133 assert_eq!(copy.apple_id, original.apple_id);
134 assert_eq!(
135 copy.with_secret(str::to_owned),
136 "abcd-efgh-ijkl-mnop",
137 "the copy signs in as the original does"
138 );
139 }
140}