Improved error reporting

This commit is contained in:
Michael 2021-05-12 05:30:21 +00:00
parent d1e109d9be
commit c22846339a
3 changed files with 20 additions and 12 deletions

View file

@ -43,7 +43,7 @@ class Apps extends BaseApi
$website = !isset($_REQUEST['website']) ? '' : $_REQUEST['website']; $website = !isset($_REQUEST['website']) ? '' : $_REQUEST['website'];
if (empty($name) || empty($redirect)) { if (empty($name) || empty($redirect)) {
DI::mstdnError()->RecordNotFound(); DI::mstdnError()->UnprocessableEntity(DI::l10n()->t('Missing parameters'));
} }
$client_id = bin2hex(random_bytes(32)); $client_id = bin2hex(random_bytes(32));
@ -60,7 +60,7 @@ class Apps extends BaseApi
} }
if (!DBA::insert('application', $fields)) { if (!DBA::insert('application', $fields)) {
DI::mstdnError()->RecordNotFound(); DI::mstdnError()->InternalError();
} }
System::jsonExit(DI::mstdnApplication()->createFromApplicationId(DBA::lastInsertId())); System::jsonExit(DI::mstdnApplication()->createFromApplicationId(DBA::lastInsertId()));

View file

@ -193,15 +193,23 @@ class BaseApi extends BaseModule
public static function getApplication() public static function getApplication()
{ {
$redirect_uri = !isset($_REQUEST['redirect_uri']) ? '' : $_REQUEST['redirect_uri']; $redirect_uri = !isset($_REQUEST['redirect_uri']) ? '' : $_REQUEST['redirect_uri'];
$client_id = !isset($_REQUEST['client_id']) ? '' : $_REQUEST['client_id']; $client_id = !isset($_REQUEST['client_id']) ? '' : $_REQUEST['client_id'];
$client_secret = !isset($_REQUEST['client_secret']) ? '' : $_REQUEST['client_secret'];
if (empty($redirect_uri) || empty($client_id)) { if ((empty($redirect_uri) && empty($client_secret)) || empty($client_id)) {
Logger::warning('Incomplete request'); Logger::warning('Incomplete request', ['request' => $_REQUEST]);
return []; return [];
} }
$condition = ['redirect_uri' => $redirect_uri, 'client_id' => $client_id]; $condition = ['client_id' => $client_id];
if (!empty($client_secret)) {
$condition['client_secret'] = $client_secret;
}
if (!empty($redirect_uri)) {
$condition['redirect_uri'] = $redirect_uri;
}
$application = DBA::selectFirst('application', [], $condition); $application = DBA::selectFirst('application', [], $condition);
if (!DBA::isResult($application)) { if (!DBA::isResult($application)) {
Logger::warning('Application not found', $condition); Logger::warning('Application not found', $condition);

View file

@ -39,18 +39,18 @@ class Token extends BaseApi
$grant_type = !isset($_REQUEST['grant_type']) ? '' : $_REQUEST['grant_type']; $grant_type = !isset($_REQUEST['grant_type']) ? '' : $_REQUEST['grant_type'];
if ($grant_type != 'authorization_code') { if ($grant_type != 'authorization_code') {
Logger::warning('Wrong or missing grant type', ['grant_type' => $grant_type]); Logger::warning('Unsupported or missing grant type', ['request' => $_REQUEST]);
DI::mstdnError()->RecordNotFound(); DI::mstdnError()->UnprocessableEntity(DI::l10n()->t('Unsupported or missing grant type'));
} }
$application = self::getApplication(); $application = self::getApplication();
if (empty($application)) { if (empty($application)) {
DI::mstdnError()->RecordNotFound(); DI::mstdnError()->UnprocessableEntity();
} }
if ($application['client_secret'] != $client_secret) { if ($application['client_secret'] != $client_secret) {
Logger::warning('Wrong client secret', $client_secret); Logger::warning('Wrong client secret', $client_secret);
DI::mstdnError()->RecordNotFound(); DI::mstdnError()->Unauthorized();
} }
$condition = ['application-id' => $application['id'], 'code' => $code]; $condition = ['application-id' => $application['id'], 'code' => $code];
@ -58,7 +58,7 @@ class Token extends BaseApi
$token = DBA::selectFirst('application-token', ['access_token', 'created_at'], $condition); $token = DBA::selectFirst('application-token', ['access_token', 'created_at'], $condition);
if (!DBA::isResult($token)) { if (!DBA::isResult($token)) {
Logger::warning('Token not found', $condition); Logger::warning('Token not found', $condition);
DI::mstdnError()->RecordNotFound(); DI::mstdnError()->Unauthorized();
} }
// @todo Use entity class // @todo Use entity class