Merge pull request #8021 from nupplaphil/task/mod_openid

Move mod/openid to src\Module\Security\OpenId
This commit is contained in:
Hypolite Petovan 2019-12-28 21:39:42 -05:00 committed by GitHub
commit f000680511
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
29 changed files with 136 additions and 111 deletions

View file

@ -8,7 +8,7 @@ use Friendica\Core\Config;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\Core\Renderer; use Friendica\Core\Renderer;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Module\Login; use Friendica\Module\Security\Login;
require_once __DIR__ . '/../include/api.php'; require_once __DIR__ . '/../include/api.php';

View file

@ -13,7 +13,7 @@ use Friendica\Core\Logger;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Core\Session; use Friendica\Core\Session;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Module\Login; use Friendica\Module\Security\Login;
use Friendica\Protocol\DFRN; use Friendica\Protocol\DFRN;
use Friendica\Protocol\OStatus; use Friendica\Protocol\OStatus;
use Friendica\Util\Network; use Friendica\Util\Network;

View file

@ -25,7 +25,7 @@ use Friendica\Model\Contact;
use Friendica\Model\Group; use Friendica\Model\Group;
use Friendica\Model\Profile; use Friendica\Model\Profile;
use Friendica\Model\User; use Friendica\Model\User;
use Friendica\Module\Login; use Friendica\Module\Security\Login;
use Friendica\Network\Probe; use Friendica\Network\Probe;
use Friendica\Protocol\Activity; use Friendica\Protocol\Activity;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;

View file

@ -19,7 +19,7 @@ use Friendica\Database\DBA;
use Friendica\Model\Event; use Friendica\Model\Event;
use Friendica\Model\Item; use Friendica\Model\Item;
use Friendica\Model\Profile; use Friendica\Model\Profile;
use Friendica\Module\Login; use Friendica\Module\Security\Login;
use Friendica\Util\ACLFormatter; use Friendica\Util\ACLFormatter;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;
use Friendica\Util\Strings; use Friendica\Util\Strings;

View file

@ -15,7 +15,7 @@ use Friendica\Core\System;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Model\Contact; use Friendica\Model\Contact;
use Friendica\Model\Mail; use Friendica\Model\Mail;
use Friendica\Module\Login; use Friendica\Module\Security\Login;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;
use Friendica\Util\Proxy as ProxyUtils; use Friendica\Util\Proxy as ProxyUtils;
use Friendica\Util\Strings; use Friendica\Util\Strings;

View file

@ -27,7 +27,7 @@ use Friendica\Model\Group;
use Friendica\Model\Item; use Friendica\Model\Item;
use Friendica\Model\Profile; use Friendica\Model\Profile;
use Friendica\Model\Term; use Friendica\Model\Term;
use Friendica\Module\Login; use Friendica\Module\Security\Login;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;
use Friendica\Util\Proxy as ProxyUtils; use Friendica\Util\Proxy as ProxyUtils;
use Friendica\Util\Strings; use Friendica\Util\Strings;

View file

@ -13,7 +13,7 @@ use Friendica\Core\Protocol;
use Friendica\Core\Renderer; use Friendica\Core\Renderer;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Module\Login; use Friendica\Module\Security\Login;
use Friendica\Model\Introduction; use Friendica\Model\Introduction;
use Friendica\Model\Notify; use Friendica\Model\Notify;

View file

@ -6,7 +6,7 @@ use Friendica\App;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\Core\Renderer; use Friendica\Core\Renderer;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Module\Login; use Friendica\Module\Security\Login;
use Friendica\Util\Network; use Friendica\Util\Network;
use Friendica\Util\Strings; use Friendica\Util\Strings;

View file

@ -1,82 +0,0 @@
<?php
/**
* @file mod/openid.php
*/
use Friendica\App;
use Friendica\BaseObject;
use Friendica\App\Authentication;
use Friendica\Core\Config;
use Friendica\Core\L10n;
use Friendica\Core\Logger;
use Friendica\Core\Session;
use Friendica\Database\DBA;
use Friendica\Util\Strings;
function openid_content(App $a) {
if (Config::get('system','no_openid')) {
$a->internalRedirect();
}
Logger::log('mod_openid ' . print_r($_REQUEST,true), Logger::DATA);
if (!empty($_GET['openid_mode']) && !empty($_SESSION['openid'])) {
$openid = new LightOpenID($a->getHostName());
if ($openid->validate()) {
$authid = $openid->data['openid_identity'];
if (empty($authid)) {
Logger::log(L10n::t('OpenID protocol error. No ID returned.') . EOL);
$a->internalRedirect();
}
// NOTE: we search both for normalised and non-normalised form of $authid
// because the normalization step was removed from setting
// mod/settings.php in 8367cad so it might have left mixed
// records in the user table
//
$condition = ['blocked' => false, 'account_expired' => false, 'account_removed' => false, 'verified' => true,
'openid' => [$authid, Strings::normaliseOpenID($authid)]];
$user = DBA::selectFirst('user', [], $condition);
if (DBA::isResult($user)) {
// successful OpenID login
unset($_SESSION['openid']);
/** @var Authentication $authentication */
$authentication = BaseObject::getClass(Authentication::class);
$authentication->setForUser($a, $user, true, true);
// just in case there was no return url set
// and we fell through
$a->internalRedirect();
}
// Successful OpenID login - but we can't match it to an existing account.
unset($_SESSION['register']);
Session::set('openid_attributes', $openid->getAttributes());
Session::set('openid_identity', $authid);
// Detect the server URL
$open_id_obj = new LightOpenID($a->getHostName());
$open_id_obj->identity = $authid;
Session::set('openid_server', $open_id_obj->discover($open_id_obj->identity));
if (intval(Config::get('config', 'register_policy')) === \Friendica\Module\Register::CLOSED) {
notice(L10n::t('Account not found. Please login to your existing account to add the OpenID to it.'));
} else {
notice(L10n::t('Account not found. Please register a new account or login to your existing account to add the OpenID to it.'));
}
$a->internalRedirect('login');
}
}
notice(L10n::t('Login failed.') . EOL);
$a->internalRedirect();
// NOTREACHED
}

View file

@ -20,7 +20,7 @@ use Friendica\Model\Contact;
use Friendica\Model\GContact; use Friendica\Model\GContact;
use Friendica\Model\Profile; use Friendica\Model\Profile;
use Friendica\Model\User; use Friendica\Model\User;
use Friendica\Module\Login; use Friendica\Module\Security\Login;
use Friendica\Network\Probe; use Friendica\Network\Probe;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;
use Friendica\Util\Strings; use Friendica\Util\Strings;

View file

@ -11,7 +11,7 @@ use Friendica\Core\Worker;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Model\Register; use Friendica\Model\Register;
use Friendica\Model\User; use Friendica\Model\User;
use Friendica\Module\Login; use Friendica\Module\Security\Login;
function user_allow($hash) function user_allow($hash)
{ {

View file

@ -24,7 +24,7 @@ use Friendica\Model\Contact;
use Friendica\Model\GContact; use Friendica\Model\GContact;
use Friendica\Model\Group; use Friendica\Model\Group;
use Friendica\Model\User; use Friendica\Model\User;
use Friendica\Module\Login; use Friendica\Module\Security\Login;
use Friendica\Protocol\Email; use Friendica\Protocol\Email;
use Friendica\Util\ACLFormatter; use Friendica\Util\ACLFormatter;
use Friendica\Util\Network; use Friendica\Util\Network;

View file

@ -18,7 +18,7 @@ use Friendica\Core\System;
use Friendica\Core\Theme; use Friendica\Core\Theme;
use Friendica\Database\Database; use Friendica\Database\Database;
use Friendica\Model\Profile; use Friendica\Model\Profile;
use Friendica\Module\Login; use Friendica\Module\Security\Login;
use Friendica\Module\Special\HTTPException as ModuleHTTPException; use Friendica\Module\Special\HTTPException as ModuleHTTPException;
use Friendica\Network\HTTPException; use Friendica\Network\HTTPException;
use Friendica\Util\ConfigFileLoader; use Friendica\Util\ConfigFileLoader;

View file

@ -5,6 +5,7 @@ namespace Friendica\Module;
use Friendica\BaseModule; use Friendica\BaseModule;
use Friendica\Core\ACL; use Friendica\Core\ACL;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\Module\Security\Login;
use Friendica\Network\HTTPException; use Friendica\Network\HTTPException;
use Friendica\Util\Strings; use Friendica\Util\Strings;

View file

@ -18,6 +18,7 @@ use Friendica\Core\System;
use Friendica\Core\Worker; use Friendica\Core\Worker;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Model; use Friendica\Model;
use Friendica\Module\Security\Login;
use Friendica\Network\HTTPException\BadRequestException; use Friendica\Network\HTTPException\BadRequestException;
use Friendica\Network\HTTPException\NotFoundException; use Friendica\Network\HTTPException\NotFoundException;
use Friendica\Network\Probe; use Friendica\Network\Probe;

View file

@ -6,6 +6,7 @@ use Friendica\BaseModule;
use Friendica\Core\Hook; use Friendica\Core\Hook;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\Core\Renderer; use Friendica\Core\Renderer;
use Friendica\Module\Security\Login;
/** /**
* Home module - Landing page of the current node * Home module - Landing page of the current node

View file

@ -17,7 +17,7 @@ use Friendica\Model\FileTag;
use Friendica\Model\Group; use Friendica\Model\Group;
use Friendica\Model\Item; use Friendica\Model\Item;
use Friendica\Model\User; use Friendica\Model\User;
use Friendica\Module\Login; use Friendica\Module\Security\Login;
use Friendica\Network\HTTPException\NotImplementedException; use Friendica\Network\HTTPException\NotImplementedException;
use Friendica\Util\ACLFormatter; use Friendica\Util\ACLFormatter;
use Friendica\Util\Crypto; use Friendica\Util\Crypto;

View file

@ -19,6 +19,7 @@ use Friendica\Model\Group;
use Friendica\Model\Item; use Friendica\Model\Item;
use Friendica\Model\Profile as ProfileModel; use Friendica\Model\Profile as ProfileModel;
use Friendica\Model\User; use Friendica\Model\User;
use Friendica\Module\Security\Login;
use Friendica\Protocol\ActivityPub; use Friendica\Protocol\ActivityPub;
use Friendica\Protocol\DFRN; use Friendica\Protocol\DFRN;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;

View file

@ -5,7 +5,7 @@ namespace Friendica\Module\Search;
use Friendica\Content\Widget; use Friendica\Content\Widget;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\Module\BaseSearchModule; use Friendica\Module\BaseSearchModule;
use Friendica\Module\Login; use Friendica\Module\Security\Login;
use Friendica\Util\Strings; use Friendica\Util\Strings;
/** /**

View file

@ -4,7 +4,7 @@
* @file src/Module/Login.php * @file src/Module/Login.php
*/ */
namespace Friendica\Module; namespace Friendica\Module\Security;
use Friendica\BaseModule; use Friendica\BaseModule;
use Friendica\App\Authentication; use Friendica\App\Authentication;
@ -13,6 +13,7 @@ use Friendica\Core\Hook;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\Core\Renderer; use Friendica\Core\Renderer;
use Friendica\Core\Session; use Friendica\Core\Session;
use Friendica\Module\Register;
use Friendica\Util\Strings; use Friendica\Util\Strings;
/** /**

View file

@ -3,7 +3,7 @@
* @file src/Module/Logout.php * @file src/Module/Logout.php
*/ */
namespace Friendica\Module; namespace Friendica\Module\Security;
use Friendica\BaseModule; use Friendica\BaseModule;
use Friendica\App\Authentication; use Friendica\App\Authentication;

View file

@ -0,0 +1,101 @@
<?php
namespace Friendica\Module\Security;
use Friendica\App\Authentication;
use Friendica\App\BaseURL;
use Friendica\BaseModule;
use Friendica\Core\Config\Configuration;
use Friendica\Core\L10n\L10n;
use Friendica\Core\Session\ISession;
use Friendica\Database\Database;
use Friendica\Util\Strings;
use LightOpenID;
use Psr\Log\LoggerInterface;
/**
* Performs an login with OpenID
*/
class OpenID extends BaseModule
{
public static function content(array $parameters = [])
{
/** @var Configuration $config */
$config = self::getClass(Configuration::class);
/** @var BaseURL $baseUrl */
$baseUrl = self::getClass(BaseURL::class);
if ($config->get('system', 'no_openid')) {
$baseUrl->redirect();
}
/** @var LoggerInterface $logger */
$logger = self::getClass(LoggerInterface::class);
$logger->debug('mod_openid.', ['request' => $_REQUEST]);
/** @var ISession $session */
$session = self::getClass(ISession::class);
if (!empty($_GET['openid_mode']) && !empty($session->get('openid'))) {
$openid = new LightOpenID($baseUrl->getHostname());
/** @var L10n $l10n */
$l10n = self::getClass(L10n::class);
if ($openid->validate()) {
$authId = $openid->data['openid_identity'];
if (empty($authId)) {
$logger->info($l10n->t('OpenID protocol error. No ID returned'));
$baseUrl->redirect();
}
// NOTE: we search both for normalised and non-normalised form of $authid
// because the normalization step was removed from setting
// mod/settings.php in 8367cad so it might have left mixed
// records in the user table
//
$condition = ['blocked' => false, 'account_expired' => false, 'account_removed' => false, 'verified' => true,
'openid' => [$authId, Strings::normaliseOpenID($authId)]];
$dba = self::getClass(Database::class);
$user = $dba->selectFirst('user', [], $condition);
if ($dba->isResult($user)) {
// successful OpenID login
$session->remove('openid');
/** @var Authentication $auth */
$auth = self::getClass(Authentication::class);
$auth->setForUser(self::getApp(), $user, true, true);
// just in case there was no return url set
// and we fell through
$baseUrl->redirect();
}
// 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($baseUrl->getHostName());
$open_id_obj->identity = $authId;
$session->set('openid_server', $open_id_obj->discover($open_id_obj->identity));
if (intval($config->get('config', 'register_policy')) === \Friendica\Module\Register::CLOSED) {
notice($l10n->t('Account not found. Please login to your existing account to add the OpenID to it.'));
} else {
notice($l10n->t('Account not found. Please register a new account or login to your existing account to add the OpenID to it.'));
}
$baseUrl->redirect('login');
}
}
}
}

View file

@ -1,6 +1,6 @@
<?php <?php
namespace Friendica\Module\TwoFactor; namespace Friendica\Module\Security\TwoFactor;
use Friendica\BaseModule; use Friendica\BaseModule;
use Friendica\App\Authentication; use Friendica\App\Authentication;

View file

@ -1,6 +1,6 @@
<?php <?php
namespace Friendica\Module\TwoFactor; namespace Friendica\Module\Security\TwoFactor;
use Friendica\BaseModule; use Friendica\BaseModule;
use Friendica\App\Authentication; use Friendica\App\Authentication;

View file

@ -9,7 +9,7 @@ use Friendica\Core\PConfig;
use Friendica\Core\Renderer; use Friendica\Core\Renderer;
use Friendica\Model\TwoFactor\AppSpecificPassword; use Friendica\Model\TwoFactor\AppSpecificPassword;
use Friendica\Module\BaseSettingsModule; use Friendica\Module\BaseSettingsModule;
use Friendica\Module\Login; use Friendica\Module\Security\Login;
/** /**
* // Page 5: 2FA enabled, app-specific password generation * // Page 5: 2FA enabled, app-specific password generation

View file

@ -12,7 +12,7 @@ use Friendica\Model\TwoFactor\AppSpecificPassword;
use Friendica\Model\TwoFactor\RecoveryCode; use Friendica\Model\TwoFactor\RecoveryCode;
use Friendica\Model\User; use Friendica\Model\User;
use Friendica\Module\BaseSettingsModule; use Friendica\Module\BaseSettingsModule;
use Friendica\Module\Login; use Friendica\Module\Security\Login;
use PragmaRX\Google2FA\Google2FA; use PragmaRX\Google2FA\Google2FA;
class Index extends BaseSettingsModule class Index extends BaseSettingsModule

View file

@ -9,7 +9,7 @@ use Friendica\Core\PConfig;
use Friendica\Core\Renderer; use Friendica\Core\Renderer;
use Friendica\Model\TwoFactor\RecoveryCode; use Friendica\Model\TwoFactor\RecoveryCode;
use Friendica\Module\BaseSettingsModule; use Friendica\Module\BaseSettingsModule;
use Friendica\Module\Login; use Friendica\Module\Security\Login;
/** /**
* // Page 3: 2FA enabled but not verified, show recovery codes * // Page 3: 2FA enabled but not verified, show recovery codes

View file

@ -14,7 +14,7 @@ use Friendica\Core\PConfig;
use Friendica\Core\Renderer; use Friendica\Core\Renderer;
use Friendica\Core\Session; use Friendica\Core\Session;
use Friendica\Module\BaseSettingsModule; use Friendica\Module\BaseSettingsModule;
use Friendica\Module\Login; use Friendica\Module\Security\Login;
use PragmaRX\Google2FA\Google2FA; use PragmaRX\Google2FA\Google2FA;
/** /**

View file

@ -23,8 +23,8 @@ return [
], ],
'/2fa' => [ '/2fa' => [
'[/]' => [Module\TwoFactor\Verify::class, [R::GET, R::POST]], '[/]' => [Module\Security\TwoFactor\Verify::class, [R::GET, R::POST]],
'/recovery' => [Module\TwoFactor\Recovery::class, [R::GET, R::POST]], '/recovery' => [Module\Security\TwoFactor\Recovery::class, [R::GET, R::POST]],
], ],
'/api' => [ '/api' => [
@ -156,8 +156,8 @@ return [
'/like/{item:\d+}' => [Module\Like::class, [R::GET]], '/like/{item:\d+}' => [Module\Like::class, [R::GET]],
'/localtime' => [Module\Debug\Localtime::class, [R::GET, R::POST]], '/localtime' => [Module\Debug\Localtime::class, [R::GET, R::POST]],
'/login' => [Module\Login::class, [R::GET, R::POST]], '/login' => [Module\Security\Login::class, [R::GET, R::POST]],
'/logout' => [Module\Logout::class, [R::GET, R::POST]], '/logout' => [Module\Security\Logout::class, [R::GET, R::POST]],
'/magic' => [Module\Magic::class, [R::GET]], '/magic' => [Module\Magic::class, [R::GET]],
'/maintenance' => [Module\Maintenance::class, [R::GET]], '/maintenance' => [Module\Maintenance::class, [R::GET]],
'/manifest' => [Module\Manifest::class, [R::GET]], '/manifest' => [Module\Manifest::class, [R::GET]],
@ -180,6 +180,7 @@ return [
], ],
'/outbox/{owner}' => [Module\Outbox::class, [R::GET]], '/outbox/{owner}' => [Module\Outbox::class, [R::GET]],
'/owa' => [Module\Owa::class, [R::GET]], '/owa' => [Module\Owa::class, [R::GET]],
'/openid' => [Module\Security\OpenID::class, [R::GET]],
'/opensearch' => [Module\OpenSearch::class, [R::GET]], '/opensearch' => [Module\OpenSearch::class, [R::GET]],
'/photo' => [ '/photo' => [