- md5
- fcb55d6b71b51034612dac6e6ce74530
- sha1
- c042b640e1a06d06eb44ec72578cbde86a0ed575
- sha256
- d262265fc72e67da7bf3a4624e879f385f585af06c8857c0bd68a20295409f86
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
<?php
/**
* @package Joomla.Plugin
* @subpackage System.Webauthn
*
* @copyright (C) 2022 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Plugin\System\Webauthn;
use Lcobucci\JWT\Encoding\JoseEncoder;
use Lcobucci\JWT\Token\Parser;
use Lcobucci\JWT\Token\Plain;
use Webauthn\MetadataService\MetadataStatementRepository;
use Webauthn\MetadataService\Statement\MetadataStatement;
// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects
/**
* Authenticator metadata repository.
*
* This repository contains the metadata of all FIDO authenticators as published by the FIDO
* Alliance in their MDS version 3.0.
*
* @link https://fidoalliance.org/metadata/
* @since 4.2.0
*/
final class MetadataRepository implements MetadataStatementRepository
{
/**
* Cache of authenticator metadata statements
*
* @var MetadataStatement[]
* @since 4.2.0
*/
private $mdsCache = [];
/**
* Map of AAGUID to $mdsCache index
*
* @var array
* @since 4.2.0
*/
private $mdsMap = [];
/**
* Have I already tried to load the metadata cache?
*
* @var bool
* @since 4.2.2
*/
private $loaded = false;
/**
* Find an authenticator metadata statement given an AAGUID
*
* @param string $aaguid The AAGUID to find
*
* @return MetadataStatement|null The metadata statement; null if the AAGUID is unknown
* @since 4.2.0
*/
public function findOneByAAGUID(string $aaguid): ?MetadataStatement
{
$this->load();
$idx = $this->mdsMap[$aaguid] ?? null;
return $idx ? $this->mdsCache[$idx] : null;
}
/**
* Get basic information of the known FIDO authenticators by AAGUID
*
* @return object[]
* @since 4.2.0
*/
public function getKnownAuthenticators(): array
{
$this->load();
$mapKeys = function (MetadataStatement $meta) {
return $meta->getAaguid();
};
$mapvalues = function (MetadataStatement $meta) {
return $meta->getAaguid() ? (object) [
'description' => $meta->getDescription(),
'icon' => $meta->getIcon(),
] : null;
};
$keys = array_map($mapKeys, $this->mdsCache);
$values = array_map($mapvalues, $this->mdsCache);
$return = array_combine($keys, $values) ?: [];
$filter = function ($x) {
return !empty($x);
};
return array_filter($return, $filter);
}
/**
* Load the authenticator metadata cache
*
* @return void
* @since 4.2.0
*/
private function load(): void
{
if ($this->loaded) {
return;
}
$this->loaded = true;
$this->mdsCache = [];
$this->mdsMap = [];
$jwtFilename = JPATH_PLUGINS . '/system/webauthn/fido.jwt';
$rawJwt = file_get_contents($jwtFilename);
if (!\is_string($rawJwt) || \strlen($rawJwt) < 1024) {
return;
}
try {
$parser = new Parser(new JoseEncoder());
$token = $parser->parse($rawJwt);
} catch (\Exception) {
return;
}
if (!($token instanceof Plain)) {
return;
}
unset($rawJwt);
$entriesMapper = function (array $entry): ?MetadataStatement {
try {
$array = json_decode(json_encode($entry['metadataStatement']), true);
return MetadataStatement::createFromArray($array);
} catch (\Exception) {
return null;
}
};
$entries = array_map($entriesMapper, $token->claims()->get('entries', []));
unset($token);
$entriesFilter = function ($x) {
return !empty($x);
};
$this->mdsCache = array_filter($entries, $entriesFilter);
foreach ($this->mdsCache as $idx => $meta) {
$aaguid = $meta->getAaguid();
if (empty($aaguid)) {
continue;
}
$this->mdsMap[$aaguid] = $idx;
}
}
}
The same content is in 28 releases, Joomla 6.1.0-beta1 to 6.2.1-rc1. See them all, or every version of this file.