use super::auth_normalise_email::normalise_email; use super::funejson::Value; fn decision(action: &str, reason: &str, account_id: Option<&str>, email: Option<&str>) -> LinkDecision { LinkDecision { action: action.to_string(), reason: reason.to_string(), account_id: account_id.map(|s| s.to_string()), email: email.map(|s| s.to_string()), } } /// What to do with an OAuth sign-in, given the accounts and links the app /// already has. The rules run in a fixed order, so the same inputs always get /// the same answer; see the README for the attack each rule stops. /// /// # Panics /// Panics when the identity's provider or subject is empty. pub fn decide_account_link(identity: &OAuthIdentity, accounts: &[LinkAccount], links: &[IdentityLink]) -> LinkDecision { if identity.provider.is_empty() { panic!("identity.provider must be a non-empty string"); } if identity.subject.is_empty() { panic!("identity.subject must be a non-empty string"); } let email = identity.email.as_deref().and_then(normalise_email); let email_ref = email.as_deref(); // The provider's subject is the identity; once linked, the email no longer matters. if let Some(existing) = links.iter().find(|l| l.provider == identity.provider && l.subject == identity.subject) { if accounts.iter().any(|a| a.id == existing.account_id) { return decision("sign-in", "existing_link", Some(&existing.account_id), email_ref); } return decision("refuse", "linked_account_missing", Some(&existing.account_id), email_ref); } let wanted = match email_ref { Some(e) => e, None => return decision("refuse", "no_email", None, None), }; let matches: Vec<&LinkAccount> = accounts .iter() .filter(|a| normalise_email(&a.email).as_deref() == Some(wanted)) .collect(); if matches.is_empty() { if identity.email_verified { return decision("create", "new_account", None, email_ref); } return decision("require-verification", "unverified_new_email", None, email_ref); } if matches.len() > 1 { return decision("refuse", "ambiguous_email", None, email_ref); } let account = matches[0]; if !identity.email_verified { return decision("refuse", "unverified_email", Some(&account.id), email_ref); } if links .iter() .any(|l| l.account_id == account.id && l.provider == identity.provider && l.subject != identity.subject) { return decision("refuse", "provider_already_linked", Some(&account.id), email_ref); } if !account.email_verified { return decision("require-verification", "account_email_unverified", Some(&account.id), email_ref); } decision("link", "verified_email_match", Some(&account.id), email_ref) } fn text(value: &Value, what: &str) -> String { match value { Value::Str(s) => s.clone(), _ => panic!("{} must be a string", what), } } fn flag(value: &Value, what: &str) -> bool { match value { Value::Bool(b) => *b, _ => panic!("{} must be true or false", what), } } fn list<'a>(value: &'a Value, what: &str) -> &'a [Value] { match value { Value::Arr(items) => items, _ => panic!("{} must be a list", what), } } pub fn link_decision_to_value(d: &LinkDecision) -> Value { let opt = |o: &Option| match o { Some(s) => Value::str(s), None => Value::Null, }; Value::obj(vec![ ("action", Value::str(&d.action)), ("reason", Value::str(&d.reason)), ("accountId", opt(&d.account_id)), ("email", opt(&d.email)), ]) } pub fn fune_vector(args: &[Value]) -> Value { let id = &args[0]; let identity = OAuthIdentity { provider: text(id.get("provider"), "identity.provider"), subject: text(id.get("subject"), "identity.subject"), email: match id.get("email") { Value::Null => None, other => Some(text(other, "identity.email")), }, email_verified: flag(id.get("emailVerified"), "identity.emailVerified"), }; let accounts: Vec = list(&args[1], "accounts") .iter() .map(|a| LinkAccount { id: text(a.get("id"), "account.id"), email: text(a.get("email"), "account.email"), email_verified: flag(a.get("emailVerified"), "account.emailVerified"), }) .collect(); let links: Vec = list(&args[2], "links") .iter() .map(|l| IdentityLink { provider: text(l.get("provider"), "link.provider"), subject: text(l.get("subject"), "link.subject"), account_id: text(l.get("accountId"), "link.accountId"), }) .collect(); link_decision_to_value(&decide_account_link(&identity, &accounts, &links)) }