3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Module\OAuth;
24 use Friendica\Core\Logger;
26 use Friendica\Module\BaseApi;
29 * @see https://docs.joinmastodon.org/spec/oauth/
30 * @see https://aaronparecki.com/oauth-2-simplified/
32 class Authorize extends BaseApi
35 * @param array $parameters
36 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
38 public static function rawContent(array $parameters = [])
40 $request = self::getRequest([
41 'response_type' => '',
43 'client_secret' => '', // Isn't normally provided. We will use it if present.
49 if ($request['response_type'] != 'code') {
50 Logger::warning('Unsupported or missing response type', ['request' => $_REQUEST]);
51 DI::mstdnError()->UnprocessableEntity(DI::l10n()->t('Unsupported or missing response type'));
54 if (empty($request['client_id']) || empty($request['redirect_uri'])) {
55 Logger::warning('Incomplete request data', ['request' => $_REQUEST]);
56 DI::mstdnError()->UnprocessableEntity(DI::l10n()->t('Incomplete request data'));
59 $application = self::getApplication($request['client_id'], $request['client_secret'], $request['redirect_uri']);
60 if (empty($application)) {
61 DI::mstdnError()->UnprocessableEntity();
64 // @todo Compare the application scope and requested scope
66 $redirect_request = $_REQUEST;
67 unset($redirect_request['pagename']);
68 $redirect = 'oauth/authorize?' . http_build_query($redirect_request);
72 Logger::info('Redirect to login');
73 DI::app()->redirect('login?return_path=' . urlencode($redirect));
75 Logger::info('Already logged in user', ['uid' => $uid]);
78 if (!self::existsTokenForUser($application, $uid) && !DI::session()->get('oauth_acknowledge')) {
79 Logger::info('Redirect to acknowledge');
80 DI::app()->redirect('oauth/acknowledge?' . http_build_query(['return_path' => $redirect, 'application' => $application['name']]));
83 DI::session()->remove('oauth_acknowledge');
85 $token = self::createTokenForUser($application, $uid, $request['scope']);
87 DI::mstdnError()->UnprocessableEntity();
90 DI::app()->redirect($application['redirect_uri'] . (strpos($application['redirect_uri'], '?') ? '&' : '?') . http_build_query(['code' => $token['code'], 'state' => $request['state']]));