friendica/src/Module/Security/OpenID.php

102 lines
3.2 KiB
PHP
Raw Normal View History

2019-12-27 21:16:40 +00:00
<?php
2020-02-09 14:45:36 +00:00
/**
* @copyright Copyright (C) 2010-2024, the Friendica project
2020-02-09 14:45:36 +00:00
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
2019-12-27 21:16:40 +00:00
namespace Friendica\Module\Security;
use Friendica\BaseModule;
2019-12-29 15:48:15 +00:00
use Friendica\DI;
2019-12-27 21:16:40 +00:00
use Friendica\Util\Strings;
use LightOpenID;
/**
* Performs an login with OpenID
*/
class OpenID extends BaseModule
{
protected function content(array $request = []): string
2019-12-27 21:16:40 +00:00
{
2019-12-29 15:48:15 +00:00
if (DI::config()->get('system', 'no_openid')) {
DI::baseUrl()->redirect();
2019-12-27 21:16:40 +00:00
}
2019-12-29 15:48:15 +00:00
DI::logger()->debug('mod_openid.', ['request' => $_REQUEST]);
2019-12-27 21:16:40 +00:00
2019-12-29 15:48:15 +00:00
$session = DI::session();
2019-12-27 21:16:40 +00:00
if (!empty($_GET['openid_mode']) && !empty($session->get('openid'))) {
$openid = new LightOpenID(DI::baseUrl()->getHost());
2019-12-27 21:16:40 +00:00
2019-12-29 15:48:15 +00:00
$l10n = DI::l10n();
2019-12-27 21:16:40 +00:00
if ($openid->validate()) {
$authId = $openid->data['openid_identity'];
if (empty($authId)) {
2019-12-29 15:48:15 +00:00
DI::logger()->info($l10n->t('OpenID protocol error. No ID returned'));
DI::baseUrl()->redirect();
2019-12-27 21:16:40 +00:00
}
// NOTE: we search both for normalised and non-normalised form of $authid
// because the normalization step was removed from settings
// in commit 8367cadeeffec4b6792a502847304b17ceba5882, so it might
// have left mixed records in the user table
2019-12-27 21:16:40 +00:00
//
$condition = ['verified' => true, 'blocked' => false, 'account_removed' => false, 'account_expired' => false,
2019-12-27 21:16:40 +00:00
'openid' => [$authId, Strings::normaliseOpenID($authId)]];
2019-12-29 15:48:15 +00:00
$dba = DI::dba();
2019-12-27 21:16:40 +00:00
$user = $dba->selectFirst('user', [], $condition);
if ($dba->isResult($user)) {
// successful OpenID login
$session->remove('openid');
DI::auth()->setForUser(DI::app(), $user, true, true);
2019-12-27 21:16:40 +00:00
$this->baseUrl->redirect(DI::session()->pop('return_path', ''));
2019-12-27 21:16:40 +00:00
}
// Successful OpenID login - but we can't match it to an existing account.
$session->remove('register');
$session->set('openid_attributes', $openid->getAttributes());
$session->set('openid_identity', $authId);
// Detect the server URL
$open_id_obj = new LightOpenID(DI::baseUrl()->getHost());
2019-12-27 21:16:40 +00:00
$open_id_obj->identity = $authId;
$session->set('openid_server', $open_id_obj->discover($open_id_obj->identity));
if (\Friendica\Module\Register::getPolicy() === \Friendica\Module\Register::CLOSED) {
2022-10-17 18:55:22 +00:00
DI::sysmsg()->addNotice($l10n->t('Account not found. Please login to your existing account to add the OpenID to it.'));
2019-12-27 21:16:40 +00:00
} else {
2022-10-17 18:55:22 +00:00
DI::sysmsg()->addNotice($l10n->t('Account not found. Please register a new account or login to your existing account to add the OpenID to it.'));
2019-12-27 21:16:40 +00:00
}
2019-12-29 15:48:15 +00:00
DI::baseUrl()->redirect('login');
2019-12-27 21:16:40 +00:00
}
}
2021-11-14 22:13:47 +00:00
return '';
2019-12-27 21:16:40 +00:00
}
}