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

182 regels
6.4 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 Assert\AssertionFailedException;
2022-12-03 00:33:46 +01:00
use D3\TestingTools\Production\IsMockable;
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;
2023-01-28 23:41:39 +01:00
use Doctrine\DBAL\Statement;
use OxidEsales\Eshop\Application\Model\User;
use OxidEsales\Eshop\Core\Config;
use OxidEsales\Eshop\Core\Model\ListModel;
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;
use Webauthn\PublicKeyCredentialSourceRepository;
use Webauthn\PublicKeyCredentialUserEntity;
class PublicKeyCredentialList extends ListModel implements PublicKeyCredentialSourceRepository
{
2022-12-03 00:33:46 +01:00
use IsMockable;
protected $_sObjectsInListName = PublicKeyCredential::class;
public function __construct()
{
$this->d3CallMockableFunction([ListModel::class, '__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
*/
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(
'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())
)
)
);
2023-01-28 23:41:39 +01:00
/** @var Statement $stmt */
$stmt = $qb->execute();
$credential = $stmt->fetchOne();
2022-11-13 21:43:33 +01:00
if (!strlen((string) $credential)) {
return null;
}
$credential = unserialize(base64_decode($credential));
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
*/
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',
2023-01-21 13:50:18 +01:00
$qb->createNamedParameter(d3GetOxidDIC()->get('d3ox.webauthn.'.Config::class)->getShopId())
)
)
);
2023-01-28 23:41:39 +01:00
/** @var Statement $stmt */
$stmt = $qb->execute();
// generate decoded credentials list
return array_map(function (array $fields) {
2023-01-21 13:50:18 +01:00
/** @var PublicKeyCredential $credential */
$credential = clone $this->getBaseObject();
$credential->assign(['credential' => $fields['credential']]);
return $credential->getCredential();
2023-01-28 23:41:39 +01:00
}, $stmt->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
{
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',
2023-01-21 13:50:18 +01:00
$qb->createNamedParameter(d3GetOxidDIC()->get('d3ox.webauthn.'.Config::class)->getShopId())
)
)
);
2023-01-28 23:41:39 +01:00
/** @var Statement $stmt */
$stmt = $qb->execute();
foreach ($stmt->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
/**
* unused, but must fulfil the interface
2022-11-04 22:02:44 +01:00
* @param PublicKeyCredentialSource $publicKeyCredentialSource
* @return void
* @throws AssertionFailedException
2023-01-28 23:41:39 +01:00
* @throws DoctrineDriverException
* @throws DoctrineException
2022-11-04 22:02:44 +01:00
*/
public function saveCredentialSource(PublicKeyCredentialSource $publicKeyCredentialSource): void
{
2022-12-12 23:41:07 +01:00
/** @var PublicKeyCredential $base */
$base = $this->getBaseObject();
$base->saveCredentialSource($publicKeyCredentialSource);
}
2022-12-13 22:24:33 +01:00
}