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

179 lines
6.6 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;
use D3\TestingTools\Production\IsMockable;
2022-11-03 13:18:02 +01:00
use D3\Webauthn\Application\Model\Exceptions\WebauthnGetException;
use D3\Webauthn\Application\Model\Webauthn;
use D3\Webauthn\Application\Model\WebauthnAfterLogin;
2022-10-29 00:19:34 +02:00
use D3\Webauthn\Application\Model\WebauthnConf;
2022-11-03 13:18:02 +01:00
use D3\Webauthn\Application\Model\Exceptions\WebauthnException;
use D3\Webauthn\Application\Model\WebauthnLogin;
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\FrontendController;
2022-11-27 01:02:23 +01:00
use OxidEsales\Eshop\Core\Request;
use OxidEsales\Eshop\Core\Routing\ControllerClassNameResolver;
use OxidEsales\Eshop\Core\Session;
2022-10-29 00:19:34 +02:00
use OxidEsales\Eshop\Core\Utils;
use OxidEsales\Eshop\Core\UtilsView;
2022-10-31 23:17:04 +01:00
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
2023-01-21 13:50:18 +01:00
use Psr\Log\LoggerInterface;
2022-10-29 00:19:34 +02:00
class d3webauthnadminlogin extends AdminController
{
use IsMockable;
2022-10-29 00:19:34 +02:00
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
{
2023-01-21 13:50:18 +01:00
if (d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->hasVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_AUTH)
) {
2023-01-21 13:50:18 +01:00
d3GetOxidDIC()->get('d3ox.webauthn.'.Utils::class)->redirect('index.php?cl=admin_start');
} elseif (!d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->hasVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTUSER)
) {
2023-01-21 13:50:18 +01:00
d3GetOxidDIC()->get('d3ox.webauthn.'.Utils::class)->redirect('index.php?cl=login');
2022-10-29 00:19:34 +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));
2023-01-21 13:50:18 +01:00
$this->addTplParam('currentProfile', d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->getVariable(WebauthnConf::WEBAUTHN_ADMIN_PROFILE));
2023-01-21 13:50:18 +01:00
d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->deleteVariable(WebauthnConf::WEBAUTHN_ADMIN_PROFILE);
2023-01-21 13:50:18 +01:00
$this->addTplParam('currentChLanguage', d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->getVariable(WebauthnConf::WEBAUTHN_ADMIN_CHLANGUAGE));
2023-01-21 13:50:18 +01:00
$afterLogin = d3GetOxidDIC()->get(WebauthnAfterLogin::class);
$afterLogin->changeLanguage();
return $this->d3CallMockableFunction([AdminController::class, 'render']);
2022-10-29 00:19:34 +02:00
}
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
{
2023-01-21 13:50:18 +01:00
$userId = d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->getVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTUSER);
2022-10-31 00:11:06 +01:00
try {
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);
2023-01-21 13:50:18 +01:00
d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->setVariable(WebauthnConf::WEBAUTHN_ADMIN_LOGIN_OBJECT, $publicKeyCredentialRequestOptions);
2022-10-31 00:11:06 +01:00
$this->addTplParam('webauthn_publickey_login', $publicKeyCredentialRequestOptions);
$this->addTplParam('isAdmin', isAdmin());
} 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.'.UtilsView::class)->addErrorToDisplay($e);
d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->error($e->getDetailedErrorMessage(), ['UserId' => $userId]);
d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->debug($e->getTraceAsString());
d3GetOxidDIC()->get('d3ox.webauthn.'.Utils::class)->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
{
try {
2023-01-21 13:50:18 +01:00
$login = $this->getWebAuthnLogin();
return $login->adminLogin(
2023-01-21 13:50:18 +01:00
d3GetOxidDIC()->get('d3ox.webauthn.'.Request::class)->getRequestEscapedParameter('profile')
);
} catch (WebauthnGetException $e) {
2023-01-21 13:50:18 +01:00
d3GetOxidDIC()->get('d3ox.webauthn.'.UtilsView::class)->addErrorToDisplay($e);
return 'login';
2022-10-29 00:19:34 +02: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-29 00:19:34 +02:00
{
2023-01-21 13:50:18 +01:00
return d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)
->getVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTCLASS);
2022-10-29 00:19:34 +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-29 00:19:34 +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-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();
}
2023-01-21 13:50:18 +01:00
/**
* @return WebauthnLogin
*/
protected function getWebAuthnLogin(): WebauthnLogin
{
/** @var Request $request */
$request = d3GetOxidDIC()->get('d3ox.webauthn.'.Request::class);
2023-01-28 23:41:39 +01:00
return oxNew(
WebauthnLogin::class,
2023-01-21 13:50:18 +01:00
$request->getRequestEscapedParameter('credential'),
$request->getRequestEscapedParameter('error')
);
}
2022-12-13 22:24:33 +01:00
}