]> git.mxchange.org Git - friendica.git/blob - src/Module/OAuth/Authorize.php
Merge remote-tracking branch 'upstream/develop' into api-lists-missing
[friendica.git] / src / Module / OAuth / Authorize.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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
28 /**
29  * @see https://docs.joinmastodon.org/spec/oauth/
30  * @see https://aaronparecki.com/oauth-2-simplified/
31  */
32 class Authorize extends BaseApi
33 {
34         /**
35          * @param array $parameters
36          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
37          */
38         public static function rawContent(array $parameters = [])
39         {
40                 $response_type = $_REQUEST['response_type'] ?? '';
41                 $client_id     = $_REQUEST['client_id'] ?? '';
42                 $client_secret = $_REQUEST['client_secret'] ?? ''; // Isn't normally provided. We will use it if present.
43                 $redirect_uri  = $_REQUEST['redirect_uri'] ?? '';
44                 $scope         = $_REQUEST['scope'] ?? 'read';
45                 $state         = $_REQUEST['state'] ?? '';
46
47                 if ($response_type != 'code') {
48                         Logger::warning('Unsupported or missing response type', ['request' => $_REQUEST]);
49                         DI::mstdnError()->UnprocessableEntity(DI::l10n()->t('Unsupported or missing response type'));
50                 }
51
52                 if (empty($client_id) || empty($redirect_uri)) {
53                         Logger::warning('Incomplete request data', ['request' => $_REQUEST]);
54                         DI::mstdnError()->UnprocessableEntity(DI::l10n()->t('Incomplete request data'));
55                 }
56
57                 $application = self::getApplication($client_id, $client_secret, $redirect_uri);
58                 if (empty($application)) {
59                         DI::mstdnError()->UnprocessableEntity();
60                 }
61
62                 // @todo Compare the application scope and requested scope
63
64                 $request = $_REQUEST;
65                 unset($request['pagename']);
66                 $redirect = 'oauth/authorize?' . http_build_query($request);
67
68                 $uid = local_user();
69                 if (empty($uid)) {
70                         Logger::info('Redirect to login');
71                         DI::app()->redirect('login?return_path=' . urlencode($redirect));
72                 } else {
73                         Logger::info('Already logged in user', ['uid' => $uid]);
74                 }
75
76                 if (!self::existsTokenForUser($application, $uid) && !DI::session()->get('oauth_acknowledge')) {
77                         Logger::info('Redirect to acknowledge');
78                         DI::app()->redirect('oauth/acknowledge?' . http_build_query(['return_path' => $redirect, 'application' => $application['name']]));
79                 }
80
81                 DI::session()->remove('oauth_acknowledge');
82
83                 $token = self::createTokenForUser($application, $uid, $scope);
84                 if (!$token) {
85                         DI::mstdnError()->UnprocessableEntity();
86                 }
87
88                 DI::app()->redirect($application['redirect_uri'] . (strpos($application['redirect_uri'], '?') ? '&' : '?') . http_build_query(['code' => $token['code'], 'state' => $state]));
89         }
90 }