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

191 regels
5.5 KiB
PHP

<?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.
*
2022-11-04 22:45:47 +01:00
* https://www.d3data.de
*
* @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-11-04 22:02:44 +01:00
declare(strict_types=1);
namespace D3\Webauthn\Application\Model\Credential;
use D3\TestingTools\Production\IsMockable;
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;
2022-12-01 23:46:09 +01:00
use OxidEsales\Eshop\Core\Config;
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
{
use IsMockable;
protected $_sCoreTable = 'd3wa_usercredentials';
public function __construct()
{
$this->init($this->getCoreTableName());
parent::__construct();
}
2022-11-02 16:38:43 +01:00
/**
* @param string $name
*/
2022-11-04 22:02:44 +01:00
public function setName(string $name): void
{
2022-11-04 22:02:44 +01:00
$this->assign([
2022-12-13 22:24:33 +01:00
'name' => $name,
2022-11-04 22:02:44 +01:00
]);
}
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
*/
2022-11-04 22:02:44 +01:00
public function setCredentialId(string $credentialId): void
{
$this->assign([
2022-12-13 22:24:33 +01:00
'credentialid' => base64_encode($credentialId),
]);
}
2022-11-02 16:38:43 +01:00
/**
2022-12-12 23:41:07 +01:00
* @return null|string
2022-11-02 16:38:43 +01:00
*/
2022-11-04 22:02:44 +01:00
public function getCredentialId(): ?string
{
2022-12-12 23:41:07 +01:00
return base64_decode($this->__get($this->_getFieldLongName('credentialid'))->rawValue) ?: null;
}
2022-11-02 16:38:43 +01:00
/**
* @param string $userId
*/
2022-11-04 22:02:44 +01:00
public function setUserId(string $userId): void
{
$this->assign([
2022-12-13 22:24:33 +01:00
'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
*/
2022-11-04 22:02:44 +01:00
public function setCredential(PublicKeyCredentialSource $credential): void
{
$this->assign([
2022-12-13 22:24:33 +01:00
'credential' => base64_encode(serialize($credential)),
]);
}
2022-11-02 16:38:43 +01:00
/**
2022-12-12 23:41:07 +01:00
* @return null|PublicKeyCredentialSource
2022-11-02 16:38:43 +01:00
*/
2022-11-04 22:02:44 +01:00
public function getCredential(): ?PublicKeyCredentialSource
{
2023-01-21 13:50:18 +01:00
return unserialize(
base64_decode(
$this->__get($this->_getFieldLongName('credential'))->rawValue
),
['allowed_classes' => [PublicKeyCredentialSource::class]]
) ?:
null;
}
/**
* @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-11-04 22:02:44 +01:00
// item exist already
/** @var PublicKeyCredentialList $pkcl */
$pkcl = d3GetOxidDIC()->get(PublicKeyCredentialList::class);
if ($pkcl->findOneByCredentialId($publicKeyCredentialSource->getPublicKeyCredentialId())) {
2022-10-31 00:11:06 +01:00
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-04 22:02:44 +01:00
* @throws DoctrineDriverException
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',
2023-01-21 13:50:18 +01:00
$qb->createNamedParameter(d3GetOxidDIC()->get('d3ox.webauthn.'.Config::class)->getShopId())
)
)
);
$oxid = $qb->execute()->fetchOne();
2022-11-13 21:43:33 +01:00
return strlen((string) $oxid) ? $oxid : null;
}
2022-12-13 22:24:33 +01:00
}