]> git.mxchange.org Git - friendica.git/blob - src/Module/OAuth/Authorize.php
Move jsonError out of Factory\Api\Mastodon\Error->UnprocessableEntity
[friendica.git] / src / Module / OAuth / Authorize.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Module\OAuth;
23
24 use Friendica\Core\Logger;
25 use Friendica\DI;
26 use Friendica\Module\BaseApi;
27 use Friendica\Security\OAuth;
28
29 /**
30  * @see https://docs.joinmastodon.org/spec/oauth/
31  * @see https://aaronparecki.com/oauth-2-simplified/
32  */
33 class Authorize extends BaseApi
34 {
35         private static $oauth_code = '';
36
37         /**
38          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
39          */
40         protected function rawContent(array $request = [])
41         {
42                 $request = $this->getRequest([
43                         'force_login'   => '', // Forces the user to re-login, which is necessary for authorizing with multiple accounts from the same instance.
44                         'response_type' => '', // Should be set equal to "code".
45                         'client_id'     => '', // Client ID, obtained during app registration.
46                         'client_secret' => '', // Isn't normally provided. We will use it if present.
47                         'redirect_uri'  => '', // Set a URI to redirect the user to. If this parameter is set to "urn:ietf:wg:oauth:2.0:oob" then the authorization code will be shown instead. Must match one of the redirect URIs declared during app registration.
48                         'scope'         => 'read', // List of requested OAuth scopes, separated by spaces (or by pluses, if using query parameters). Must be a subset of scopes declared during app registration. If not provided, defaults to "read".
49                         'state'         => '',
50                 ], $request);
51
52                 if ($request['response_type'] != 'code') {
53                         Logger::warning('Unsupported or missing response type', ['request' => $_REQUEST]);
54                         $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity($this->t('Unsupported or missing response type')));
55                 }
56
57                 if (empty($request['client_id']) || empty($request['redirect_uri'])) {
58                         Logger::warning('Incomplete request data', ['request' => $_REQUEST]);
59                         $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity($this->t('Incomplete request data')));
60                 }
61
62                 $application = OAuth::getApplication($request['client_id'], $request['client_secret'], $request['redirect_uri']);
63                 if (empty($application)) {
64                         $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity());
65                 }
66
67                 // @todo Compare the application scope and requested scope
68
69                 $redirect_request = $_REQUEST;
70                 unset($redirect_request['pagename']);
71                 $redirect = 'oauth/authorize?' . http_build_query($redirect_request);
72
73                 $uid = DI::userSession()->getLocalUserId();
74                 if (empty($uid)) {
75                         Logger::info('Redirect to login');
76                         DI::app()->redirect('login?return_path=' . urlencode($redirect));
77                 } else {
78                         Logger::info('Already logged in user', ['uid' => $uid]);
79                 }
80
81                 if (!OAuth::existsTokenForUser($application, $uid) && !DI::session()->get('oauth_acknowledge')) {
82                         Logger::info('Redirect to acknowledge');
83                         DI::app()->redirect('oauth/acknowledge?' . http_build_query(['return_path' => $redirect, 'application' => $application['name']]));
84                 }
85
86                 DI::session()->remove('oauth_acknowledge');
87
88                 $token = OAuth::createTokenForUser($application, $uid, $request['scope']);
89                 if (!$token) {
90                         $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity());
91                 }
92
93                 if ($application['redirect_uri'] != 'urn:ietf:wg:oauth:2.0:oob') {
94                         DI::app()->redirect($request['redirect_uri'] . (strpos($request['redirect_uri'], '?') ? '&' : '?') . http_build_query(['code' => $token['code'], 'state' => $request['state']]));
95                 }
96
97                 self::$oauth_code = $token['code'];
98         }
99
100         protected function content(array $request = []): string
101         {
102                 if (empty(self::$oauth_code)) {
103                         return '';
104                 }
105
106                 return DI::l10n()->t('Please copy the following authentication code into your application and close this window: %s', self::$oauth_code);
107         }
108 }