webauthn/src/Modules/Application/Component/d3_webauthn_UserComponent.php

209 lines
7.1 KiB
PHP
Raw Normal View History

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-31 00:11:06 +01:00
use Assert\AssertionFailedException;
2022-11-03 13:18:02 +01:00
use D3\Webauthn\Application\Model\Exceptions\WebauthnGetException;
2022-10-26 22:27:25 +02:00
use D3\Webauthn\Application\Model\WebauthnConf;
use D3\Webauthn\Application\Model\Webauthn;
2022-11-03 13:18:02 +01:00
use D3\Webauthn\Application\Model\Exceptions\WebauthnException;
2022-10-31 23:17:04 +01:00
use D3\Webauthn\Modules\Application\Model\d3_User_Webauthn;
2022-10-31 00:11:06 +01:00
use Doctrine\DBAL\Driver\Exception as DoctrineDriverException;
2022-10-29 00:19:34 +02:00
use Doctrine\DBAL\Exception;
2022-10-24 22:24:40 +02:00
use OxidEsales\Eshop\Application\Model\User;
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Eshop\Core\Session;
use OxidEsales\Eshop\Core\UtilsView;
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
2022-10-31 00:11:06 +01:00
* @throws ContainerExceptionInterface
* @throws Exception
* @throws NotFoundExceptionInterface
* @throws DoctrineDriverException
2022-10-24 22:24:40 +02:00
*/
public function login_noredirect()
{
2022-10-26 22:27:25 +02:00
$lgn_user = Registry::getRequest()->getRequestParameter('lgn_usr');
2022-11-02 16:38:43 +01:00
$password = Registry::getRequest()->getRequestParameter('lgn_pwd');
2022-10-31 23:17:04 +01:00
/** @var d3_User_Webauthn $user */
$user = oxNew(User::class);
$userId = $user->d3GetLoginUserId($lgn_user);
2022-10-26 22:27:25 +02:00
2022-11-01 21:43:55 +01:00
if ($lgn_user && $userId && !strlen(trim($password))) {
2022-10-24 22:24:40 +02:00
$webauthn = $this->d3GetWebauthnObject();
2022-10-26 22:27:25 +02:00
if ($webauthn->isActive($userId)
2022-10-31 00:11:06 +01:00
&& !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-31 00:11:06 +01:00
Registry::getSession()->setVariable(
WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER,
$userId
);
Registry::getSession()->setVariable(
WebauthnConf::WEBAUTHN_SESSION_NAVPARAMS,
$this->getParent()->getNavigationParams()
);
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()
);
return "d3webauthnlogin";
}
}
parent::login_noredirect();
}
/**
2022-10-26 22:27:25 +02:00
* @return Webauthn
2022-10-24 22:24:40 +02:00
*/
2022-10-31 00:11:06 +01:00
public function d3GetWebauthnObject(): Webauthn
2022-10-24 22:24:40 +02:00
{
2022-10-26 22:27:25 +02:00
return oxNew(Webauthn::class);
2022-10-24 22:24:40 +02:00
}
/**
* @return UtilsView
*/
2022-10-31 00:11:06 +01:00
public function d3GetUtilsView(): UtilsView
2022-10-24 22:24:40 +02:00
{
return Registry::getUtilsView();
}
2022-10-31 23:17:04 +01:00
public function cancelWebauthnLogin(): bool
2022-10-24 22:24:40 +02:00
{
$this->d3WebauthnClearSessionVariables();
return false;
}
/**
2022-10-30 00:27:11 +02:00
* @param Webauthn $webauthn
2022-10-31 00:11:06 +01:00
* @param $userId
2022-10-24 22:24:40 +02:00
* @return bool
2022-10-31 00:11:06 +01:00
* @throws ContainerExceptionInterface
* @throws DoctrineDriverException
* @throws Exception
* @throws NotFoundExceptionInterface
2022-10-24 22:24:40 +02:00
*/
2022-10-31 00:11:06 +01:00
public function isNoWebauthnOrNoLogin(Webauthn $webauthn, $userId): bool
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
*/
2022-10-31 00:11:06 +01:00
public function hasValidWebauthn(string $sWebauth, Webauthn $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)
);
2022-11-03 13:18:02 +01:00
} catch (WebauthnException $e) {
2022-10-30 00:27:11 +02:00
return false;
}
2022-10-24 22:24:40 +02:00
}
/**
2022-10-31 23:17:04 +01:00
* @param User $user
2022-10-24 22:24:40 +02:00
* @param $sWebauthn
*/
2022-10-31 23:17:04 +01:00
public function d3WebauthnRelogin(User $user, $sWebauthn)
2022-10-24 22:24:40 +02:00
{
2022-10-31 23:17:04 +01:00
$setSessionCookie = Registry::getRequest()->getRequestParameter('lgn_cook');
2022-10-26 22:27:25 +02:00
$this->d3GetSession()->setVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH, $sWebauthn);
2022-10-31 23:17:04 +01:00
$this->d3GetSession()->setVariable('usr', $user->getId());
2022-10-24 22:24:40 +02:00
$this->setUser(null);
$this->setLoginStatus(USER_LOGIN_SUCCESS);
2022-10-31 23:17:04 +01:00
// cookie must be set ?
if ($setSessionCookie && Registry::getConfig()->getConfigParam('blShowRememberMe')) {
Registry::getUtilsServer()->setUserCookie(
$user->oxuser__oxusername->value,
$user->oxuser__oxpassword->value,
Registry::getConfig()->getShopId(),
31536000,
User::USER_COOKIE_SALT
);
}
$this->_afterLogin($user);
2022-10-24 22:24:40 +02:00
}
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-31 00:11:06 +01:00
$this->d3GetSession()->deleteVariable(WebauthnConf::WEBAUTHN_LOGIN_OBJECT);
2022-10-24 22:24:40 +02:00
}
/**
* @return Session
*/
2022-10-31 00:11:06 +01:00
public function d3GetSession(): Session
2022-10-24 22:24:40 +02:00
{
return Registry::getSession();
}
public function d3AssertAuthn()
{
/** @var d3_User_Webauthn $user */
$user = oxNew(User::class);
try {
if (strlen(Registry::getRequest()->getRequestEscapedParameter('error'))) {
2022-11-03 13:18:02 +01:00
/** @var WebauthnGetException $e */
$e = oxNew(
WebauthnGetException::class,
Registry::getRequest()->getRequestEscapedParameter('error')
);
2022-11-03 13:18:02 +01:00
throw $e;
}
if (strlen(Registry::getRequest()->getRequestEscapedParameter('credential'))) {
$credential = Registry::getRequest()->getRequestEscapedParameter('credential');
2022-11-03 13:18:02 +01:00
$webAuthn = oxNew( Webauthn::class );
$webAuthn->assertAuthn( $credential );
$user->load(Registry::getSession()->getVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER));
$this->d3WebauthnRelogin($user, $credential);
}
2022-11-03 13:18:02 +01:00
} catch (WebauthnException $e) {
Registry::getUtilsView()->addErrorToDisplay($e);
Registry::getLogger()->error(
2022-11-04 00:12:42 +01:00
$e->getDetailedErrorMessage(),
2022-11-03 13:18:02 +01:00
['UserId' => Registry::getSession()->getVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER)]
);
2022-11-04 00:05:52 +01:00
Registry::getLogger()->debug($e->getTraceAsString());
$user->logout();
}
}
2022-10-24 22:24:40 +02:00
}