webauthn/src/Application/Model/Credential/PublicKeyCredential.php

178 lines
5.1 KiB
PHP
Raw Normal View History

<?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\Application\Model\Credential;
use DateTime;
2022-10-31 00:11:06 +01:00
use Doctrine\DBAL\Driver\Exception as DoctrineDriverException;
use Doctrine\DBAL\Exception as DoctrineException;
use Doctrine\DBAL\Query\QueryBuilder;
2022-10-31 00:11:06 +01:00
use Exception;
use OxidEsales\Eshop\Core\Model\BaseModel;
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory;
use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactoryInterface;
2022-10-31 00:11:06 +01:00
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Webauthn\PublicKeyCredentialSource;
class PublicKeyCredential extends BaseModel
{
protected $_sCoreTable = 'd3wa_usercredentials';
public function __construct()
{
$this->init($this->getCoreTableName());
parent::__construct();
}
2022-11-02 16:38:43 +01:00
/**
* @param string $name
*/
public function setName(string $name)
{
$this->assign(['name' => $name]);
}
2022-11-02 16:38:43 +01:00
/**
* @return string|null
*/
public function getName(): ?string
{
return $this->getFieldData('name');
}
2022-11-02 16:38:43 +01:00
/**
* @param string $credentialId
*/
public function setCredentialId(string $credentialId)
{
$this->assign([
'credentialid' => base64_encode($credentialId)
]);
}
2022-11-02 16:38:43 +01:00
/**
* @return false|string
*/
public function getCredentialId()
{
return base64_decode($this->__get($this->_getFieldLongName('credentialid'))->rawValue);
}
2022-11-02 16:38:43 +01:00
/**
* @param string $userId
*/
public function setUserId(string $userId)
{
$this->assign([
'oxuserid' => $userId
]);
}
2022-11-02 16:38:43 +01:00
/**
* @return string|null
*/
public function getUserId(): ?string
{
return $this->__get($this->_getFieldLongName('oxuserid'))->rawValue;
}
2022-11-02 16:38:43 +01:00
/**
* @param PublicKeyCredentialSource $credential
*/
public function setCredential(PublicKeyCredentialSource $credential)
{
$this->assign([
'credential' => base64_encode(serialize($credential))
]);
}
2022-11-02 16:38:43 +01:00
/**
* @return false|PublicKeyCredentialSource
*/
public function getCredential()
{
return unserialize(base64_decode($this->__get($this->_getFieldLongName('credential'))->rawValue));
}
/**
* @param PublicKeyCredentialSource $publicKeyCredentialSource
2022-11-02 16:38:43 +01:00
* @param string|null $keyName
*
* @return void
2022-11-02 16:38:43 +01:00
* @throws ContainerExceptionInterface
* @throws DoctrineDriverException
* @throws DoctrineException
* @throws NotFoundExceptionInterface
2022-10-31 00:11:06 +01:00
* @throws Exception
*/
public function saveCredentialSource(PublicKeyCredentialSource $publicKeyCredentialSource, string $keyName = null): void
{
2022-10-31 00:11:06 +01:00
if ((oxNew(PublicKeyCredentialList::class))
->findOneByCredentialId($publicKeyCredentialSource->getPublicKeyCredentialId())
) {
return;
}
// will save on every successfully assertion, set id to prevent duplicated database entries
$id = $this->getIdByCredentialId($publicKeyCredentialSource->getPublicKeyCredentialId());
if ($this->exists($id)) {
$this->load($id);
}
$this->setShopId(Registry::getConfig()->getShopId());
$this->setUserId($publicKeyCredentialSource->getUserHandle());
$this->setCredentialId($publicKeyCredentialSource->getPublicKeyCredentialId());
$this->setCredential($publicKeyCredentialSource);
$this->setName($keyName ?: $this->getName() ?: (new DateTime())->format('Y-m-d H:i:s'));
$this->save();
}
2022-10-31 00:11:06 +01:00
/**
* @param string $publicKeyCredentialId
2022-11-02 16:38:43 +01:00
*
2022-10-31 00:11:06 +01:00
* @return string|null
* @throws ContainerExceptionInterface
2022-11-02 16:38:43 +01:00
* @throws DoctrineException
2022-10-31 00:11:06 +01:00
* @throws NotFoundExceptionInterface
*/
public function getIdByCredentialId(string $publicKeyCredentialId): ?string
{
/** @var QueryBuilder $qb */
$qb = ContainerFactory::getInstance()->getContainer()->get(QueryBuilderFactoryInterface::class)->create();
$qb->select('oxid')
->from($this->getViewName())
->where(
$qb->expr()->and(
$qb->expr()->eq(
'credentialid',
$qb->createNamedParameter(base64_encode($publicKeyCredentialId))
),
$qb->expr()->eq(
'oxshopid',
$qb->createNamedParameter(Registry::getConfig()->getShopId())
)
)
);
$oxid = $qb->execute()->fetchOne();
return strlen($oxid) ? $oxid : null;
}
}