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
|
|
|
|
*/
|
|
|
|
|
2022-11-04 22:02:44 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-10-25 01:01:10 +02:00
|
|
|
namespace D3\Webauthn\Application\Model\Credential;
|
|
|
|
|
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;
|
|
|
|
use OxidEsales\Eshop\Application\Model\User;
|
|
|
|
use OxidEsales\Eshop\Core\Model\ListModel;
|
|
|
|
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;
|
|
|
|
use Webauthn\PublicKeyCredentialSourceRepository;
|
|
|
|
use Webauthn\PublicKeyCredentialUserEntity;
|
|
|
|
|
|
|
|
class PublicKeyCredentialList extends ListModel implements PublicKeyCredentialSourceRepository
|
|
|
|
{
|
|
|
|
protected $_sObjectsInListName = PublicKeyCredential::class;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct(PublicKeyCredential::class);
|
|
|
|
}
|
|
|
|
|
2022-10-31 00:11:06 +01:00
|
|
|
/**
|
|
|
|
* @param string $publicKeyCredentialId
|
|
|
|
* @return PublicKeyCredentialSource|null
|
|
|
|
* @throws DoctrineDriverException
|
|
|
|
* @throws DoctrineException
|
|
|
|
* @throws ContainerExceptionInterface
|
|
|
|
* @throws NotFoundExceptionInterface
|
|
|
|
*/
|
2022-10-25 01:01:10 +02:00
|
|
|
public function findOneByCredentialId(string $publicKeyCredentialId): ?PublicKeyCredentialSource
|
|
|
|
{
|
|
|
|
/** @var QueryBuilder $qb */
|
|
|
|
$qb = ContainerFactory::getInstance()->getContainer()->get(QueryBuilderFactoryInterface::class)->create();
|
|
|
|
$qb->select('credential')
|
|
|
|
->from($this->getBaseObject()->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())
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$credential = $qb->execute()->fetchOne();
|
|
|
|
|
|
|
|
if (!strlen($credential)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-11-02 08:23:41 +01:00
|
|
|
$credential = unserialize(base64_decode($credential));
|
2022-10-25 01:01:10 +02:00
|
|
|
|
|
|
|
return $credential instanceof PublicKeyCredentialSource ? $credential : null;
|
|
|
|
}
|
|
|
|
|
2022-10-31 00:11:06 +01:00
|
|
|
/**
|
|
|
|
* @param PublicKeyCredentialUserEntity $publicKeyCredentialUserEntity
|
|
|
|
* @return array|PublicKeyCredentialSource[]
|
|
|
|
* @throws ContainerExceptionInterface
|
|
|
|
* @throws DoctrineDriverException
|
|
|
|
* @throws DoctrineException
|
|
|
|
* @throws NotFoundExceptionInterface
|
|
|
|
*/
|
2022-10-25 01:01:10 +02:00
|
|
|
public function findAllForUserEntity(PublicKeyCredentialUserEntity $publicKeyCredentialUserEntity): array
|
|
|
|
{
|
|
|
|
/** @var QueryBuilder $qb */
|
|
|
|
$qb = ContainerFactory::getInstance()->getContainer()->get(QueryBuilderFactoryInterface::class)->create();
|
|
|
|
$qb->select('credential')
|
|
|
|
->from($this->getBaseObject()->getViewName())
|
|
|
|
->where(
|
|
|
|
$qb->expr()->and(
|
|
|
|
$qb->expr()->eq(
|
|
|
|
'oxuserid',
|
|
|
|
$qb->createNamedParameter($publicKeyCredentialUserEntity->getId())
|
|
|
|
),
|
|
|
|
$qb->expr()->eq(
|
|
|
|
'oxshopid',
|
|
|
|
$qb->createNamedParameter(Registry::getConfig()->getShopId())
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
// generate decoded credentials list
|
|
|
|
return array_map(function (array $fields) {
|
2022-11-02 08:23:41 +01:00
|
|
|
return unserialize(base64_decode($fields['credential']));
|
2022-10-25 01:01:10 +02:00
|
|
|
}, $qb->execute()->fetchAllAssociative());
|
|
|
|
}
|
|
|
|
|
2022-10-31 00:11:06 +01:00
|
|
|
/**
|
|
|
|
* @param User $user
|
2022-11-04 22:02:44 +01:00
|
|
|
* @return self
|
2022-10-31 00:11:06 +01:00
|
|
|
* @throws ContainerExceptionInterface
|
|
|
|
* @throws DoctrineDriverException
|
|
|
|
* @throws DoctrineException
|
|
|
|
* @throws NotFoundExceptionInterface
|
|
|
|
*/
|
|
|
|
public function getAllFromUser(User $user): PublicKeyCredentialList
|
2022-10-25 01:01:10 +02:00
|
|
|
{
|
|
|
|
if (!$user->isLoaded()) {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var QueryBuilder $qb */
|
|
|
|
$qb = ContainerFactory::getInstance()->getContainer()->get(QueryBuilderFactoryInterface::class)->create();
|
|
|
|
$qb->select('oxid')
|
|
|
|
->from($this->getBaseObject()->getViewName())
|
|
|
|
->where(
|
|
|
|
$qb->expr()->and(
|
|
|
|
$qb->expr()->eq(
|
|
|
|
'oxuserid',
|
|
|
|
$qb->createNamedParameter($user->getId())
|
|
|
|
),
|
|
|
|
$qb->expr()->eq(
|
|
|
|
'oxshopid',
|
|
|
|
$qb->createNamedParameter(Registry::getConfig()->getShopId())
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($qb->execute()->fetchAllAssociative() as $fields) {
|
|
|
|
$id = $fields['oxid'];
|
|
|
|
$credential = clone $this->getBaseObject();
|
|
|
|
$credential->load($id);
|
|
|
|
$this->offsetSet($id, $credential);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2022-11-04 22:02:44 +01:00
|
|
|
/**
|
|
|
|
* @param PublicKeyCredentialSource $publicKeyCredentialSource
|
|
|
|
* @return void
|
|
|
|
*/
|
2022-10-25 01:01:10 +02:00
|
|
|
public function saveCredentialSource(PublicKeyCredentialSource $publicKeyCredentialSource): void
|
|
|
|
{
|
|
|
|
$this->getBaseObject()->saveCredentialSource($publicKeyCredentialSource);
|
|
|
|
}
|
|
|
|
}
|