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

178 lines
5.8 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
*/
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\Admin;
use D3\Webauthn\Application\Model\Credential\PublicKeyCredential;
use D3\Webauthn\Application\Model\Credential\PublicKeyCredentialList;
2022-11-03 13:18:02 +01:00
use D3\Webauthn\Application\Model\Exceptions\WebauthnCreateException;
use D3\Webauthn\Application\Model\Exceptions\WebauthnException;
use D3\Webauthn\Application\Model\Webauthn;
2022-10-24 22:24:40 +02:00
use D3\Webauthn\Modules\Application\Model\d3_User_Webauthn;
2022-11-02 16:38:43 +01:00
use Doctrine\DBAL\Driver\Exception as DoctrineDriverException;
use Doctrine\DBAL\Exception as DoctrineException;
2022-10-24 22:24:40 +02:00
use Exception;
use OxidEsales\Eshop\Application\Controller\Admin\AdminDetailsController;
use OxidEsales\Eshop\Application\Model\User;
use OxidEsales\Eshop\Core\Registry;
2022-11-02 16:38:43 +01:00
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
2022-10-24 22:24:40 +02:00
class d3user_webauthn extends AdminDetailsController
{
protected $_sSaveError = null;
protected $_sThisTemplate = 'd3user_webauthn.tpl';
/**
* @return string
*/
2022-10-30 00:27:11 +02:00
public function render(): string
2022-10-24 22:24:40 +02:00
{
2022-11-02 16:38:43 +01:00
$this->addTplParam('readonly', !(oxNew(Webauthn::class)->isAvailable()));
2022-10-24 22:24:40 +02:00
parent::render();
$soxId = $this->getEditObjectId();
if (isset($soxId) && $soxId != "-1") {
/** @var d3_User_Webauthn $oUser */
$oUser = $this->getUserObject();
if ($oUser->load($soxId)) {
$this->addTplParam("oxid", $oUser->getId());
} else {
$this->addTplParam("oxid", '-1');
}
$this->addTplParam("edit", $oUser);
}
if ($this->_sSaveError) {
$this->addTplParam("sSaveError", $this->_sSaveError);
}
return $this->_sThisTemplate;
}
2022-11-04 22:02:44 +01:00
/**
* @return void
*/
public function requestNewCredential(): void
{
2022-11-02 16:38:43 +01:00
try {
$this->setPageType( 'requestnew' );
$this->setAuthnRegister();
} catch (Exception|ContainerExceptionInterface|NotFoundExceptionInterface|DoctrineDriverException $e) {
2022-11-03 13:18:02 +01:00
Registry::getUtilsView()->addErrorToDisplay($e);
2022-11-04 00:12:42 +01:00
Registry::getLogger()->error($e->getMessage(), ['UserId' => $this->getEditObjectId()]);
2022-11-04 00:05:52 +01:00
Registry::getLogger()->debug($e->getTraceAsString());
2022-11-02 16:38:43 +01:00
Registry::getUtils()->redirect('index.php?cl=d3user_webauthn');
}
}
2022-11-04 22:02:44 +01:00
/**
* @return void
*/
public function saveAuthn(): void
{
2022-11-02 16:38:43 +01:00
try {
if ( strlen( Registry::getRequest()->getRequestEscapedParameter( 'error' ) ) ) {
2022-11-03 13:18:02 +01:00
/** @var WebauthnCreateException $e */
$e = oxNew(WebauthnCreateException::class, Registry::getRequest()->getRequestEscapedParameter( 'error' ));
throw $e;
2022-11-02 16:38:43 +01:00
}
2022-11-02 16:38:43 +01:00
if ( strlen( Registry::getRequest()->getRequestEscapedParameter( 'credential' ) ) ) {
/** @var Webauthn $webauthn */
$webauthn = oxNew( Webauthn::class );
$webauthn->saveAuthn( Registry::getRequest()->getRequestEscapedParameter( 'credential' ), Registry::getRequest()->getRequestEscapedParameter( 'keyname' ) );
}
2022-11-03 13:18:02 +01:00
} catch (WebauthnException|Exception|NotFoundExceptionInterface|ContainerExceptionInterface|DoctrineDriverException $e) {
Registry::getLogger()->error($e->getDetailedErrorMessage(), ['UserId' => $this->getEditObjectId()]);
2022-11-04 00:05:52 +01:00
Registry::getLogger()->debug($e->getTraceAsString());
2022-11-03 13:18:02 +01:00
Registry::getUtilsView()->addErrorToDisplay($e);
}
}
2022-11-04 22:02:44 +01:00
/**
* @param $pageType
* @return void
*/
public function setPageType($pageType): void
{
$this->addTplParam('pageType', $pageType);
}
2022-11-02 16:38:43 +01:00
/**
* @throws ContainerExceptionInterface
* @throws DoctrineDriverException
* @throws NotFoundExceptionInterface
* @throws DoctrineException
2022-11-04 22:02:44 +01:00
* @throws WebauthnException
2022-11-02 16:38:43 +01:00
*/
2022-11-04 22:02:44 +01:00
public function setAuthnRegister(): void
2022-10-24 22:24:40 +02:00
{
2022-11-02 16:38:43 +01:00
$authn = oxNew(Webauthn::class);
$user = $this->getUserObject();
$user->load($this->getEditObjectId());
$publicKeyCredentialCreationOptions = $authn->getCreationOptions($user);
$this->addTplParam(
'webauthn_publickey_create',
$publicKeyCredentialCreationOptions
);
$this->addTplParam('isAdmin', isAdmin());
$this->addTplParam('keyname', Registry::getRequest()->getRequestEscapedParameter('credenialname'));
2022-10-24 22:24:40 +02:00
}
/**
* @param $userId
2022-11-02 16:38:43 +01:00
*
* @return array
2022-11-02 16:38:43 +01:00
* @throws ContainerExceptionInterface
* @throws DoctrineDriverException
* @throws DoctrineException
* @throws NotFoundExceptionInterface
2022-10-24 22:24:40 +02:00
*/
2022-10-30 00:27:11 +02:00
public function getCredentialList($userId): array
2022-10-24 22:24:40 +02:00
{
$oUser = $this->getUserObject();
$oUser->load($userId);
$publicKeyCrendetials = oxNew(PublicKeyCredentialList::class);
return $publicKeyCrendetials->getAllFromUser($oUser)->getArray();
2022-10-24 22:24:40 +02:00
}
/**
* @return User
*/
2022-10-30 00:27:11 +02:00
public function getUserObject(): User
2022-10-24 22:24:40 +02:00
{
return oxNew(User::class);
}
2022-11-04 22:02:44 +01:00
/**
* @return void
*/
public function deleteKey(): void
{
/** @var PublicKeyCredential $credential */
$credential = oxNew(PublicKeyCredential::class);
$credential->delete(Registry::getRequest()->getRequestEscapedParameter('deleteoxid'));
}
2022-10-24 22:24:40 +02:00
}