$message])); } if (!file_exists('config.php')) { passkeyEndpointRefuse(503, 'ITFlow is not set up yet.'); } require_once "config.php"; require_once "functions.php"; require_once __DIR__ . "/includes/session_init.php"; if (!isset($config_enable_setup) || $config_enable_setup == 1) { passkeyEndpointRefuse(503, 'ITFlow is not set up yet.'); } if ( $config_https_only && (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] !== 'on') && (!isset($_SERVER['HTTP_X_FORWARDED_PROTO']) || $_SERVER['HTTP_X_FORWARDED_PROTO'] !== 'https') ) { passkeyEndpointRefuse(403, 'Sign in over HTTPS.'); } require_once "includes/inc_set_timezone.php"; managedRequestGuard('login'); if ($_SERVER['REQUEST_METHOD'] !== 'POST') { passkeyEndpointRefuse(405, 'Not here.'); } if (!passkeysOn()) { passkeyEndpointRefuse(403, 'Passkeys are turned off - sign in with your email and password.'); } $session_ip = escapeSql(getIP()); $session_user_agent = escapeSql(mb_substr((string) ($_SERVER['HTTP_USER_AGENT'] ?? ''), 0, 250)); $session_user_id = 0; // The same lockout as the sign-in page, serialized per address the same way (login.php) $login_lock_name = 'itflow_login_ip_' . md5($session_ip); $login_lock = mysqli_fetch_row(mysqli_query($mysqli, "SELECT GET_LOCK('$login_lock_name', 10)")); if (empty($login_lock[0])) { error_log("ITFlow: timed out waiting on the login rate limit lock for $session_ip, proceeding unserialized"); } $failed = mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(log_id) FROM logs WHERE log_ip = '$session_ip' AND log_type IN ('" . implode("', '", SIGN_IN_TYPES) . "') AND log_action IN ('" . implode("', '", SIGN_IN_FAILED) . "') AND log_created_at > (NOW() - INTERVAL " . SIGN_IN_WINDOW_MINUTES . " MINUTE)")); if (intval($failed[0] ?? 0) >= SIGN_IN_LOCKOUT) { logAudit("Login", "Blocked", "Sign in turned away from $session_ip - " . SIGN_IN_LOCKOUT . " failed sign-ins in " . SIGN_IN_WINDOW_MINUTES . " minutes"); passkeyEndpointRefuse(429, 'Too many failed sign-ins from here - try again later.'); } $action = (string) ($_POST['action'] ?? ''); if ($action === 'options') { echo json_encode(passkeySignInOptions()); exit; } if ($action !== 'verify') { passkeyEndpointRefuse(400, 'Not here.'); } [$passkey, $why, $failed_user_id] = passkeyVerifySignIn($_POST); $user = null; if ($passkey) { $user_id = intval($passkey['user_id']); $user = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT users.user_id, user_type, user_email, user_status, user_archived_at, user_service_account, user_specific_encryption_ciphertext, contact_id, contact_client_id, client_archived_at FROM users LEFT JOIN contacts ON contacts.contact_user_id = users.user_id LEFT JOIN clients ON clients.client_id = contacts.contact_client_id WHERE users.user_id = $user_id")); if (!$user || intval($user['user_status']) !== 1 || !empty($user['user_archived_at']) || !empty($user['user_service_account'])) { $why = 'the account is disabled or archived'; $user = null; } elseif (intval($user['user_type']) === 2 && (empty($user['contact_id']) || !empty($user['client_archived_at']))) { $why = 'the account has no contact or client'; $user = null; } $failed_user_id = $user_id; } if (!$user) { $failed_type = 'Login'; $failed_client_id = 0; if ($failed_user_id) { $who = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT user_type, user_email, contact_client_id FROM users LEFT JOIN contacts ON contacts.contact_user_id = users.user_id WHERE users.user_id = " . intval($failed_user_id))); if ($who && intval($who['user_type']) === 2) { $failed_type = 'Client Login'; $failed_client_id = intval($who['contact_client_id']); } $what = 'Failed sign in with a passkey as ' . ($who['user_email'] ?? 'an unknown account') . " - $why"; } else { $what = "Failed sign in with a passkey - $why"; } signInLogFailure($failed_type, 'Failed', escapeSql($what), intval($failed_user_id), $failed_client_id); if (str_starts_with($why, 'the sign-in took too long')) { passkeyEndpointRefuse(401, 'That took too long - try again.'); } if (str_starts_with($why, 'the passkey is not one added here')) { passkeyEndpointRefuse(401, 'That passkey isn\'t set up here - sign in with your email and password, then add it in your account.'); } passkeyEndpointRefuse(401, 'That passkey didn\'t sign you in - try again, or sign in with your email and password.'); } $token = bin2hex(random_bytes(32)); if (intval($user['user_type']) === 1) { // The vault: opened with the passkey's PRF secret when it has a copy and the browser gave one - // otherwise it stays locked until they type their password (vaultUnlock()) or use a passkey that opens it unset($_SESSION['user_encryption_session_ciphertext'], $_SESSION['user_encryption_session_iv'], $_SESSION['passkey_seal']); $agent_vault = false; $prf = passkeyFromB64url($_POST['prf'] ?? ''); if ($passkey['passkey_vault'] !== '' && $prf !== false) { $master = passkeyOpenVault($passkey['passkey_vault'], $prf, $passkey['passkey_credential_hash']); if ($master !== false) { generateUserSessionKey($master); $agent_vault = true; } } // It gave its secret but has no copy that opens with it - added where the browser didn't give the // secret then. Kept until the vault opens this session, which seals a copy to it (passkeySealKept()), // so from the next sign-in it opens the vault itself if (!$agent_vault && $prf !== false && strlen($prf) >= 32 && trim((string) $user['user_specific_encryption_ciphertext']) !== '') { passkeySealKeep($passkey, $prf); } $_SESSION['pending_passkey_login'] = [ 'type' => 'agent', 'email' => $user['user_email'], 'agent_user_id' => intval($user['user_id']), 'passkey_name' => $passkey['passkey_name'], 'agent_vault' => $agent_vault, 'token' => $token, 'created' => time(), ]; } else { $_SESSION['pending_passkey_login'] = [ 'type' => 'client', 'email' => $user['user_email'], 'client_user_id' => intval($user['user_id']), 'passkey_name' => $passkey['passkey_name'], 'token' => $token, 'created' => time(), ]; } unset($_SESSION['pending_dual_login'], $_SESSION['pending_mfa_login']); echo json_encode(['token' => $token]);