]> git.mxchange.org Git - friendica.git/blob - src/Module/OAuth/Authorize.php
API: Tests with various clients, small fixes
[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\Model\Post\Content;
27 use Friendica\Module\BaseApi;
28 use Friendica\Security\OAuth;
29
30 /**
31  * @see https://docs.joinmastodon.org/spec/oauth/
32  * @see https://aaronparecki.com/oauth-2-simplified/
33  */
34 class Authorize extends BaseApi
35 {
36         /**
37          * @param array $parameters
38          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
39          */
40         public static function rawContent(array $parameters = [])
41         {
42                 $request = self::getRequest([
43                         'response_type' => '',
44                         'client_id'     => '',
45                         'client_secret' => '', // Isn't normally provided. We will use it if present.
46                         'redirect_uri'  => '',
47                         'scope'         => 'read',
48                         'state'         => '',
49                 ]);
50
51                 if ($request['response_type'] != 'code') {
52                         Logger::warning('Unsupported or missing response type', ['request' => $_REQUEST]);
53                         DI::mstdnError()->UnprocessableEntity(DI::l10n()->t('Unsupported or missing response type'));
54                 }
55
56                 if (empty($request['client_id']) || empty($request['redirect_uri'])) {
57                         Logger::warning('Incomplete request data', ['request' => $_REQUEST]);
58                         DI::mstdnError()->UnprocessableEntity(DI::l10n()->t('Incomplete request data'));
59                 }
60
61                 $application = OAuth::getApplication($request['client_id'], $request['client_secret'], $request['redirect_uri']);
62                 if (empty($application)) {
63                         DI::mstdnError()->UnprocessableEntity();
64                 }
65
66                 // @todo Compare the application scope and requested scope
67
68                 $redirect_request = $_REQUEST;
69                 unset($redirect_request['pagename']);
70                 $redirect = 'oauth/authorize?' . http_build_query($redirect_request);
71
72                 $uid = local_user();
73                 if (empty($uid)) {
74                         Logger::info('Redirect to login');
75                         DI::app()->redirect('login?return_path=' . urlencode($redirect));
76                 } else {
77                         Logger::info('Already logged in user', ['uid' => $uid]);
78                 }
79
80                 if (!OAuth::existsTokenForUser($application, $uid) && !DI::session()->get('oauth_acknowledge')) {
81                         Logger::info('Redirect to acknowledge');
82                         DI::app()->redirect('oauth/acknowledge?' . http_build_query(['return_path' => $redirect, 'application' => $application['name']]));
83                 }
84
85                 DI::session()->remove('oauth_acknowledge');
86
87                 $token = OAuth::createTokenForUser($application, $uid, $request['scope']);
88                 if (!$token) {
89                         DI::mstdnError()->UnprocessableEntity();
90                 }
91
92                 if ($application['redirect_uri'] != 'urn:ietf:wg:oauth:2.0:oob') {
93                         DI::app()->redirect($application['redirect_uri'] . (strpos($application['redirect_uri'], '?') ? '&' : '?') . http_build_query(['code' => $token['code'], 'state' => $request['state']]));
94                 }
95
96                 DI::session()->set('oauth_token_code', $token['code']);
97         }
98
99         public static function content(array $parameters = [])
100         {
101                 $code = DI::session()->get('oauth_token_code');
102                 DI::session()->remove('oauth_token_code');
103
104                 if (empty($code)) {
105                         return '';
106                 }
107
108                 return DI::l10n()->t('Please copy the following authentication code into your application and close this window: %s', $code);
109         }
110 }