| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Webauthn\MetadataService\Service; |
| 6 |
|
| 7 |
use function assert; |
| 8 |
use const DIRECTORY_SEPARATOR; |
| 9 |
use function file_get_contents; |
| 10 |
use InvalidArgumentException; |
| 11 |
use function is_array; |
| 12 |
use function sprintf; |
| 13 |
use Symfony\Component\Serializer\Encoder\JsonEncoder; |
| 14 |
use Symfony\Component\Serializer\SerializerInterface; |
| 15 |
use Webauthn\AttestationStatement\AttestationStatementSupportManager; |
| 16 |
use Webauthn\Denormalizer\WebauthnSerializerFactory; |
| 17 |
use Webauthn\Exception\MetadataStatementLoadingException; |
| 18 |
use Webauthn\MetadataService\Statement\MetadataStatement; |
| 19 |
|
| 20 |
final class FolderResourceMetadataService implements MetadataService |
| 21 |
{ |
| 22 |
private readonly SerializerInterface $serializer; |
| 23 |
|
| 24 |
public function __construct( |
| 25 |
private string $rootPath, |
| 26 |
?SerializerInterface $serializer = null, |
| 27 |
) { |
| 28 |
$this->serializer = $serializer ?? (new WebauthnSerializerFactory( |
| 29 |
AttestationStatementSupportManager::create() |
| 30 |
))->create(); |
| 31 |
$this->rootPath = rtrim($rootPath, DIRECTORY_SEPARATOR); |
| 32 |
is_dir($this->rootPath) || throw new InvalidArgumentException('The given parameter is not a valid folder.'); |
| 33 |
is_readable($this->rootPath) || throw new InvalidArgumentException( |
| 34 |
'The given parameter is not a valid folder.' |
| 35 |
); |
| 36 |
} |
| 37 |
|
| 38 |
public static function create(string $rootPath, ?SerializerInterface $serializer = null): self |
| 39 |
{ |
| 40 |
return new self($rootPath, $serializer); |
| 41 |
} |
| 42 |
|
| 43 |
public function list(): iterable |
| 44 |
{ |
| 45 |
$files = glob($this->rootPath . DIRECTORY_SEPARATOR . '*'); |
| 46 |
is_array($files) || throw MetadataStatementLoadingException::create('Unable to read files.'); |
| 47 |
foreach ($files as $file) { |
| 48 |
if (is_dir($file) || ! is_readable($file)) { |
| 49 |
continue; |
| 50 |
} |
| 51 |
|
| 52 |
yield basename($file); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
public function has(string $aaguid): bool |
| 57 |
{ |
| 58 |
$filename = $this->rootPath . DIRECTORY_SEPARATOR . $aaguid; |
| 59 |
|
| 60 |
return is_file($filename) && is_readable($filename); |
| 61 |
} |
| 62 |
|
| 63 |
public function get(string $aaguid): MetadataStatement |
| 64 |
{ |
| 65 |
$this->has($aaguid) || throw new InvalidArgumentException(sprintf( |
| 66 |
'The MDS with the AAGUID "%s" does not exist.', |
| 67 |
$aaguid |
| 68 |
)); |
| 69 |
$filename = $this->rootPath . DIRECTORY_SEPARATOR . $aaguid; |
| 70 |
$content = file_get_contents($filename); |
| 71 |
assert($content !== false, 'The file exists and is readable.'); |
| 72 |
$data = trim($content); |
| 73 |
$mds = $this->serializer->deserialize($data, MetadataStatement::class, JsonEncoder::FORMAT); |
| 74 |
$mds->aaguid !== null || throw MetadataStatementLoadingException::create('Invalid Metadata Statement.'); |
| 75 |
|
| 76 |
return $mds; |
| 77 |
} |
| 78 |
} |