Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
CachedKeyChainGenerator
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
7 / 7
7
100.00% covered (success)
100.00%
1 / 1
 generate
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 save
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 invalidateKeyChain
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 invalidateJwks
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getKeyChainGenerator
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
 getCache
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\Platform\Service;
24
25use OAT\Library\Lti1p3Core\Security\Key\KeyChainInterface;
26use oat\oatbox\cache\SimpleCache;
27use oat\oatbox\service\ConfigurableService;
28use oat\taoLti\models\classes\Security\DataAccess\Repository\CachedPlatformJwksRepository;
29use oat\taoLti\models\classes\Security\DataAccess\Repository\CachedPlatformKeyChainRepository;
30use oat\taoLti\models\classes\Security\DataAccess\Repository\PlatformKeyChainRepository;
31use Psr\SimpleCache\CacheInterface;
32
33class CachedKeyChainGenerator extends ConfigurableService implements KeyChainGeneratorInterface
34{
35    public function generate(
36        string $id = PlatformKeyChainRepository::OPTION_DEFAULT_KEY_ID_VALUE,
37        string $name = PlatformKeyChainRepository::OPTION_DEFAULT_KEY_NAME_VALUE,
38        ?string $passPhrase = null
39    ): KeyChainInterface {
40        $keyChain = $this->getKeyChainGenerator()->generate($id, $name, $passPhrase);
41        $this->save($keyChain);
42
43        return $keyChain;
44    }
45
46    private function save(KeyChainInterface $keyChain): bool
47    {
48        $this->getKeyChainRepository()->saveKeyChain($keyChain);
49
50        $this->invalidateKeyChain($keyChain);
51        $this->invalidateJwks();
52
53        return true;
54    }
55
56    private function invalidateKeyChain(KeyChainInterface $keyChain): void
57    {
58        $this->getCache()->delete(
59            sprintf(CachedPlatformKeyChainRepository::PRIVATE_PATTERN, $keyChain->getIdentifier())
60        );
61
62        $this->getCache()->delete(
63            sprintf(CachedPlatformKeyChainRepository::PUBLIC_PATTERN, $keyChain->getIdentifier())
64        );
65    }
66
67    private function invalidateJwks(): void
68    {
69        $this->getCache()->delete(CachedPlatformJwksRepository::JWKS_KEY);
70    }
71
72    private function getKeyChainGenerator(): KeyChainGeneratorInterface
73    {
74        return $this->getServiceLocator()->get(OpenSslKeyChainGenerator::class);
75    }
76
77    private function getKeyChainRepository(): PlatformKeyChainRepository
78    {
79        return $this->getServiceLocator()->get(PlatformKeyChainRepository::class);
80    }
81
82    private function getCache(): CacheInterface
83    {
84        return $this->getServiceLocator()->get(SimpleCache::SERVICE_ID);
85    }
86}