]> git.mxchange.org Git - friendica.git/blobdiff - src/Security/BasicAuth.php
Issue 13221: Diaspora posts are now stored correctly
[friendica.git] / src / Security / BasicAuth.php
index d4c8bc6dcb7df1a227a192d9471171e924149382..51da7a98ad472429d7f21b0d106b2129b8eb16f9 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2021, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -24,7 +24,6 @@ namespace Friendica\Security;
 use Exception;
 use Friendica\Core\Hook;
 use Friendica\Core\Logger;
-use Friendica\Core\Session;
 use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Model\User;
@@ -32,7 +31,7 @@ use Friendica\Network\HTTPException\UnauthorizedException;
 use Friendica\Util\DateTimeFormat;
 
 /**
- * Authentification via the basic auth method
+ * Authentication via the basic auth method
  */
 class BasicAuth
 {
@@ -78,9 +77,9 @@ class BasicAuth
                        return [];
                }
 
-               if (!empty(self::$current_token)) {
-                       return self::$current_token;
-               }
+               //if (!empty(self::$current_token)) {
+               //      return self::$current_token;
+               //}
 
                $source = $_REQUEST['source'] ?? '';
 
@@ -123,7 +122,6 @@ class BasicAuth
        private static function getUserIdByAuth(bool $do_login = true):int
        {
                $a = DI::app();
-               Session::set('allow_api', false);
                self::$current_user_id = 0;
 
                // workaround for HTTP-auth in CGI mode
@@ -178,7 +176,10 @@ class BasicAuth
                                return 0;
                        }
                        Logger::debug('Access denied', ['parameters' => $_SERVER]);
-                       header('WWW-Authenticate: Basic realm="Friendica"');
+                       // Checking for commandline for the tests, we have to avoid to send a header
+                       if (DI::config()->get('system', 'basicauth') && (php_sapi_name() !== 'cli')) {
+                               header('WWW-Authenticate: Basic realm="Friendica"');
+                       }
                        throw new UnauthorizedException("This API requires login");
                }
 
@@ -187,15 +188,10 @@ class BasicAuth
 
                DI::auth()->setForUser($a, $record, false, false, $login_refresh);
 
-               Session::set('allow_api', true);
-
                Hook::callAll('logged_in', $record);
 
-               if (Session::get('allow_api')) {
-                       self::$current_user_id = local_user();
-               } else {
-                       self::$current_user_id = 0;
-               }
+               self::$current_user_id = DI::userSession()->getLocalUserId();
+
                return self::$current_user_id;
        }
 }