4 * @file /src/Core/Authentication.php
7 namespace Friendica\Core;
10 use Friendica\BaseObject;
11 use Friendica\Network\HTTPException\ForbiddenException;
14 * Handle Authentification, Session and Cookies
16 class Authentication extends BaseObject
19 * @brief Calculate the hash that is needed for the "Friendica" cookie
21 * @param array $user Record from "user" table
23 * @return string Hashed data
24 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
26 public static function getCookieHashForUser($user)
30 hash_hmac("sha256", $user["password"], $user["prvkey"]),
31 Config::get("system", "site_prvkey")
36 * @brief Set the "Friendica" cookie
39 * @param array $user Record from "user" table
40 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
42 public static function setCookie($time, $user = [])
45 $time = $time + time();
49 $value = json_encode([
50 "uid" => $user["uid"],
51 "hash" => self::getCookieHashForUser($user),
52 "ip" => ($_SERVER['REMOTE_ADDR'] ?? '') ?: '0.0.0.0'
58 setcookie("Friendica", $value, $time, "/", "", (Config::get('system', 'ssl_policy') == App\BaseURL::SSL_POLICY_FULL), true);
62 * @brief Kills the "Friendica" cookie and all session data
64 public static function deleteSession()
66 self::setCookie(-3600); // make sure cookie is deleted on browser close, as a security measure
71 public static function twoFactorCheck($uid, App $a)
73 // Check user setting, if 2FA disabled return
74 if (!PConfig::get($uid, '2fa', 'verified')) {
78 // Check current path, if 2fa authentication module return
79 if ($a->argc > 0 && in_array($a->argv[0], ['2fa', 'view', 'help', 'api', 'proxy', 'logout'])) {
83 // Case 1: 2FA session present and valid: return
84 if (Session::get('2fa')) {
88 // Case 2: No valid 2FA session: redirect to code verification page
90 throw new ForbiddenException();
92 $a->internalRedirect('2fa');