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/>.
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 public static function content(array $parameters = [])
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()->getHostname());
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 setting
60 // mod/settings.php in 8367cad so it might have left mixed
61 // 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 // just in case there was no return url set
77 // and we fell through
78 DI::baseUrl()->redirect();
81 // Successful OpenID login - but we can't match it to an existing account.
82 $session->remove('register');
83 $session->set('openid_attributes', $openid->getAttributes());
84 $session->set('openid_identity', $authId);
86 // Detect the server URL
87 $open_id_obj = new LightOpenID(DI::baseUrl()->getHostName());
88 $open_id_obj->identity = $authId;
89 $session->set('openid_server', $open_id_obj->discover($open_id_obj->identity));
91 if (intval(DI::config()->get('config', 'register_policy')) === \Friendica\Module\Register::CLOSED) {
92 notice($l10n->t('Account not found. Please login to your existing account to add the OpenID to it.'));
94 notice($l10n->t('Account not found. Please register a new account or login to your existing account to add the OpenID to it.'));
97 DI::baseUrl()->redirect('login');