Move jsonError out of Factory\Api\Mastodon\Error->Unauthorized

This commit is contained in:
Hypolite Petovan 2023-10-11 09:24:13 -04:00
parent 7486ebdc10
commit 6a2ca1a6b6
4 changed files with 10 additions and 13 deletions

View File

@ -64,13 +64,10 @@ class Error extends BaseFactory
return new \Friendica\Object\Api\Mastodon\Error($error, $error_description); 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'); $error = $error ?: $this->l10n->t('Unauthorized');
$errorObj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description); return new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
$this->logError(401, $error);
$this->jsonError(401, $errorObj->toArray());
} }
public function Forbidden(string $error = '') public function Forbidden(string $error = '')

View File

@ -36,7 +36,7 @@ class VerifyCredentials extends BaseApi
$application = self::getCurrentApplication(); $application = self::getCurrentApplication();
if (empty($application['id'])) { if (empty($application['id'])) {
DI::mstdnError()->Unauthorized(); $this->logErrorAndJsonExit(401, $this->errorFactory->Unauthorized());
} }
$this->jsonExit(DI::mstdnApplication()->createFromApplicationId($application['id'])); $this->jsonExit(DI::mstdnApplication()->createFromApplicationId($application['id']));

View File

@ -50,8 +50,8 @@ class Revoke extends BaseApi
$condition = ['client_id' => $request['client_id'], 'client_secret' => $request['client_secret'], 'access_token' => $request['token']]; $condition = ['client_id' => $request['client_id'], 'client_secret' => $request['client_secret'], 'access_token' => $request['token']];
$token = DBA::selectFirst('application-view', ['id'], $condition); $token = DBA::selectFirst('application-view', ['id'], $condition);
if (empty($token['id'])) { if (empty($token['id'])) {
Logger::notice('Token not found', $condition); $this->logger->notice('Token not found', $condition);
DI::mstdnError()->Unauthorized(); $this->logErrorAndJsonExit(401, $this->errorFactory->Unauthorized());
} }
DBA::delete('application-token', ['application-id' => $token['id']]); DBA::delete('application-token', ['application-id' => $token['id']]);

View File

@ -74,13 +74,13 @@ class Token extends BaseApi
} }
if (empty($request['client_id']) || empty($request['client_secret'])) { if (empty($request['client_id']) || empty($request['client_secret'])) {
Logger::warning('Incomplete request data', ['request' => $request]); $this->logger->warning('Incomplete request data', ['request' => $request]);
DI::mstdnError()->Unauthorized('invalid_client', DI::l10n()->t('Incomplete request data')); $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']); $application = OAuth::getApplication($request['client_id'], $request['client_secret'], $request['redirect_uri']);
if (empty($application)) { 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') { 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); $token = DBA::selectFirst('application-view', ['access_token', 'created_at', 'uid'], $condition);
if (!DBA::isResult($token)) { if (!DBA::isResult($token)) {
Logger::notice('Token not found or outdated', $condition); $this->logger->notice('Token not found or outdated', $condition);
DI::mstdnError()->Unauthorized(); $this->logErrorAndJsonExit(401, $this->errorFactory->Unauthorized());
} }
$owner = User::getOwnerDataById($token['uid']); $owner = User::getOwnerDataById($token['uid']);
$me = $owner['url']; $me = $owner['url'];