3 * @copyright Copyright (C) 2020, Friendica
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/>.
23 use Friendica\Core\Renderer;
24 use Friendica\Database\DBA;
26 use Friendica\Module\Security\Login;
28 require_once __DIR__ . '/../include/api.php';
30 function oauth_get_client(OAuthRequest $request)
32 $params = $request->get_parameters();
33 $token = $params['oauth_token'];
35 $r = q("SELECT `clients`.*
36 FROM `clients`, `tokens`
37 WHERE `clients`.`client_id`=`tokens`.`client_id`
38 AND `tokens`.`id`='%s' AND `tokens`.`scope`='request'", DBA::escape($token));
40 if (!DBA::isResult($r)) {
47 function api_post(App $a)
50 notice(DI::l10n()->t('Permission denied.') . EOL);
54 if (count($a->user) && !empty($a->user['uid']) && $a->user['uid'] != local_user()) {
55 notice(DI::l10n()->t('Permission denied.') . EOL);
60 function api_content(App $a)
62 if (DI::args()->getCommand() == 'api/oauth/authorize') {
64 * api/oauth/authorize interact with the user. return a standard page
67 DI::page()['template'] = "minimal";
69 // get consumer/client from request token
71 $request = OAuthRequest::from_request();
72 } catch (Exception $e) {
78 if (!empty($_POST['oauth_yes'])) {
79 $app = oauth_get_client($request);
81 return "Invalid request. Unknown token.";
83 $consumer = new OAuthConsumer($app['client_id'], $app['pw'], $app['redirect_uri']);
85 $verifier = md5($app['secret'] . local_user());
86 DI::config()->set("oauth", $verifier, local_user());
88 if ($consumer->callback_url != null) {
89 $params = $request->get_parameters();
91 if (strstr($consumer->callback_url, $glue)) {
94 DI::baseUrl()->redirect($consumer->callback_url . $glue . 'oauth_token=' . OAuthUtil::urlencode_rfc3986($params['oauth_token']) . '&oauth_verifier=' . OAuthUtil::urlencode_rfc3986($verifier));
98 $tpl = Renderer::getMarkupTemplate("oauth_authorize_done.tpl");
99 $o = Renderer::replaceMacros($tpl, [
100 '$title' => DI::l10n()->t('Authorize application connection'),
101 '$info' => DI::l10n()->t('Return to your app and insert this Securty Code:'),
102 '$code' => $verifier,
109 /// @TODO We need login form to redirect to this page
110 notice(DI::l10n()->t('Please login to continue.') . EOL);
111 return Login::form(DI::args()->getQueryString(), false, $request->get_parameters());
113 //FKOAuth1::loginUser(4);
115 $app = oauth_get_client($request);
117 return "Invalid request. Unknown token.";
120 $tpl = Renderer::getMarkupTemplate('oauth_authorize.tpl');
121 $o = Renderer::replaceMacros($tpl, [
122 '$title' => DI::l10n()->t('Authorize application connection'),
124 '$authorize' => DI::l10n()->t('Do you want to authorize this application to access your posts and contacts, and/or create new posts for you?'),
125 '$yes' => DI::l10n()->t('Yes'),
126 '$no' => DI::l10n()->t('No'),