3 namespace Friendica\Module\Security;
5 use Friendica\App\Authentication;
6 use Friendica\App\BaseURL;
7 use Friendica\BaseModule;
8 use Friendica\Core\Config\Configuration;
9 use Friendica\Core\L10n\L10n;
10 use Friendica\Core\Session\ISession;
11 use Friendica\Database\Database;
13 use Friendica\Util\Strings;
15 use Psr\Log\LoggerInterface;
18 * Performs an login with OpenID
20 class OpenID extends BaseModule
22 public static function content(array $parameters = [])
24 if (DI::config()->get('system', 'no_openid')) {
25 DI::baseUrl()->redirect();
28 DI::logger()->debug('mod_openid.', ['request' => $_REQUEST]);
30 $session = DI::session();
32 if (!empty($_GET['openid_mode']) && !empty($session->get('openid'))) {
34 $openid = new LightOpenID(DI::baseUrl()->getHostname());
38 if ($openid->validate()) {
39 $authId = $openid->data['openid_identity'];
42 DI::logger()->info($l10n->t('OpenID protocol error. No ID returned'));
43 DI::baseUrl()->redirect();
46 // NOTE: we search both for normalised and non-normalised form of $authid
47 // because the normalization step was removed from setting
48 // mod/settings.php in 8367cad so it might have left mixed
49 // records in the user table
51 $condition = ['blocked' => false, 'account_expired' => false, 'account_removed' => false, 'verified' => true,
52 'openid' => [$authId, Strings::normaliseOpenID($authId)]];
56 $user = $dba->selectFirst('user', [], $condition);
57 if ($dba->isResult($user)) {
59 // successful OpenID login
60 $session->remove('openid');
62 DI::auth()->setForUser(self::getApp(), $user, true, true);
64 // just in case there was no return url set
65 // and we fell through
66 DI::baseUrl()->redirect();
69 // Successful OpenID login - but we can't match it to an existing account.
70 $session->remove('register');
71 $session->set('openid_attributes', $openid->getAttributes());
72 $session->set('openid_identity', $authId);
74 // Detect the server URL
75 $open_id_obj = new LightOpenID(DI::baseUrl()->getHostName());
76 $open_id_obj->identity = $authId;
77 $session->set('openid_server', $open_id_obj->discover($open_id_obj->identity));
79 if (intval(DI::config()->get('config', 'register_policy')) === \Friendica\Module\Register::CLOSED) {
80 notice($l10n->t('Account not found. Please login to your existing account to add the OpenID to it.'));
82 notice($l10n->t('Account not found. Please register a new account or login to your existing account to add the OpenID to it.'));
85 DI::baseUrl()->redirect('login');