sign_in_with_apple_fixed/
data.rs

1use serde::{Deserialize, Deserializer, Serialize};
2
3pub const APPLE_PUB_KEYS: &str =
4	"https://appleid.apple.com/auth/keys";
5pub const APPLE_ISSUER: &str = "https://appleid.apple.com";
6
7#[derive(Debug, Serialize, Deserialize)]
8pub struct KeyComponents {
9	pub kty: String,   // "RSA"
10	pub kid: String,   // "eXaunmL"
11	pub r#use: String, // "sig"
12	pub alg: String,   // "RS256"
13	pub n: String,     // "4dGQ7bQK8LgILOdL..."
14	pub e: String,     // "AQAB"
15}
16
17#[derive(Debug, PartialEq, Serialize, Deserialize)]
18pub struct Claims {
19	pub iss: String,
20	pub aud: String,
21	pub exp: i32,
22	pub iat: i32,
23	pub sub: String,
24	pub email: Option<String>,
25	pub email_verified: Option<String>,
26	pub auth_time: i32,
27}
28
29/// see <https://developer.apple.com/documentation/sign_in_with_apple/processing_changes_for_sign_in_with_apple_accounts>
30#[derive(Debug, PartialEq, Serialize, Deserialize)]
31pub struct ClaimsServer2Server {
32	pub iss: String,
33	pub aud: String,
34	pub exp: i32,
35	pub iat: i32,
36	pub jti: String,
37	/// Note that this is documented different to how it is sent.
38	/// see https://developer.apple.com/forums/thread/655485
39	#[serde(deserialize_with = "deserialize_events")]
40	pub events: ClaimsServer2ServerEvent,
41}
42
43#[derive(Debug, PartialEq, Serialize, Deserialize)]
44pub struct ClaimsServer2ServerEvent {
45	#[serde(rename = "type")]
46	pub event_type: String,
47	pub sub: String,
48	pub event_time: i64,
49	pub email: Option<String>,
50	pub is_private_email: Option<String>,
51}
52
53// The signature of a deserialize_with function must follow the pattern:
54//
55//    fn deserialize<'de, D>(D) -> Result<T, D::Error>
56//    where
57//        D: Deserializer<'de>
58//
59// although it may also be generic over the output types T.
60pub fn deserialize_events<'de, D>(
61	deserializer: D,
62) -> Result<ClaimsServer2ServerEvent, D::Error>
63where
64	D: Deserializer<'de>,
65{
66	let s = String::deserialize(deserializer)?;
67	let events: ClaimsServer2ServerEvent =
68		serde_json::from_str(s.as_str())
69			.map_err(serde::de::Error::custom)?;
70	Ok(events)
71}