webauthn/src/Application/Controller/Admin/d3webauthnadminlogin.php

193 lines
6.4 KiB
PHP
Raw Normal View History

2022-10-29 00:19:34 +02:00
<?php
/**
2022-11-04 22:45:47 +01:00
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* https://www.d3data.de
2022-10-29 00:19:34 +02:00
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
2022-11-04 22:45:47 +01:00
* @author D3 Data Development - Daniel Seifert <info@shopmodule.com>
* @link https://www.oxidmodule.com
2022-10-29 00:19:34 +02:00
*/
2022-11-04 22:02:44 +01:00
declare(strict_types=1);
2022-10-29 00:19:34 +02:00
namespace D3\Webauthn\Application\Controller\Admin;
2022-11-03 13:18:02 +01:00
use D3\Webauthn\Application\Model\Exceptions\WebauthnGetException;
2022-10-29 00:19:34 +02:00
use D3\Webauthn\Application\Model\Webauthn;
use D3\Webauthn\Application\Model\WebauthnConf;
2022-11-03 13:18:02 +01:00
use D3\Webauthn\Application\Model\Exceptions\WebauthnException;
2022-10-29 00:19:34 +02:00
use D3\Webauthn\Modules\Application\Component\d3_webauthn_UserComponent;
use D3\Webauthn\Modules\Application\Model\d3_User_Webauthn;
2022-10-31 23:17:04 +01:00
use Doctrine\DBAL\Driver\Exception as DoctrineDriverException;
use Doctrine\DBAL\Exception as DoctrineException;
2022-10-29 00:19:34 +02:00
use OxidEsales\Eshop\Application\Controller\Admin\AdminController;
use OxidEsales\Eshop\Application\Controller\Admin\LoginController;
use OxidEsales\Eshop\Application\Controller\FrontendController;
use OxidEsales\Eshop\Application\Model\User;
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Eshop\Core\Utils;
2022-10-31 23:17:04 +01:00
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
2022-10-29 00:19:34 +02:00
class d3webauthnadminlogin extends AdminController
{
protected $_sThisTemplate = 'd3webauthnadminlogin.tpl';
2022-11-04 22:02:44 +01:00
/**
* @return bool
*/
2022-10-30 00:27:11 +02:00
protected function _authorize(): bool
2022-10-29 00:19:34 +02:00
{
return true;
}
/**
2022-11-04 22:02:44 +01:00
* @return string
2022-10-31 23:17:04 +01:00
* @throws ContainerExceptionInterface
* @throws DoctrineDriverException
* @throws DoctrineException
* @throws NotFoundExceptionInterface
2022-10-29 00:19:34 +02:00
*/
2022-11-04 22:02:44 +01:00
public function render(): string
2022-10-29 00:19:34 +02:00
{
if (Registry::getSession()->hasVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH) ||
2022-10-30 00:27:11 +02:00
!Registry::getSession()->hasVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER)
2022-10-29 00:19:34 +02:00
) {
$this->getUtils()->redirect('index.php?cl=admin_start');
2022-10-30 00:27:11 +02:00
if (!defined('OXID_PHP_UNIT')) {
2022-10-29 00:19:34 +02:00
// @codeCoverageIgnoreStart
exit;
// @codeCoverageIgnoreEnd
}
}
$this->generateCredentialRequest();
return parent::render();
}
2022-10-31 23:17:04 +01:00
/**
* @return void
* @throws DoctrineDriverException
* @throws DoctrineException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
2022-11-04 22:02:44 +01:00
public function generateCredentialRequest(): void
2022-10-29 00:19:34 +02:00
{
2022-11-03 13:18:02 +01:00
$userId = Registry::getSession()->getVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER);
2022-10-31 00:11:06 +01:00
try {
/** @var Webauthn $webauthn */
$webauthn = oxNew(Webauthn::class);
$publicKeyCredentialRequestOptions = $webauthn->getRequestOptions($userId);
Registry::getSession()->setVariable(WebauthnConf::WEBAUTHN_LOGIN_OBJECT, $publicKeyCredentialRequestOptions);
$this->addTplParam('webauthn_publickey_login', $publicKeyCredentialRequestOptions);
$this->addTplParam('isAdmin', isAdmin());
} catch (WebauthnException $e) {
2022-10-31 23:17:04 +01:00
Registry::getSession()->setVariable(WebauthnConf::GLOBAL_SWITCH, true);
2022-11-03 13:18:02 +01:00
Registry::getUtilsView()->addErrorToDisplay($e);
2022-11-04 00:12:42 +01:00
Registry::getLogger()->error($e->getDetailedErrorMessage(), ['UserId' => $userId]);
2022-11-04 00:05:52 +01:00
Registry::getLogger()->debug($e->getTraceAsString());
2022-10-31 23:17:04 +01:00
$this->getUtils()->redirect('index.php?cl=login');
2022-10-31 00:11:06 +01:00
}
2022-10-29 00:19:34 +02:00
}
2022-11-04 22:02:44 +01:00
/**
* @return string|null
*/
public function d3AssertAuthn(): ?string
2022-10-29 00:19:34 +02:00
{
/** @var d3_User_Webauthn $user */
$user = oxNew(User::class);
2022-11-03 13:18:02 +01:00
$userId = Registry::getSession()->getVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER);
2022-10-29 00:19:34 +02:00
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-10-29 00:19:34 +02:00
);
2022-11-03 13:18:02 +01:00
throw $e;
2022-10-29 00:19:34 +02:00
}
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 );
2022-10-31 00:11:06 +01:00
$user->load($userId);
2022-10-29 00:19:34 +02:00
Registry::getSession()->setVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH, true);
/** @var d3_webauthn_UserComponent $userCmp */
$loginController = oxNew(LoginController::class);
return $loginController->checklogin();
}
2022-11-03 13:18:02 +01:00
} catch (WebauthnException $e) {
Registry::getUtilsView()->addErrorToDisplay($e);
2022-11-04 00:12:42 +01:00
Registry::getLogger()->error($e->getDetailedErrorMessage(), ['UserId' => $userId]);
2022-11-04 00:05:52 +01:00
Registry::getLogger()->debug($e->getTraceAsString());
2022-10-29 00:19:34 +02:00
$user->logout();
}
2022-10-30 00:27:11 +02:00
return 'login';
2022-10-29 00:19:34 +02:00
}
/**
* @return Utils
*/
2022-10-30 00:27:11 +02:00
public function getUtils(): Utils
2022-10-29 00:19:34 +02:00
{
return Registry::getUtils();
}
2022-11-04 22:02:44 +01:00
/**
* @return string|null
*/
public function getPreviousClass(): ?string
2022-10-29 00:19:34 +02:00
{
return Registry::getSession()->getVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTCLASS);
}
2022-11-04 22:02:44 +01:00
/**
* @return bool
*/
2022-10-30 00:27:11 +02:00
public function previousClassIsOrderStep(): bool
2022-10-29 00:19:34 +02:00
{
$sClassKey = Registry::getSession()->getVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTCLASS);
$resolvedClass = Registry::getControllerClassNameResolver()->getClassNameById($sClassKey);
2022-10-30 00:27:11 +02:00
$resolvedClass = $resolvedClass ?: 'start';
2022-10-29 00:19:34 +02:00
/** @var FrontendController $oController */
$oController = oxNew($resolvedClass);
return $oController->getIsOrderStep();
}
/**
* @return bool
*/
2022-10-30 00:27:11 +02:00
public function getIsOrderStep(): bool
2022-10-29 00:19:34 +02:00
{
return $this->previousClassIsOrderStep();
}
/**
* Returns Bread Crumb - you are here page1/page2/page3...
*
* @return array
*/
2022-10-30 00:27:11 +02:00
public function getBreadCrumb(): array
2022-10-29 00:19:34 +02:00
{
$aPaths = [];
$aPath = [];
$iBaseLanguage = Registry::getLang()->getBaseLanguage();
$aPath['title'] = Registry::getLang()->translateString('D3_WEBAUTHN_BREADCRUMB', $iBaseLanguage, false);
$aPath['link'] = $this->getLink();
$aPaths[] = $aPath;
return $aPaths;
}
}