]> git.mxchange.org Git - friendica.git/blob - src/Core/Authentication.php
Merge pull request #7434 from annando/contact-protocol
[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 use Friendica\Util\BaseURL;
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("sha256", Config::get("system", "site_prvkey") .
29                                 $user["prvkey"] .
30                                 $user["password"]));
31         }
32
33         /**
34          * @brief Set the "Friendica" cookie
35          *
36          * @param int   $time
37          * @param array $user Record from "user" table
38          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
39          */
40         public static  function setCookie($time, $user = [])
41         {
42                 if ($time != 0) {
43                         $time = $time + time();
44                 }
45
46                 if ($user) {
47                         $value = json_encode(["uid" => $user["uid"],
48                                 "hash" => self::getCookieHashForUser($user),
49                                 "ip" => defaults($_SERVER, 'REMOTE_ADDR', '0.0.0.0')]);
50                 } else {
51                         $value = "";
52                 }
53
54                 setcookie("Friendica", $value, $time, "/", "", (Config::get('system', 'ssl_policy') == BaseUrl::SSL_POLICY_FULL), true);
55         }
56
57         /**
58          * @brief Kills the "Friendica" cookie and all session data
59          */
60         public static function deleteSession()
61         {
62                 self::setCookie(-3600); // make sure cookie is deleted on browser close, as a security measure
63                 session_unset();
64                 session_destroy();
65         }
66
67         public static function twoFactorCheck($uid, App $a)
68         {
69                 // Check user setting, if 2FA disabled return
70                 if (!PConfig::get($uid, '2fa', 'verified')) {
71                         return;
72                 }
73
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'])) {
76                         return;
77                 }
78
79                 // Case 1: 2FA session present and valid: return
80                 if (Session::get('2fa')) {
81                         return;
82                 }
83
84                 // Case 2: No valid 2FA session: redirect to code verification page
85                 if ($a->isAjax()) {
86                         throw new ForbiddenException();
87                 } else {
88                         $a->internalRedirect('2fa');
89                 }
90         }
91 }
92