3 * @file /src/Core/Authentication.php
6 namespace Friendica\Core;
9 use Friendica\BaseObject;
10 use Friendica\Util\BaseURL;
13 * Handle Authentification, Session and Cookies
15 class Authentication extends BaseObject
18 * @brief Calculate the hash that is needed for the "Friendica" cookie
20 * @param array $user Record from "user" table
22 * @return string Hashed data
23 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
25 public static function getCookieHashForUser($user)
27 return(hash("sha256", Config::get("system", "site_prvkey") .
33 * @brief Set the "Friendica" cookie
36 * @param array $user Record from "user" table
37 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
39 public static function setCookie($time, $user = [])
42 $time = $time + time();
46 $value = json_encode(["uid" => $user["uid"],
47 "hash" => self::getCookieHashForUser($user),
48 "ip" => defaults($_SERVER, 'REMOTE_ADDR', '0.0.0.0')]);
53 setcookie("Friendica", $value, $time, "/", "", (Config::get('system', 'ssl_policy') == BaseUrl::SSL_POLICY_FULL), true);
57 * @brief Kills the "Friendica" cookie and all session data
59 public static function deleteSession()
61 self::setCookie(-3600); // make sure cookie is deleted on browser close, as a security measure
66 public static function twoFactorCheck($uid, App $a)
68 // Check user setting, if 2FA disabled return
69 if (!PConfig::get($uid, '2fa', 'verified')) {
73 // Check current path, if 2fa authentication module return
74 if ($a->argc > 0 && in_array($a->argv[0], ['ping', '2fa', 'view', 'help', 'logout'])) {
78 // Case 1: 2FA session present and valid: return
79 if (Session::get('2fa')) {
83 // Case 2: No valid 2FA session: redirect to code verification page
84 $a->internalRedirect('2fa');