]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Authentication.php
Add search types
[friendica.git] / src / Core / Authentication.php
index 1963d34b4ee89f57565218dc4fb490a8b870bbb2..646729c434014ac37794cdedf1ecc3d36c3e9f63 100644 (file)
@@ -5,11 +5,9 @@
 
 namespace Friendica\Core;
 
+use Friendica\App;
 use Friendica\BaseObject;
-use Friendica\Database\DBA;
-use Friendica\Model\User;
 use Friendica\Util\BaseURL;
-use Friendica\Util\DateTimeFormat;
 
 /**
 * Handle Authentification, Session and Cookies
@@ -56,119 +54,34 @@ class Authentication extends BaseObject
        }
 
        /**
-        * @brief Sets the provided user's authenticated session
-        *
-        * @todo  Should be moved to Friendica\Core\Session once it's created
-        *
-        * @param array $user_record
-        * @param bool  $login_initial
-        * @param bool  $interactive
-        * @param bool  $login_refresh
-        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @brief Kills the "Friendica" cookie and all session data
         */
-       public static function setAuthenticatedSessionForUser($user_record, $login_initial = false, $interactive = false, $login_refresh = false)
+       public static function deleteSession()
        {
-               $a = self::getApp();
-
-               $_SESSION['uid'] = $user_record['uid'];
-               $_SESSION['theme'] = $user_record['theme'];
-               $_SESSION['mobile-theme'] = PConfig::get($user_record['uid'], 'system', 'mobile_theme');
-               $_SESSION['authenticated'] = 1;
-               $_SESSION['page_flags'] = $user_record['page-flags'];
-               $_SESSION['my_url'] = $a->getbaseUrl() . '/profile/' . $user_record['nickname'];
-               $_SESSION['my_address'] = $user_record['nickname'] . '@' . substr($a->getbaseUrl(), strpos($a->getbaseUrl(), '://') + 3);
-               $_SESSION['addr'] = defaults($_SERVER, 'REMOTE_ADDR', '0.0.0.0');
-
-               $a->user = $user_record;
-
-               if ($interactive) {
-                       if ($a->user['login_date'] <= DBA::NULL_DATETIME) {
-                               $_SESSION['return_path'] = 'profile_photo/new';
-                               $a->module = 'profile_photo';
-                               info(L10n::t("Welcome ") . $a->user['username'] . EOL);
-                               info(L10n::t('Please upload a profile photo.') . EOL);
-                       } else {
-                               info(L10n::t("Welcome back ") . $a->user['username'] . EOL);
-                       }
-               }
-
-               $member_since = strtotime($a->user['register_date']);
-               if (time() < ($member_since + ( 60 * 60 * 24 * 14))) {
-                       $_SESSION['new_member'] = true;
-               } else {
-                       $_SESSION['new_member'] = false;
-               }
-               if (strlen($a->user['timezone'])) {
-                       date_default_timezone_set($a->user['timezone']);
-                       $a->timezone = $a->user['timezone'];
-               }
-
-               $masterUid = $user_record['uid'];
-
-               if (!empty($_SESSION['submanage'])) {
-                       $user = DBA::selectFirst('user', ['uid'], ['uid' => $_SESSION['submanage']]);
-                       if (DBA::isResult($user)) {
-                               $masterUid = $user['uid'];
-                       }
-               }
-
-               $a->identities = User::identities($masterUid);
-
-               if ($login_initial) {
-                       Logger::log('auth_identities: ' . print_r($a->identities, true), Logger::DEBUG);
-               }
-               if ($login_refresh) {
-                       Logger::log('auth_identities refresh: ' . print_r($a->identities, true), Logger::DEBUG);
-               }
+               self::setCookie(-3600); // make sure cookie is deleted on browser close, as a security measure
+               session_unset();
+               session_destroy();
+       }
 
-               $contact = DBA::selectFirst('contact', [], ['uid' => $_SESSION['uid'], 'self' => true]);
-               if (DBA::isResult($contact)) {
-                       $a->contact = $contact;
-                       $a->cid = $contact['id'];
-                       $_SESSION['cid'] = $a->cid;
+       public static function twoFactorCheck($uid, App $a)
+       {
+               // Check user setting, if 2FA disabled return
+               if (!PConfig::get($uid, '2fa', 'verified')) {
+                       return;
                }
 
-               header('X-Account-Management-Status: active; name="' . $a->user['username'] . '"; id="' . $a->user['nickname'] . '"');
-
-               if ($login_initial || $login_refresh) {
-                       DBA::update('user', ['login_date' => DateTimeFormat::utcNow()], ['uid' => $_SESSION['uid']]);
-
-                       // Set the login date for all identities of the user
-                       DBA::update('user', ['login_date' => DateTimeFormat::utcNow()],
-                               ['parent-uid' => $masterUid, 'account_removed' => false]);
+               // Check current path, if 2fa authentication module return
+               if ($a->argc > 0 && in_array($a->argv[0], ['ping', '2fa', 'view', 'help', 'api', 'proxy', 'logout'])) {
+                       return;
                }
 
-               if ($login_initial) {
-                       /*
-                        * If the user specified to remember the authentication, then set a cookie
-                        * that expires after one week (the default is when the browser is closed).
-                        * The cookie will be renewed automatically.
-                        * The week ensures that sessions will expire after some inactivity.
-                        */
-                       if (!empty($_SESSION['remember'])) {
-                               Logger::log('Injecting cookie for remembered user ' . $a->user['nickname']);
-                               self::setCookie(604800, $user_record);
-                               unset($_SESSION['remember']);
-                       }
+               // Case 1: 2FA session present and valid: return
+               if (Session::get('2fa')) {
+                       return;
                }
 
-               if ($login_initial) {
-                       Hook::callAll('logged_in', $a->user);
-
-                       if (($a->module !== 'home') && isset($_SESSION['return_path'])) {
-                               $a->internalRedirect($_SESSION['return_path']);
-                       }
-               }
-       }
-
-       /**
-        * @brief Kills the "Friendica" cookie and all session data
-        */
-       public static function deleteSession()
-       {
-               self::setCookie(-3600); // make sure cookie is deleted on browser close, as a security measure
-               session_unset();
-               session_destroy();
+               // Case 2: No valid 2FA session: redirect to code verification page
+               $a->internalRedirect('2fa');
        }
 }