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/>.
23 use Friendica\Core\Renderer;
24 use Friendica\Database\DBA;
26 use Friendica\Module\Security\Login;
27 use Friendica\Security\OAuth1\OAuthRequest;
28 use Friendica\Security\OAuth1\OAuthUtil;
30 require_once __DIR__ . '/../include/api.php';
32 function oauth_get_client(OAuthRequest $request)
34 $params = $request->get_parameters();
35 $token = $params['oauth_token'];
37 $r = q("SELECT `clients`.*
38 FROM `clients`, `tokens`
39 WHERE `clients`.`client_id`=`tokens`.`client_id`
40 AND `tokens`.`id`='%s' AND `tokens`.`scope`='request'", DBA::escape($token));
42 if (!DBA::isResult($r)) {
49 function api_post(App $a)
52 notice(DI::l10n()->t('Permission denied.'));
56 if (count($a->user) && !empty($a->user['uid']) && $a->user['uid'] != local_user()) {
57 notice(DI::l10n()->t('Permission denied.'));
62 function api_content(App $a)
64 if (DI::args()->getCommand() == 'api/oauth/authorize') {
66 * api/oauth/authorize interact with the user. return a standard page
69 DI::page()['template'] = "minimal";
71 // get consumer/client from request token
73 $request = OAuthRequest::from_request();
74 } catch (Exception $e) {
80 if (!empty($_POST['oauth_yes'])) {
81 $app = oauth_get_client($request);
83 return "Invalid request. Unknown token.";
85 $consumer = new OAuthConsumer($app['client_id'], $app['pw'], $app['redirect_uri']);
87 $verifier = md5($app['secret'] . local_user());
88 DI::config()->set("oauth", $verifier, local_user());
90 if ($consumer->callback_url != null) {
91 $params = $request->get_parameters();
93 if (strstr($consumer->callback_url, $glue)) {
96 DI::baseUrl()->redirect($consumer->callback_url . $glue . 'oauth_token=' . OAuthUtil::urlencode_rfc3986($params['oauth_token']) . '&oauth_verifier=' . OAuthUtil::urlencode_rfc3986($verifier));
100 $tpl = Renderer::getMarkupTemplate("oauth_authorize_done.tpl");
101 $o = Renderer::replaceMacros($tpl, [
102 '$title' => DI::l10n()->t('Authorize application connection'),
103 '$info' => DI::l10n()->t('Return to your app and insert this Securty Code:'),
104 '$code' => $verifier,
111 /// @TODO We need login form to redirect to this page
112 notice(DI::l10n()->t('Please login to continue.'));
113 return Login::form(DI::args()->getQueryString(), false, $request->get_parameters());
115 //FKOAuth1::loginUser(4);
117 $app = oauth_get_client($request);
119 return "Invalid request. Unknown token.";
122 $tpl = Renderer::getMarkupTemplate('oauth_authorize.tpl');
123 $o = Renderer::replaceMacros($tpl, [
124 '$title' => DI::l10n()->t('Authorize application connection'),
126 '$authorize' => DI::l10n()->t('Do you want to authorize this application to access your posts and contacts, and/or create new posts for you?'),
127 '$yes' => DI::l10n()->t('Yes'),
128 '$no' => DI::l10n()->t('No'),