From 9c59bcb6a5ce6e9000dd502969360026f8d363bc Mon Sep 17 00:00:00 2001
From: Michael <heluecht@pirati.ca>
Date: Wed, 2 Jun 2021 22:11:21 +0000
Subject: [PATCH] Support OAuth for the legacy API

---
 include/api.php        | 6 ++++++
 src/Module/BaseApi.php | 5 ++++-
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/include/api.php b/include/api.php
index 8e89895de..935bd11d6 100644
--- a/include/api.php
+++ b/include/api.php
@@ -44,6 +44,7 @@ use Friendica\Model\Photo;
 use Friendica\Model\Post;
 use Friendica\Model\User;
 use Friendica\Model\Verb;
+use Friendica\Module\BaseApi;
 use Friendica\Network\HTTPException;
 use Friendica\Network\HTTPException\BadRequestException;
 use Friendica\Network\HTTPException\ExpectationFailedException;
@@ -88,6 +89,11 @@ $called_api = [];
  */
 function api_user()
 {
+	$user = BaseApi::getCurrentUserID(true);
+	if (!empty($user)) {
+		return $user;
+	}
+
 	if (!empty($_SESSION['allow_api'])) {
 		return local_user();
 	}
diff --git a/src/Module/BaseApi.php b/src/Module/BaseApi.php
index 7735b9802..015324569 100644
--- a/src/Module/BaseApi.php
+++ b/src/Module/BaseApi.php
@@ -238,7 +238,7 @@ class BaseApi extends BaseModule
 	 *
 	 * @return int User ID
 	 */
-	protected static function getCurrentUserID()
+	public static function getCurrentUserID(bool $nologin = false)
 	{
 		if (empty(self::$current_user_id)) {
 			self::$current_token = self::getTokenByBearer();
@@ -247,7 +247,10 @@ class BaseApi extends BaseModule
 			} else {
 				self::$current_user_id = 0;
 			}
+		}
 
+		if ($nologin) {
+			return (int)self::$current_user_id;
 		}
 
 		if (empty(self::$current_user_id)) {