From 6a2ca1a6b6b9d0597f60eda9d444509734cc2c76 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Wed, 11 Oct 2023 09:24:13 -0400 Subject: [PATCH] Move jsonError out of Factory\Api\Mastodon\Error->Unauthorized --- src/Factory/Api/Mastodon/Error.php | 7 ++----- src/Module/Api/Mastodon/Apps/VerifyCredentials.php | 2 +- src/Module/OAuth/Revoke.php | 4 ++-- src/Module/OAuth/Token.php | 10 +++++----- 4 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/Factory/Api/Mastodon/Error.php b/src/Factory/Api/Mastodon/Error.php index 1dad4d9c8e..6614100642 100644 --- a/src/Factory/Api/Mastodon/Error.php +++ b/src/Factory/Api/Mastodon/Error.php @@ -64,13 +64,10 @@ class Error extends BaseFactory return new \Friendica\Object\Api\Mastodon\Error($error, $error_description); } - public function Unauthorized(string $error = '', string $error_description = '') + public function Unauthorized(string $error = '', string $error_description = ''): \Friendica\Object\Api\Mastodon\Error { $error = $error ?: $this->l10n->t('Unauthorized'); - $errorObj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description); - - $this->logError(401, $error); - $this->jsonError(401, $errorObj->toArray()); + return new \Friendica\Object\Api\Mastodon\Error($error, $error_description); } public function Forbidden(string $error = '') diff --git a/src/Module/Api/Mastodon/Apps/VerifyCredentials.php b/src/Module/Api/Mastodon/Apps/VerifyCredentials.php index 82e0016ef4..69d470306a 100644 --- a/src/Module/Api/Mastodon/Apps/VerifyCredentials.php +++ b/src/Module/Api/Mastodon/Apps/VerifyCredentials.php @@ -36,7 +36,7 @@ class VerifyCredentials extends BaseApi $application = self::getCurrentApplication(); if (empty($application['id'])) { - DI::mstdnError()->Unauthorized(); + $this->logErrorAndJsonExit(401, $this->errorFactory->Unauthorized()); } $this->jsonExit(DI::mstdnApplication()->createFromApplicationId($application['id'])); diff --git a/src/Module/OAuth/Revoke.php b/src/Module/OAuth/Revoke.php index cde4c36c63..9791144348 100644 --- a/src/Module/OAuth/Revoke.php +++ b/src/Module/OAuth/Revoke.php @@ -50,8 +50,8 @@ class Revoke extends BaseApi $condition = ['client_id' => $request['client_id'], 'client_secret' => $request['client_secret'], 'access_token' => $request['token']]; $token = DBA::selectFirst('application-view', ['id'], $condition); if (empty($token['id'])) { - Logger::notice('Token not found', $condition); - DI::mstdnError()->Unauthorized(); + $this->logger->notice('Token not found', $condition); + $this->logErrorAndJsonExit(401, $this->errorFactory->Unauthorized()); } DBA::delete('application-token', ['application-id' => $token['id']]); diff --git a/src/Module/OAuth/Token.php b/src/Module/OAuth/Token.php index ab9cd3ec26..0186f28519 100644 --- a/src/Module/OAuth/Token.php +++ b/src/Module/OAuth/Token.php @@ -74,13 +74,13 @@ class Token extends BaseApi } if (empty($request['client_id']) || empty($request['client_secret'])) { - Logger::warning('Incomplete request data', ['request' => $request]); - DI::mstdnError()->Unauthorized('invalid_client', DI::l10n()->t('Incomplete request data')); + $this->logger->warning('Incomplete request data', ['request' => $request]); + $this->logErrorAndJsonExit(401, $this->errorFactory->Unauthorized('invalid_client', $this->t('Incomplete request data')));; } $application = OAuth::getApplication($request['client_id'], $request['client_secret'], $request['redirect_uri']); if (empty($application)) { - DI::mstdnError()->Unauthorized('invalid_client', DI::l10n()->t('Invalid data or unknown client')); + $this->logErrorAndJsonExit(401, $this->errorFactory->Unauthorized('invalid_client', $this->t('Invalid data or unknown client'))); } if ($request['grant_type'] == 'client_credentials') { @@ -98,8 +98,8 @@ class Token extends BaseApi $token = DBA::selectFirst('application-view', ['access_token', 'created_at', 'uid'], $condition); if (!DBA::isResult($token)) { - Logger::notice('Token not found or outdated', $condition); - DI::mstdnError()->Unauthorized(); + $this->logger->notice('Token not found or outdated', $condition); + $this->logErrorAndJsonExit(401, $this->errorFactory->Unauthorized()); } $owner = User::getOwnerDataById($token['uid']); $me = $owner['url'];