get('accounts', 'password', ['username' => $username]); return (comparePassword($password, $hash)); } function user_exists($username) { return $database->has('accounts', ['username' => strtolower($username)]); } //////////////////////////////////////////////////////////////////////////////// // 2-factor authentication // //////////////////////////////////////////////////////////////////////////////// /** * Check if a user has TOTP setup * @global $database $database * @param string $username * @return boolean true if TOTP secret exists, else false */ function userHasTOTP($username) { global $database; $username = strtolower($username); $secret = $database->get('accounts', 'authsecret', ['username' => $username]); if (is_empty($secret)) { return false; } return true; } /** * Verify a TOTP multiauth code * @global $database * @param string $username * @param int $code * @return boolean true if it's legit, else false */ function verifyTOTP($username, $code) { global $database; $username = strtolower($username); $authsecret = $database->get('accounts', 'authsecret', ['username' => $username]); if (is_empty($authsecret)) { return false; } $totp = new TOTP(null, $authsecret); return $totp->verify($code); }