3 * @copyright Copyright (C) 2010-2023, 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/>.
22 namespace Friendica\Module\Security;
24 use Friendica\BaseModule;
26 use Friendica\Util\Strings;
30 * Performs an login with OpenID
32 class OpenID extends BaseModule
34 protected function content(array $request = []): string
36 if (DI::config()->get('system', 'no_openid')) {
37 DI::baseUrl()->redirect();
40 DI::logger()->debug('mod_openid.', ['request' => $_REQUEST]);
42 $session = DI::session();
44 if (!empty($_GET['openid_mode']) && !empty($session->get('openid'))) {
46 $openid = new LightOpenID(DI::baseUrl()->getHost());
50 if ($openid->validate()) {
51 $authId = $openid->data['openid_identity'];
54 DI::logger()->info($l10n->t('OpenID protocol error. No ID returned'));
55 DI::baseUrl()->redirect();
58 // NOTE: we search both for normalised and non-normalised form of $authid
59 // because the normalization step was removed from settings
60 // in commit 8367cadeeffec4b6792a502847304b17ceba5882, so it might
61 // have left mixed records in the user table
63 $condition = ['blocked' => false, 'account_expired' => false, 'account_removed' => false, 'verified' => true,
64 'openid' => [$authId, Strings::normaliseOpenID($authId)]];
68 $user = $dba->selectFirst('user', [], $condition);
69 if ($dba->isResult($user)) {
71 // successful OpenID login
72 $session->remove('openid');
74 DI::auth()->setForUser(DI::app(), $user, true, true);
76 $this->baseUrl->redirect(DI::session()->pop('return_path', ''));
79 // Successful OpenID login - but we can't match it to an existing account.
80 $session->remove('register');
81 $session->set('openid_attributes', $openid->getAttributes());
82 $session->set('openid_identity', $authId);
84 // Detect the server URL
85 $open_id_obj = new LightOpenID(DI::baseUrl()->getHost());
86 $open_id_obj->identity = $authId;
87 $session->set('openid_server', $open_id_obj->discover($open_id_obj->identity));
89 if (intval(DI::config()->get('config', 'register_policy')) === \Friendica\Module\Register::CLOSED) {
90 DI::sysmsg()->addNotice($l10n->t('Account not found. Please login to your existing account to add the OpenID to it.'));
92 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.'));
95 DI::baseUrl()->redirect('login');