3 * @file /src/Core/Authentication.php
6 namespace Friendica\Core;
9 use Friendica\BaseObject;
10 use Friendica\Network\HTTPException\ForbiddenException;
11 use Friendica\Util\BaseURL;
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)
28 return(hash("sha256", Config::get("system", "site_prvkey") .
34 * @brief Set the "Friendica" cookie
37 * @param array $user Record from "user" table
38 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
40 public static function setCookie($time, $user = [])
43 $time = $time + time();
47 $value = json_encode(["uid" => $user["uid"],
48 "hash" => self::getCookieHashForUser($user),
49 "ip" => defaults($_SERVER, 'REMOTE_ADDR', '0.0.0.0')]);
54 setcookie("Friendica", $value, $time, "/", "", (Config::get('system', 'ssl_policy') == BaseUrl::SSL_POLICY_FULL), true);
58 * @brief Kills the "Friendica" cookie and all session data
60 public static function deleteSession()
62 self::setCookie(-3600); // make sure cookie is deleted on browser close, as a security measure
67 public static function twoFactorCheck($uid, App $a)
69 // Check user setting, if 2FA disabled return
70 if (!PConfig::get($uid, '2fa', 'verified')) {
74 // Check current path, if 2fa authentication module return
75 if ($a->argc > 0 && in_array($a->argv[0], ['2fa', 'view', 'help', 'api', 'proxy', 'logout'])) {
79 // Case 1: 2FA session present and valid: return
80 if (Session::get('2fa')) {
84 // Case 2: No valid 2FA session: redirect to code verification page
86 throw new ForbiddenException();
88 $a->internalRedirect('2fa');