Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
PlatformJwksRepository
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
4 / 4
5
100.00% covered (success)
100.00%
1 / 1
 find
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
2
 withJwksExporter
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getJwksExporter
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getKeyChainRepository
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; under version 2
7 * of the License (non-upgradable).
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 *
18 * Copyright (c) 2020 (original work) Open Assessment Technologies SA;
19 */
20
21declare(strict_types=1);
22
23namespace oat\taoLti\models\classes\Security\DataAccess\Repository;
24
25use OAT\Library\Lti1p3Core\Security\Jwks\Exporter\Jwk\JwkExporterInterface;
26use OAT\Library\Lti1p3Core\Security\Jwks\Exporter\Jwk\JwkRS256Exporter;
27use OAT\Library\Lti1p3Core\Security\Key\Key;
28use OAT\Library\Lti1p3Core\Security\Key\KeyChain;
29use OAT\Library\Lti1p3Core\Security\Key\KeyChainRepositoryInterface;
30use oat\oatbox\service\ConfigurableService;
31use oat\tao\model\security\Business\Contract\JwksRepositoryInterface;
32use oat\tao\model\security\Business\Domain\Key\Jwk;
33use oat\tao\model\security\Business\Domain\Key\Jwks;
34use oat\tao\model\security\Business\Domain\Key\KeyChainQuery;
35
36class PlatformJwksRepository extends ConfigurableService implements JwksRepositoryInterface
37{
38    /** @var JwkExporterInterface */
39    private $jwksExporter;
40
41    public function find(): Jwks
42    {
43        $collection = $this->getKeyChainRepository()
44            ->findAll(new KeyChainQuery())
45            ->getKeyChains();
46
47        $jwkList = [];
48        $exporter = $this->getJwksExporter();
49
50        foreach ($collection as $key) {
51            $keyChain = new KeyChain(
52                $key->getIdentifier(),
53                $key->getName(),
54                new Key($key->getPublicKey()->getValue())
55            );
56
57            $exported = $exporter->export($keyChain);
58
59            $jwkList[] = new Jwk(
60                $exported['kty'],
61                $exported['e'],
62                $exported['n'],
63                $exported['kid'],
64                $exported['alg'],
65                $exported['use']
66            );
67        }
68
69        return new Jwks(...$jwkList);
70    }
71
72    public function withJwksExporter(JwkExporterInterface $jwksExporter): self
73    {
74        $this->jwksExporter = $jwksExporter;
75
76        return $this;
77    }
78
79    private function getJwksExporter(): JwkExporterInterface
80    {
81        return $this->jwksExporter ?? new JwkRS256Exporter();
82    }
83
84    private function getKeyChainRepository(): PlatformKeyChainRepository
85    {
86        return $this->getServiceLocator()->get(PlatformKeyChainRepository::SERVICE_ID);
87    }
88}