2022-10-24 22:24:40 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This Software is the property of Data Development and is protected
|
|
|
|
* by copyright law - it is NOT Freeware.
|
|
|
|
* Any unauthorized use of this software without a valid license
|
|
|
|
* is a violation of the license agreement and will be prosecuted by
|
|
|
|
* civil and criminal law.
|
|
|
|
* http://www.shopmodule.com
|
|
|
|
*
|
|
|
|
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
|
|
|
|
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
|
|
|
|
* @link http://www.oxidmodule.com
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace D3\Webauthn\Modules\Application\Component;
|
|
|
|
|
2022-10-26 22:27:25 +02:00
|
|
|
use D3\Webauthn\Application\Model\WebauthnConf;
|
2022-10-24 22:24:40 +02:00
|
|
|
use D3\Webauthn\Application\Model\Exceptions\d3webauthnMissingPublicKeyCredentialRequestOptions;
|
|
|
|
use D3\Webauthn\Application\Model\Exceptions\d3webauthnWrongAuthException;
|
2022-10-26 22:27:25 +02:00
|
|
|
use D3\Webauthn\Application\Model\Webauthn;
|
2022-10-24 22:24:40 +02:00
|
|
|
use D3\Webauthn\Modules\Application\Model\d3_User_Webauthn;
|
|
|
|
use Doctrine\DBAL\DBALException;
|
2022-10-29 00:19:34 +02:00
|
|
|
use Doctrine\DBAL\Exception;
|
2022-10-26 22:27:25 +02:00
|
|
|
use Doctrine\DBAL\Query\QueryBuilder;
|
2022-10-24 22:24:40 +02:00
|
|
|
use OxidEsales\Eshop\Application\Model\User;
|
|
|
|
use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException;
|
|
|
|
use OxidEsales\Eshop\Core\Registry;
|
|
|
|
use OxidEsales\Eshop\Core\Session;
|
|
|
|
use OxidEsales\Eshop\Core\UtilsView;
|
2022-10-26 22:27:25 +02:00
|
|
|
use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory;
|
|
|
|
use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactoryInterface;
|
2022-10-29 00:19:34 +02:00
|
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
|
|
use Psr\Container\NotFoundExceptionInterface;
|
2022-10-24 22:24:40 +02:00
|
|
|
|
|
|
|
class d3_webauthn_UserComponent extends d3_webauthn_UserComponent_parent
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @return string|void
|
|
|
|
* @throws DBALException
|
|
|
|
* @throws DatabaseConnectionException
|
|
|
|
*/
|
|
|
|
public function login_noredirect()
|
|
|
|
{
|
2022-10-26 22:27:25 +02:00
|
|
|
$lgn_user = Registry::getRequest()->getRequestParameter('lgn_usr');
|
2022-10-29 00:19:34 +02:00
|
|
|
$userId = $this->d3GetLoginUserId($lgn_user);
|
2022-10-26 22:27:25 +02:00
|
|
|
|
2022-10-28 00:45:32 +02:00
|
|
|
if ($lgn_user && $userId) {
|
2022-10-24 22:24:40 +02:00
|
|
|
$webauthn = $this->d3GetWebauthnObject();
|
2022-10-28 00:45:32 +02:00
|
|
|
|
2022-10-26 22:27:25 +02:00
|
|
|
if ($webauthn->isActive($userId)
|
|
|
|
&& false == Registry::getSession()->getVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH)
|
2022-10-24 22:24:40 +02:00
|
|
|
) {
|
|
|
|
Registry::getSession()->setVariable(
|
2022-10-26 22:27:25 +02:00
|
|
|
WebauthnConf::WEBAUTHN_SESSION_CURRENTCLASS,
|
2022-10-24 22:24:40 +02:00
|
|
|
$this->getParent()->getClassKey() != 'd3webauthnlogin' ? $this->getParent()->getClassKey() : 'start');
|
2022-10-28 00:45:32 +02:00
|
|
|
Registry::getSession()->setVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER, $userId);
|
2022-10-24 22:24:40 +02:00
|
|
|
Registry::getSession()->setVariable(
|
2022-10-26 22:27:25 +02:00
|
|
|
WebauthnConf::WEBAUTHN_SESSION_NAVFORMPARAMS,
|
2022-10-24 22:24:40 +02:00
|
|
|
$this->getParent()->getViewConfig()->getNavFormParams()
|
|
|
|
);
|
|
|
|
|
|
|
|
//$oUser->d3templogout();
|
|
|
|
|
|
|
|
return "d3webauthnlogin";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::login_noredirect();
|
|
|
|
|
2022-10-26 22:27:25 +02:00
|
|
|
/** @var d3_User_Webauthn $user */
|
2022-10-24 22:24:40 +02:00
|
|
|
/*
|
|
|
|
$oUser = $this->getUser();
|
|
|
|
|
|
|
|
if ($oUser && $oUser->getId()) {
|
|
|
|
$webauthn = $this->d3GetWebauthnObject();
|
|
|
|
$webauthn->loadByUserId($oUser->getId());
|
|
|
|
|
|
|
|
if ($webauthn->isActive()
|
2022-10-26 22:27:25 +02:00
|
|
|
&& false == Registry::getSession()->getVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH)
|
2022-10-24 22:24:40 +02:00
|
|
|
) {
|
|
|
|
Registry::getSession()->setVariable(
|
2022-10-26 22:27:25 +02:00
|
|
|
WebauthnConf::WEBAUTHN_SESSION_CURRENTCLASS,
|
2022-10-24 22:24:40 +02:00
|
|
|
$this->getParent()->getClassKey() != 'd3webauthnlogin' ? $this->getParent()->getClassKey() : 'start');
|
2022-10-26 22:27:25 +02:00
|
|
|
Registry::getSession()->setVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER, $oUser->getId());
|
2022-10-24 22:24:40 +02:00
|
|
|
Registry::getSession()->setVariable(
|
2022-10-26 22:27:25 +02:00
|
|
|
WebauthnConf::WEBAUTHN_SESSION_NAVFORMPARAMS,
|
2022-10-24 22:24:40 +02:00
|
|
|
$this->getParent()->getViewConfig()->getNavFormParams()
|
|
|
|
);
|
|
|
|
|
|
|
|
$oUser->d3templogout();
|
|
|
|
|
|
|
|
return "d3webauthnlogin";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-10-26 22:27:25 +02:00
|
|
|
* @return Webauthn
|
2022-10-24 22:24:40 +02:00
|
|
|
*/
|
|
|
|
public function d3GetWebauthnObject()
|
|
|
|
{
|
2022-10-26 22:27:25 +02:00
|
|
|
return oxNew(Webauthn::class);
|
2022-10-24 22:24:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool|string
|
|
|
|
* @throws DatabaseConnectionException
|
|
|
|
* @throws d3webauthnMissingPublicKeyCredentialRequestOptions
|
|
|
|
*/
|
|
|
|
public function checkWebauthnlogin()
|
|
|
|
{
|
|
|
|
$sWebauth = base64_decode(Registry::getRequest()->getRequestParameter('keyauth'));
|
|
|
|
|
2022-10-30 00:27:11 +02:00
|
|
|
$userId = Registry::getSession()->getVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER);
|
2022-10-24 22:24:40 +02:00
|
|
|
$oUser = oxNew(User::class);
|
2022-10-30 00:27:11 +02:00
|
|
|
$oUser->load($userId);
|
2022-10-24 22:24:40 +02:00
|
|
|
|
|
|
|
$webauthn = $this->d3GetWebauthnObject();
|
|
|
|
|
|
|
|
try {
|
2022-10-30 00:27:11 +02:00
|
|
|
if (false == $this->isNoWebauthnOrNoLogin($webauthn, $userId) && $this->hasValidWebauthn($sWebauth, $webauthn)) {
|
2022-10-24 22:24:40 +02:00
|
|
|
$this->d3WebauthnRelogin($oUser, $sWebauth);
|
|
|
|
$this->d3WebauthnClearSessionVariables();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch (d3webauthnWrongAuthException $oEx) {
|
|
|
|
$this->d3GetUtilsView()->addErrorToDisplay($oEx, false, false, "", 'd3webauthnlogin');
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'd3webauthnlogin';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return UtilsView
|
|
|
|
*/
|
|
|
|
public function d3GetUtilsView()
|
|
|
|
{
|
|
|
|
return Registry::getUtilsView();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function cancelWebauthnLogin()
|
|
|
|
{
|
|
|
|
$this->d3WebauthnClearSessionVariables();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-10-30 00:27:11 +02:00
|
|
|
* @param Webauthn $webauthn
|
2022-10-24 22:24:40 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
2022-10-30 00:27:11 +02:00
|
|
|
public function isNoWebauthnOrNoLogin($webauthn, $userId)
|
2022-10-24 22:24:40 +02:00
|
|
|
{
|
|
|
|
return false == $this->d3GetSession()->getVariable("auth")
|
2022-10-30 00:27:11 +02:00
|
|
|
|| false == $webauthn->isActive($userId);
|
2022-10-24 22:24:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $sWebauth
|
2022-10-30 00:27:11 +02:00
|
|
|
* @param Webauthn $webauthn
|
2022-10-24 22:24:40 +02:00
|
|
|
* @return bool
|
|
|
|
* @throws d3webauthnMissingPublicKeyCredentialRequestOptions
|
|
|
|
* @throws d3webauthnWrongAuthException
|
|
|
|
*/
|
2022-10-30 00:27:11 +02:00
|
|
|
public function hasValidWebauthn($sWebauth, $webauthn): bool
|
2022-10-24 22:24:40 +02:00
|
|
|
{
|
2022-10-30 00:27:11 +02:00
|
|
|
try {
|
|
|
|
return Registry::getSession()->getVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH) ||
|
|
|
|
(
|
|
|
|
$sWebauth && $webauthn->assertAuthn($sWebauth)
|
|
|
|
);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-10-24 22:24:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param User $oUser
|
|
|
|
* @param $sWebauthn
|
|
|
|
*/
|
|
|
|
public function d3WebauthnRelogin(User $oUser, $sWebauthn)
|
|
|
|
{
|
2022-10-26 22:27:25 +02:00
|
|
|
$this->d3GetSession()->setVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH, $sWebauthn);
|
2022-10-24 22:24:40 +02:00
|
|
|
$this->d3GetSession()->setVariable('usr', $oUser->getId());
|
|
|
|
$this->setUser(null);
|
|
|
|
$this->setLoginStatus(USER_LOGIN_SUCCESS);
|
|
|
|
$this->_afterLogin($oUser);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function d3WebauthnClearSessionVariables()
|
|
|
|
{
|
2022-10-26 22:27:25 +02:00
|
|
|
$this->d3GetSession()->deleteVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTCLASS);
|
|
|
|
$this->d3GetSession()->deleteVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER);
|
|
|
|
$this->d3GetSession()->deleteVariable(WebauthnConf::WEBAUTHN_SESSION_NAVFORMPARAMS);
|
2022-10-24 22:24:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Session
|
|
|
|
*/
|
|
|
|
public function d3GetSession()
|
|
|
|
{
|
|
|
|
return Registry::getSession();
|
|
|
|
}
|
2022-10-29 00:19:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string|null
|
|
|
|
* @throws \Doctrine\DBAL\Driver\Exception
|
|
|
|
* @throws Exception
|
|
|
|
* @throws ContainerExceptionInterface
|
|
|
|
* @throws NotFoundExceptionInterface
|
|
|
|
*/
|
|
|
|
public function d3GetLoginUserId($username): ?string
|
|
|
|
{
|
|
|
|
if (empty($username)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$user = oxNew(User::class);
|
|
|
|
|
|
|
|
/** @var QueryBuilder $qb */
|
|
|
|
$qb = ContainerFactory::getInstance()->getContainer()->get(QueryBuilderFactoryInterface::class)->create();
|
|
|
|
$qb->select('oxid')
|
|
|
|
->from($user->getViewName())
|
|
|
|
->where(
|
|
|
|
$qb->expr()->and(
|
|
|
|
$qb->expr()->eq(
|
|
|
|
'oxusername',
|
|
|
|
$qb->createNamedParameter($username)
|
|
|
|
),
|
|
|
|
$qb->expr()->eq(
|
|
|
|
'oxshopid',
|
|
|
|
$qb->createNamedParameter(Registry::getConfig()->getShopId())
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)->setMaxResults(1);
|
|
|
|
|
|
|
|
return $qb->execute()->fetchOne();
|
|
|
|
}
|
2022-10-24 22:24:40 +02:00
|
|
|
}
|