friendica/src/Module/OAuth/Token.php

73 lines
2.6 KiB
PHP
Raw Normal View History

2021-05-11 08:30:20 +02:00
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Module\OAuth;
use Friendica\Core\Logger;
use Friendica\Core\System;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Module\BaseApi;
/**
2021-05-12 14:00:24 +02:00
* @see https://docs.joinmastodon.org/spec/oauth/
2021-05-13 13:26:56 +02:00
* @see https://aaronparecki.com/oauth-2-simplified/
2021-05-11 08:30:20 +02:00
*/
class Token extends BaseApi
{
public static function post(array $parameters = [])
{
2021-05-12 14:40:45 +02:00
$grant_type = $_REQUEST['grant_type'] ?? '';
2021-05-13 13:26:56 +02:00
$code = $_REQUEST['code'] ?? '';
$redirect_uri = $_REQUEST['redirect_uri'] ?? '';
$client_id = $_REQUEST['client_id'] ?? '';
$client_secret = $_REQUEST['client_secret'] ?? '';
2021-05-11 08:30:20 +02:00
if ($grant_type != 'authorization_code') {
2021-05-12 07:30:21 +02:00
Logger::warning('Unsupported or missing grant type', ['request' => $_REQUEST]);
DI::mstdnError()->UnprocessableEntity(DI::l10n()->t('Unsupported or missing grant type'));
2021-05-11 08:30:20 +02:00
}
2021-05-13 13:26:56 +02:00
if (empty($client_id) || empty($client_secret) || empty($redirect_uri)) {
Logger::warning('Incomplete request data', ['request' => $_REQUEST]);
DI::mstdnError()->UnprocessableEntity(DI::l10n()->t('Incomplete request data'));
2021-05-11 08:30:20 +02:00
}
2021-05-13 13:26:56 +02:00
$application = self::getApplication($client_id, $client_secret, $redirect_uri);
if (empty($application)) {
DI::mstdnError()->UnprocessableEntity();
2021-05-11 08:30:20 +02:00
}
2021-05-13 13:26:56 +02:00
// For security reasons only allow freshly created tokens
$condition = ["`application-id` = ? AND `code` = ? AND `created_at` > UTC_TIMESTAMP() - INTERVAL ? MINUTE", $application['id'], $code, 5];
2021-05-11 08:31:48 +02:00
2021-05-11 21:53:19 +02:00
$token = DBA::selectFirst('application-token', ['access_token', 'created_at'], $condition);
2021-05-11 08:30:20 +02:00
if (!DBA::isResult($token)) {
2021-05-13 13:26:56 +02:00
Logger::warning('Token not found or outdated', $condition);
2021-05-12 07:30:21 +02:00
DI::mstdnError()->Unauthorized();
2021-05-11 08:30:20 +02:00
}
2021-05-13 13:26:56 +02:00
$object = new \Friendica\Object\Api\Mastodon\Token($token['access_token'], 'Bearer', $application['scopes'], $token['created_at']);
System::jsonExit($object->toArray());
2021-05-11 08:30:20 +02:00
}
}