2022-10-25 01:01:10 +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
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace D3\Webauthn\Application\Model\Credential;
|
|
|
|
|
2022-10-28 00:45:32 +02:00
|
|
|
use DateTime;
|
2022-10-31 00:11:06 +01:00
|
|
|
use Doctrine\DBAL\Driver\Exception as DoctrineDriverException;
|
|
|
|
use Doctrine\DBAL\Exception as DoctrineException;
|
2022-10-25 01:01:10 +02:00
|
|
|
use Doctrine\DBAL\Query\QueryBuilder;
|
2022-10-31 00:11:06 +01:00
|
|
|
use Exception;
|
2022-10-25 01:01:10 +02:00
|
|
|
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;
|
2022-10-25 01:01:10 +02:00
|
|
|
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)
|
2022-10-25 01:01:10 +02:00
|
|
|
{
|
|
|
|
$this->assign(['name' => $name]);
|
|
|
|
}
|
|
|
|
|
2022-11-02 16:38:43 +01:00
|
|
|
/**
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function getName(): ?string
|
2022-10-25 01:01:10 +02:00
|
|
|
{
|
|
|
|
return $this->getFieldData('name');
|
|
|
|
}
|
2022-10-26 00:02:55 +02:00
|
|
|
|
2022-11-02 16:38:43 +01:00
|
|
|
/**
|
|
|
|
* @param string $credentialId
|
|
|
|
*/
|
|
|
|
public function setCredentialId(string $credentialId)
|
2022-10-25 01:01:10 +02:00
|
|
|
{
|
|
|
|
$this->assign([
|
2022-11-02 08:23:41 +01:00
|
|
|
'credentialid' => base64_encode($credentialId)
|
2022-10-25 01:01:10 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2022-11-02 16:38:43 +01:00
|
|
|
/**
|
|
|
|
* @return false|string
|
|
|
|
*/
|
2022-10-25 01:01:10 +02:00
|
|
|
public function getCredentialId()
|
|
|
|
{
|
2022-11-02 08:23:41 +01:00
|
|
|
return base64_decode($this->__get($this->_getFieldLongName('credentialid'))->rawValue);
|
2022-10-25 01:01:10 +02:00
|
|
|
}
|
|
|
|
|
2022-11-02 16:38:43 +01:00
|
|
|
/**
|
|
|
|
* @param string $userId
|
|
|
|
*/
|
|
|
|
public function setUserId(string $userId)
|
2022-10-25 01:01:10 +02:00
|
|
|
{
|
|
|
|
$this->assign([
|
|
|
|
'oxuserid' => $userId
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2022-11-02 16:38:43 +01:00
|
|
|
/**
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function getUserId(): ?string
|
2022-10-25 01:01:10 +02:00
|
|
|
{
|
|
|
|
return $this->__get($this->_getFieldLongName('oxuserid'))->rawValue;
|
|
|
|
}
|
|
|
|
|
2022-11-02 16:38:43 +01:00
|
|
|
/**
|
|
|
|
* @param PublicKeyCredentialSource $credential
|
|
|
|
*/
|
|
|
|
public function setCredential(PublicKeyCredentialSource $credential)
|
2022-10-25 01:01:10 +02:00
|
|
|
{
|
|
|
|
$this->assign([
|
2022-11-02 08:23:41 +01:00
|
|
|
'credential' => base64_encode(serialize($credential))
|
2022-10-25 01:01:10 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2022-11-02 16:38:43 +01:00
|
|
|
/**
|
|
|
|
* @return false|PublicKeyCredentialSource
|
|
|
|
*/
|
2022-10-25 01:01:10 +02:00
|
|
|
public function getCredential()
|
|
|
|
{
|
2022-11-02 08:23:41 +01:00
|
|
|
return unserialize(base64_decode($this->__get($this->_getFieldLongName('credential'))->rawValue));
|
2022-10-25 01:01:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param PublicKeyCredentialSource $publicKeyCredentialSource
|
2022-11-02 16:38:43 +01:00
|
|
|
* @param string|null $keyName
|
|
|
|
*
|
2022-10-25 01:01:10 +02:00
|
|
|
* @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
|
2022-10-25 01:01:10 +02:00
|
|
|
*/
|
2022-10-26 00:02:55 +02:00
|
|
|
public function saveCredentialSource(PublicKeyCredentialSource $publicKeyCredentialSource, string $keyName = null): void
|
2022-10-25 01:01:10 +02:00
|
|
|
{
|
2022-10-31 00:11:06 +01:00
|
|
|
if ((oxNew(PublicKeyCredentialList::class))
|
|
|
|
->findOneByCredentialId($publicKeyCredentialSource->getPublicKeyCredentialId())
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-25 01:01:10 +02:00
|
|
|
// will save on every successfully assertion, set id to prevent duplicated database entries
|
|
|
|
$id = $this->getIdByCredentialId($publicKeyCredentialSource->getPublicKeyCredentialId());
|
2022-10-28 00:45:32 +02:00
|
|
|
|
|
|
|
if ($this->exists($id)) {
|
|
|
|
$this->load($id);
|
|
|
|
}
|
2022-10-25 01:01:10 +02:00
|
|
|
|
|
|
|
$this->setShopId(Registry::getConfig()->getShopId());
|
|
|
|
$this->setUserId($publicKeyCredentialSource->getUserHandle());
|
|
|
|
$this->setCredentialId($publicKeyCredentialSource->getPublicKeyCredentialId());
|
|
|
|
$this->setCredential($publicKeyCredentialSource);
|
2022-10-28 00:45:32 +02:00
|
|
|
$this->setName($keyName ?: $this->getName() ?: (new DateTime())->format('Y-m-d H:i:s'));
|
2022-10-25 01:01:10 +02:00
|
|
|
$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
|
|
|
|
*/
|
2022-10-25 01:01:10 +02:00
|
|
|
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(
|
2022-10-26 00:02:55 +02:00
|
|
|
'credentialid',
|
2022-11-02 08:23:41 +01:00
|
|
|
$qb->createNamedParameter(base64_encode($publicKeyCredentialId))
|
2022-10-25 01:01:10 +02:00
|
|
|
),
|
|
|
|
$qb->expr()->eq(
|
|
|
|
'oxshopid',
|
|
|
|
$qb->createNamedParameter(Registry::getConfig()->getShopId())
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$oxid = $qb->execute()->fetchOne();
|
|
|
|
|
|
|
|
return strlen($oxid) ? $oxid : null;
|
|
|
|
}
|
|
|
|
}
|