. * */ namespace Friendica\Module\OAuth; use Friendica\Core\Logger; use Friendica\Core\System; use Friendica\Database\DBA; use Friendica\DI; use Friendica\Module\BaseApi; /** * @see https://docs.joinmastodon.org/spec/oauth/ */ class Token extends BaseApi { public static function post(array $parameters = []) { $client_secret = $_REQUEST['client_secret'] ?? ''; $code = $_REQUEST['code'] ?? ''; $grant_type = $_REQUEST['grant_type'] ?? ''; if ($grant_type != 'authorization_code') { Logger::warning('Unsupported or missing grant type', ['request' => $_REQUEST]); DI::mstdnError()->UnprocessableEntity(DI::l10n()->t('Unsupported or missing grant type')); } $application = self::getApplication(); if (empty($application)) { DI::mstdnError()->UnprocessableEntity(); } if ($application['client_secret'] != $client_secret) { Logger::warning('Wrong client secret', $client_secret); DI::mstdnError()->Unauthorized(); } $condition = ['application-id' => $application['id'], 'code' => $code]; $token = DBA::selectFirst('application-token', ['access_token', 'created_at'], $condition); if (!DBA::isResult($token)) { Logger::warning('Token not found', $condition); DI::mstdnError()->Unauthorized(); } // @todo Use entity class System::jsonExit(['access_token' => $token['access_token'], 'token_type' => 'Bearer', 'scope' => $application['scopes'], 'created_at' => $token['created_at']]); } }