Sign in with Apple
POST /v1/auth/apple verifies the ID token from Sign in with Apple JS against Apple's keys, the Services IDs in APPLE_CLIENT_IDS and Apple's issuer, then logs in like Google: the linked identity, else the user with the same verified address, else a new user (invitations and referrals as for Google). The Google verifier became a shared ID token verifier. Apple gives the name only at the first sign-in; the page sends it along (family name first). The login page shows Apple's white button under Google's when it is configured; the privacy pages say what Apple passes on, including the relay addresses. The deploy compose's MAIL_FROM default is noreply@ like production (the sender registered for Apple's e-mail relay). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -195,6 +195,18 @@ fn google_token(sub: &str, email: &str, audience: &str) -> String {
|
||||
jsonwebtoken::encode(&header, &claims, &jsonwebtoken::EncodingKey::from_rsa_pem(GOOGLE_KEY_PEM).unwrap()).unwrap()
|
||||
}
|
||||
|
||||
fn apple_token(sub: &str, email: &str, audience: &str) -> String {
|
||||
let now = time::OffsetDateTime::now_utc().unix_timestamp();
|
||||
// Apple sends email_verified as a string and no name.
|
||||
let claims = json!({
|
||||
"iss": "https://appleid.apple.com", "aud": audience, "sub": sub, "email": email,
|
||||
"email_verified": "true", "is_private_email": "true", "iat": now, "exp": now + 600
|
||||
});
|
||||
let mut header = jsonwebtoken::Header::new(jsonwebtoken::Algorithm::RS256);
|
||||
header.kid = Some("test-key".into());
|
||||
jsonwebtoken::encode(&header, &claims, &jsonwebtoken::EncodingKey::from_rsa_pem(GOOGLE_KEY_PEM).unwrap()).unwrap()
|
||||
}
|
||||
|
||||
// --- test client -------------------------------------------------------------------------------
|
||||
|
||||
struct TestApp {
|
||||
@@ -851,7 +863,7 @@ async fn export_and_delete_account(pool: PgPool) {
|
||||
/// config and catalog, shared links, invitation pages, the waitlist and
|
||||
/// unsubscribing. Every other route (charts, calculations, analyses) needs a
|
||||
/// logged-in user, so other apps cannot use them.
|
||||
const PUBLIC_ROUTES: [&str; 16] = [
|
||||
const PUBLIC_ROUTES: [&str; 17] = [
|
||||
"/healthz",
|
||||
// Stripe calls it; the signature proves it.
|
||||
"/v1/payments/stripe/webhook",
|
||||
@@ -860,6 +872,7 @@ const PUBLIC_ROUTES: [&str; 16] = [
|
||||
"/v1/auth/register",
|
||||
"/v1/auth/login",
|
||||
"/v1/auth/google",
|
||||
"/v1/auth/apple",
|
||||
"/v1/auth/verify-email",
|
||||
"/v1/auth/password-reset",
|
||||
"/v1/auth/password-reset/confirm",
|
||||
@@ -2673,3 +2686,59 @@ async fn tarot_readings_draw_on_the_server_and_use_tarot_units(pool: PgPool) {
|
||||
assert_eq!(r.status, StatusCode::NO_CONTENT, "{}", r.body);
|
||||
assert_eq!(count().await, 0);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "db::MIGRATOR")]
|
||||
async fn apple_login_creates_links_and_rejects(pool: PgPool) {
|
||||
let app = TestApp::new(pool).await;
|
||||
let r = app.get("/v1/config", None).await;
|
||||
assert_eq!(r.body["apple_login"], json!({"enabled": true, "client_id": "hu.sorsfonalak.test"}));
|
||||
let relay = "x7k2p9@privaterelay.appleid.com";
|
||||
// The name comes from the page (Apple gives it at the first sign-in only).
|
||||
let r = app
|
||||
.post(
|
||||
"/v1/auth/apple",
|
||||
None,
|
||||
json!({"id_token": apple_token("a-1", relay, "hu.sorsfonalak.test"), "name": "Kovács Anna"}),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(r.status, StatusCode::CREATED, "{}", r.body);
|
||||
assert_eq!((&r.body["created"], &r.body["user"]["display_name"]), (&json!(true), &json!("Kovács Anna")));
|
||||
assert_eq!(r.body["user"]["email"], relay);
|
||||
let r =
|
||||
app.post("/v1/auth/apple", None, json!({"id_token": apple_token("a-1", relay, "hu.sorsfonalak.test")})).await;
|
||||
assert_eq!((r.status, &r.body["created"]), (StatusCode::OK, &json!(false)));
|
||||
assert_eq!(r.body["user"]["display_name"], "Kovács Anna");
|
||||
|
||||
// the same address as a Google account: both log into one user
|
||||
let google = app
|
||||
.post("/v1/auth/google", None, json!({"id_token": google_token("g-9", "bela@example.com", GOOGLE_CLIENT)}))
|
||||
.await;
|
||||
let token = google.body["session"]["token"].as_str().unwrap().to_string();
|
||||
let r = app
|
||||
.post(
|
||||
"/v1/auth/apple",
|
||||
None,
|
||||
json!({"id_token": apple_token("a-2", "bela@example.com", "hu.sorsfonalak.test")}),
|
||||
)
|
||||
.await;
|
||||
assert_eq!((r.status, &r.body["created"]), (StatusCode::OK, &json!(false)));
|
||||
assert_eq!(app.get("/v1/me", Some(&token)).await.body["providers"], json!(["google", "apple"]));
|
||||
|
||||
// a Google token is no Apple token, nor one for another Services ID
|
||||
let r = app
|
||||
.post("/v1/auth/apple", None, json!({"id_token": google_token("g-1", "gabor@gmail.com", GOOGLE_CLIENT)}))
|
||||
.await;
|
||||
assert_eq!(r.status, StatusCode::UNAUTHORIZED);
|
||||
let r = app.post("/v1/auth/apple", None, json!({"id_token": apple_token("a-3", relay, "other.service")})).await;
|
||||
assert_eq!(r.status, StatusCode::UNAUTHORIZED);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "db::MIGRATOR")]
|
||||
async fn apple_login_can_be_off(pool: PgPool) {
|
||||
let app = TestApp::with_config(pool, |c| c.apple_client_ids.clear()).await;
|
||||
assert_eq!(app.get("/v1/config", None).await.body["apple_login"]["enabled"], false);
|
||||
let r = app
|
||||
.post("/v1/auth/apple", None, json!({"id_token": apple_token("a-1", "a@b.hu", "hu.sorsfonalak.test")}))
|
||||
.await;
|
||||
assert_eq!(r.status, StatusCode::FORBIDDEN);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user