Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
60.34% |
35 / 58 |
|
75.00% |
6 / 8 |
CRAP | |
0.00% |
0 / 1 |
| CachedPlatformKeyChainRepository | |
60.34% |
35 / 58 |
|
75.00% |
6 / 8 |
26.22 | |
0.00% |
0 / 1 |
| saveDefaultKeyChain | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| find | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
3 | |||
| findAll | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
20 | |||
| findByKeySetName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setKeys | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| exists | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| getCacheService | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getPlatformKeyChainRepository | |
100.00% |
1 / 1 |
|
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 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\taoLti\models\classes\Security\DataAccess\Repository; |
| 24 | |
| 25 | use common_exception_NoImplementation; |
| 26 | use ErrorException; |
| 27 | use OAT\Library\Lti1p3Core\Security\Key\Key; |
| 28 | use OAT\Library\Lti1p3Core\Security\Key\KeyChain; |
| 29 | use OAT\Library\Lti1p3Core\Security\Key\KeyChainInterface; |
| 30 | use OAT\Library\Lti1p3Core\Security\Key\KeyChainRepositoryInterface; |
| 31 | use oat\oatbox\cache\SimpleCache; |
| 32 | use oat\oatbox\service\ConfigurableService; |
| 33 | use oat\tao\model\security\Business\Domain\Key\KeyChainCollection; |
| 34 | use oat\tao\model\security\Business\Domain\Key\KeyChainQuery; |
| 35 | use oat\tao\model\security\Business\Domain\Key\Key as TaoKey; |
| 36 | use oat\tao\model\security\Business\Domain\Key\KeyChain as TaoKeyChain; |
| 37 | use Psr\SimpleCache\CacheInterface; |
| 38 | use Psr\SimpleCache\InvalidArgumentException; |
| 39 | |
| 40 | class CachedPlatformKeyChainRepository extends ConfigurableService implements KeyChainRepositoryInterface |
| 41 | { |
| 42 | public const PRIVATE_PATTERN = 'PLATFORM_LTI_PRIVATE_KEY_%s'; |
| 43 | public const PUBLIC_PATTERN = 'PLATFORM_LTI_PUBLIC_KEY_%s'; |
| 44 | |
| 45 | /** |
| 46 | * @throws InvalidArgumentException |
| 47 | * @throws ErrorException |
| 48 | */ |
| 49 | public function saveDefaultKeyChain(KeyChainInterface $keyChain): void |
| 50 | { |
| 51 | $this->setKeys( |
| 52 | $keyChain, |
| 53 | $keyChain->getIdentifier() |
| 54 | ); |
| 55 | |
| 56 | $this->getPlatformKeyChainRepository()->saveDefaultKeyChain($keyChain); |
| 57 | } |
| 58 | |
| 59 | public function find(string $identifier): ?KeyChainInterface |
| 60 | { |
| 61 | if ($this->exists($identifier)) { |
| 62 | $rawKeys = $this->getCacheService()->getMultiple( |
| 63 | [ |
| 64 | sprintf(self::PRIVATE_PATTERN, $identifier), |
| 65 | sprintf(self::PUBLIC_PATTERN, $identifier), |
| 66 | ] |
| 67 | ); |
| 68 | |
| 69 | $configuration = $this->getPlatformKeyChainRepository()->findConfiguration($identifier); |
| 70 | |
| 71 | return new KeyChain( |
| 72 | $configuration[PlatformKeyChainRepository::OPTION_DEFAULT_KEY_ID] ?? '', |
| 73 | $configuration[PlatformKeyChainRepository::OPTION_DEFAULT_KEY_NAME] ?? '', |
| 74 | new Key($rawKeys[sprintf(self::PUBLIC_PATTERN, $identifier)]), |
| 75 | new Key($rawKeys[sprintf(self::PRIVATE_PATTERN, $identifier)]) |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | $keyChain = $this->getPlatformKeyChainRepository()->find($identifier); |
| 80 | |
| 81 | if ($keyChain !== null) { |
| 82 | $this->setKeys($keyChain, $identifier); |
| 83 | } |
| 84 | |
| 85 | return $keyChain; |
| 86 | } |
| 87 | |
| 88 | public function findAll(KeyChainQuery $query): KeyChainCollection |
| 89 | { |
| 90 | if ($query->getIdentifier() == null) { |
| 91 | $query = new KeyChainQuery($this->getPlatformKeyChainRepository()->getDefaultKeyId()); |
| 92 | } |
| 93 | |
| 94 | if ($this->exists($query->getIdentifier())) { |
| 95 | $rawKeys = $this->getCacheService()->getMultiple( |
| 96 | [ |
| 97 | sprintf(self::PRIVATE_PATTERN, $query->getIdentifier()), |
| 98 | sprintf(self::PUBLIC_PATTERN, $query->getIdentifier()), |
| 99 | ] |
| 100 | ); |
| 101 | |
| 102 | $configuration = $this->getPlatformKeyChainRepository()->findConfiguration($query->getIdentifier()); |
| 103 | |
| 104 | return new KeyChainCollection( |
| 105 | new TaoKeyChain( |
| 106 | $configuration[PlatformKeyChainRepository::OPTION_DEFAULT_KEY_ID] ?? '', |
| 107 | $configuration[PlatformKeyChainRepository::OPTION_DEFAULT_KEY_NAME] ?? '', |
| 108 | new TaoKey($rawKeys[sprintf(self::PUBLIC_PATTERN, $query->getIdentifier())]), |
| 109 | new TaoKey($rawKeys[sprintf(self::PRIVATE_PATTERN, $query->getIdentifier())]) |
| 110 | ) |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | $keyChainCollection = $this->getPlatformKeyChainRepository()->findAll($query); |
| 115 | |
| 116 | foreach ($keyChainCollection->getKeyChains() as $keyChain) { |
| 117 | $this->setKeys($keyChain, $query->getIdentifier()); |
| 118 | } |
| 119 | |
| 120 | return $keyChainCollection; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * @throws common_exception_NoImplementation |
| 125 | */ |
| 126 | public function findByKeySetName(string $keySetName): array |
| 127 | { |
| 128 | throw new common_exception_NoImplementation(); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * @var KeyChainInterface|TaoKeyChain $keyChain |
| 133 | * @throws InvalidArgumentException |
| 134 | */ |
| 135 | private function setKeys($keyChain, string $identifier): void |
| 136 | { |
| 137 | $this->getCacheService()->set( |
| 138 | sprintf(self::PRIVATE_PATTERN, $identifier), |
| 139 | $keyChain->getPrivateKey()->getContent() |
| 140 | ); |
| 141 | |
| 142 | $this->getCacheService()->set( |
| 143 | sprintf(self::PUBLIC_PATTERN, $identifier), |
| 144 | $keyChain->getPublicKey()->getContent() |
| 145 | ); |
| 146 | } |
| 147 | |
| 148 | private function exists(string $identifier): bool |
| 149 | { |
| 150 | return $this->getCacheService()->has(sprintf(self::PRIVATE_PATTERN, $identifier)) && |
| 151 | $this->getCacheService()->has(sprintf(self::PUBLIC_PATTERN, $identifier)); |
| 152 | } |
| 153 | |
| 154 | private function getCacheService(): CacheInterface |
| 155 | { |
| 156 | return $this->getServiceLocator()->get(SimpleCache::SERVICE_ID); |
| 157 | } |
| 158 | |
| 159 | private function getPlatformKeyChainRepository(): PlatformKeyChainRepository |
| 160 | { |
| 161 | return $this->getServiceLocator()->get(PlatformKeyChainRepository::SERVICE_ID); |
| 162 | } |
| 163 | } |