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