webauthn/src/Application/Controller/d3webauthnlogin.php

161 regels
5.2 KiB
PHP

2022-10-24 22:24:40 +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-24 22:24:40 +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-24 22:24:40 +02:00
*/
2022-11-04 22:02:44 +01:00
declare(strict_types=1);
2022-10-24 22:24:40 +02:00
namespace D3\Webauthn\Application\Controller;
2022-11-17 00:27:43 +01:00
use D3\TestingTools\Production\IsMockable;
use D3\Webauthn\Application\Model\Webauthn;
2022-10-26 22:27:25 +02:00
use D3\Webauthn\Application\Model\WebauthnConf;
2022-11-03 13:18:02 +01:00
use D3\Webauthn\Application\Model\Exceptions\WebauthnException;
2022-10-31 23:17:04 +01:00
use Doctrine\DBAL\Driver\Exception as DoctrineDriverException;
2022-10-31 00:11:06 +01:00
use Doctrine\DBAL\Exception as DoctrineException;
2022-10-24 22:24:40 +02:00
use OxidEsales\Eshop\Application\Controller\FrontendController;
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Eshop\Core\Routing\ControllerClassNameResolver;
use OxidEsales\Eshop\Core\Session;
2022-10-24 22:24:40 +02:00
use OxidEsales\Eshop\Core\Utils;
2022-10-31 00:11:06 +01:00
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
2023-01-21 13:50:18 +01:00
use Psr\Log\LoggerInterface;
2022-10-24 22:24:40 +02:00
class d3webauthnlogin extends FrontendController
{
2022-11-17 00:27:43 +01:00
use IsMockable;
2022-10-24 22:24:40 +02:00
protected $_sThisTemplate = 'd3webauthnlogin.tpl';
2022-11-04 22:02:44 +01:00
/**
* @return array
*/
public function getNavigationParams(): array
{
2023-01-21 13:50:18 +01:00
$navparams = d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)->getVariable(
WebauthnConf::WEBAUTHN_SESSION_NAVPARAMS
);
return array_merge(
$this->d3CallMockableFunction([FrontendController::class, 'getNavigationParams']),
$navparams,
['cl' => $navparams['actcontrol']]
);
}
2022-10-24 22:24:40 +02:00
/**
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-24 22:24:40 +02:00
*/
2022-11-04 22:02:44 +01:00
public function render(): string
2022-10-24 22:24:40 +02:00
{
2023-01-21 13:50:18 +01:00
if (d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->hasVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH) ||
2023-01-21 13:50:18 +01:00
!d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->hasVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER)
2022-10-24 22:24:40 +02:00
) {
2023-01-21 13:50:18 +01:00
d3GetOxidDIC()->get('d3ox.webauthn.'.Utils::class)->redirect('index.php?cl=start');
2022-10-24 22:24:40 +02:00
}
$this->generateCredentialRequest();
2023-01-21 13:50:18 +01:00
$this->addTplParam('navFormParams', d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->getVariable(WebauthnConf::WEBAUTHN_SESSION_NAVFORMPARAMS));
2022-10-24 22:24:40 +02:00
return $this->d3CallMockableFunction([FrontendController::class, 'render']);
2022-10-24 22:24:40 +02:00
}
2022-10-31 00:11:06 +01:00
/**
* @return void
2022-10-31 23:17:04 +01:00
* @throws DoctrineDriverException
2022-10-31 00:11:06 +01:00
* @throws DoctrineException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
2022-11-04 22:02:44 +01:00
public function generateCredentialRequest(): void
2022-10-24 22:24:40 +02:00
{
2023-01-21 13:50:18 +01:00
$userId = d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->getVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER);
2022-11-03 13:18:02 +01:00
2022-10-31 00:11:06 +01:00
try {
/** @var Webauthn $webauthn */
2023-01-21 13:50:18 +01:00
$webauthn = d3GetOxidDIC()->get(Webauthn::class);
2022-10-31 00:11:06 +01:00
$publicKeyCredentialRequestOptions = $webauthn->getRequestOptions($userId);
$this->addTplParam('webauthn_publickey_login', $publicKeyCredentialRequestOptions);
$this->addTplParam('isAdmin', isAdmin());
2022-10-31 00:11:06 +01:00
} catch (WebauthnException $e) {
2023-01-21 13:50:18 +01:00
d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->setVariable(WebauthnConf::GLOBAL_SWITCH, true);
2023-01-21 13:50:18 +01:00
d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->error($e->getDetailedErrorMessage(), ['UserId' => $userId]);
d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->debug($e->getTraceAsString());
2022-11-03 13:18:02 +01:00
Registry::getUtilsView()->addErrorToDisplay($e);
2023-01-21 13:50:18 +01:00
d3GetOxidDIC()->get('d3ox.webauthn.'.Utils::class)->redirect('index.php?cl=start');
2022-10-31 00:11:06 +01:00
}
}
2022-11-04 22:02:44 +01:00
/**
* @return string|null
*/
2022-11-27 01:02:23 +01:00
public function d3GetPreviousClass(): ?string
2022-10-24 22:24:40 +02:00
{
2023-01-21 13:50:18 +01:00
return d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->getVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTCLASS);
2022-10-24 22:24:40 +02:00
}
2022-11-04 22:02:44 +01:00
/**
* @return bool
*/
2022-10-30 00:27:11 +02:00
public function previousClassIsOrderStep(): bool
2022-10-24 22:24:40 +02:00
{
2022-11-27 01:02:23 +01:00
$sClassKey = $this->d3GetPreviousClass();
2023-01-21 13:50:18 +01:00
$resolvedClass = d3GetOxidDIC()->get('d3ox.webauthn.'.ControllerClassNameResolver::class)
->getClassNameById($sClassKey);
2022-10-30 00:27:11 +02:00
$resolvedClass = $resolvedClass ?: 'start';
2022-10-24 22:24:40 +02:00
/** @var FrontendController $oController */
$oController = oxNew($resolvedClass);
2022-11-17 00:27:43 +01:00
2022-10-24 22:24:40 +02:00
return $oController->getIsOrderStep();
}
/**
* @return bool
*/
2022-10-30 00:27:11 +02:00
public function getIsOrderStep(): bool
2022-10-24 22:24:40 +02:00
{
return $this->previousClassIsOrderStep();
}
/**
* @return array
*/
2022-10-30 00:27:11 +02:00
public function getBreadCrumb(): array
2022-10-24 22:24:40 +02:00
{
$aPaths = [];
$aPath = [];
$iBaseLanguage = Registry::getLang()->getBaseLanguage();
2022-12-12 23:41:07 +01:00
$aPath['title'] = Registry::getLang()->translateString(
'D3_WEBAUTHN_BREADCRUMB',
(int) $iBaseLanguage,
false
);
2022-10-24 22:24:40 +02:00
$aPath['link'] = $this->getLink();
$aPaths[] = $aPath;
return $aPaths;
}
2022-12-13 22:24:33 +01:00
}