]> git.mxchange.org Git - friendica.git/blob - src/Core/Authentication.php
Relocated functions
[friendica.git] / src / Core / Authentication.php
1 <?php
2
3 /**
4  * @file /src/Core/Authentication.php
5  */
6
7 namespace Friendica\Core;
8
9 use Friendica\App;
10 use Friendica\BaseObject;
11 use Friendica\Network\HTTPException\ForbiddenException;
12
13 /**
14  * Handle Authentification, Session and Cookies
15  */
16 class Authentication extends BaseObject
17 {
18         /**
19          * @brief Calculate the hash that is needed for the "Friendica" cookie
20          *
21          * @param array $user Record from "user" table
22          *
23          * @return string Hashed data
24          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
25          */
26         public static function getCookieHashForUser($user)
27         {
28                 return hash_hmac(
29                         "sha256",
30                         hash_hmac("sha256", $user["password"], $user["prvkey"]),
31                         Config::get("system", "site_prvkey")
32                 );
33         }
34
35         /**
36          * @brief Set the "Friendica" cookie
37          *
38          * @param int   $time
39          * @param array $user Record from "user" table
40          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
41          */
42         public static  function setCookie($time, $user = [])
43         {
44                 if ($time != 0) {
45                         $time = $time + time();
46                 }
47
48                 if ($user) {
49                         $value = json_encode([
50                                 "uid" => $user["uid"],
51                                 "hash" => self::getCookieHashForUser($user),
52                                 "ip" => ($_SERVER['REMOTE_ADDR'] ?? '') ?: '0.0.0.0'
53                         ]);
54                 } else {
55                         $value = "";
56                 }
57
58                 setcookie("Friendica", $value, $time, "/", "", (Config::get('system', 'ssl_policy') == App\BaseURL::SSL_POLICY_FULL), true);
59         }
60
61         /**
62          * @brief Kills the "Friendica" cookie and all session data
63          */
64         public static function deleteSession()
65         {
66                 self::setCookie(-3600); // make sure cookie is deleted on browser close, as a security measure
67                 session_unset();
68                 session_destroy();
69         }
70
71         public static function twoFactorCheck($uid, App $a)
72         {
73                 // Check user setting, if 2FA disabled return
74                 if (!PConfig::get($uid, '2fa', 'verified')) {
75                         return;
76                 }
77
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'])) {
80                         return;
81                 }
82
83                 // Case 1: 2FA session present and valid: return
84                 if (Session::get('2fa')) {
85                         return;
86                 }
87
88                 // Case 2: No valid 2FA session: redirect to code verification page
89                 if ($a->isAjax()) {
90                         throw new ForbiddenException();
91                 } else {
92                         $a->internalRedirect('2fa');
93                 }
94         }
95 }