diff --git a/composer.json b/composer.json index 4f3e777c..6fe5f486 100644 --- a/composer.json +++ b/composer.json @@ -61,8 +61,8 @@ "oat-sa/oatbox-extension-installer": "~1.1||dev-master", "oat-sa/lib-lti1p3-ags": "~2", "oat-sa/lib-lti1p3-core": "~7", - "oat-sa/generis" : ">=15.40.0", - "oat-sa/tao-core" : ">=54.10.0" + "oat-sa/generis": ">=16.0.0", + "oat-sa/tao-core": ">=54.26.0" }, "autoload" : { "psr-4" : { diff --git a/models/classes/ExceptionInterpreter.php b/models/classes/ExceptionInterpreter.php index 13b33dc1..5a722b6c 100644 --- a/models/classes/ExceptionInterpreter.php +++ b/models/classes/ExceptionInterpreter.php @@ -77,6 +77,6 @@ private function log($msg) ->get(FileSystemService::SERVICE_ID) ->getFileSystem(self::FILESYSTEM_ID_TO_LOG); - $fs->put('lti_' . $this->exception->getKey() . '.log', $msg); + $fs->write('lti_' . $this->exception->getKey() . '.log', $msg); } } diff --git a/models/classes/Security/DataAccess/Repository/PlatformKeyChainRepository.php b/models/classes/Security/DataAccess/Repository/PlatformKeyChainRepository.php index 11c8d235..02d282f0 100644 --- a/models/classes/Security/DataAccess/Repository/PlatformKeyChainRepository.php +++ b/models/classes/Security/DataAccess/Repository/PlatformKeyChainRepository.php @@ -23,11 +23,12 @@ namespace oat\taoLti\models\classes\Security\DataAccess\Repository; use common_exception_NoImplementation; -use League\Flysystem\FilesystemInterface; use OAT\Library\Lti1p3Core\Security\Key\Key; use OAT\Library\Lti1p3Core\Security\Key\KeyChain; use OAT\Library\Lti1p3Core\Security\Key\KeyChainInterface; use OAT\Library\Lti1p3Core\Security\Key\KeyChainRepositoryInterface; +use oat\oatbox\filesystem\FilesystemException; +use oat\oatbox\filesystem\FilesystemInterface; use oat\oatbox\filesystem\FileSystemService; use oat\oatbox\service\ConfigurableService; use oat\tao\model\security\Business\Domain\Key\Key as TaoKey; @@ -69,25 +70,23 @@ protected function save(KeyChainInterface $keyChain, string $identifier): void $publicKeyPath = $configs[self::OPTION_DEFAULT_PUBLIC_KEY_PATH] ?? null; $privateKeyPath = $configs[self::OPTION_DEFAULT_PRIVATE_KEY_PATH] ?? null; - $isPublicKeySaved = false; - $isPrivateKeySaved = false; if ($publicKeyPath !== null && $privateKeyPath !== null) { - $isPublicKeySaved = $this->getFileSystem() - ->put( - ltrim($publicKeyPath, DIRECTORY_SEPARATOR), - $keyChain->getPublicKey()->getContent() - ); - - $isPrivateKeySaved = $this->getFileSystem() - ->put( - ltrim($privateKeyPath, DIRECTORY_SEPARATOR), - $keyChain->getPrivateKey()->getContent() - ); - } - - if (!$isPublicKeySaved || !$isPrivateKeySaved) { - throw new PlatformKeyChainException('Impossible to write LTI keys'); + try { + $this->getFileSystem() + ->write( + ltrim($publicKeyPath, DIRECTORY_SEPARATOR), + $keyChain->getPublicKey()->getContent() + ); + + $this->getFileSystem() + ->write( + ltrim($privateKeyPath, DIRECTORY_SEPARATOR), + $keyChain->getPrivateKey()->getContent() + ); + } catch (\Exception $e) { + throw new PlatformKeyChainException('Impossible to write LTI keys', 0, $e); + } } } @@ -114,8 +113,8 @@ public function find(string $identifier): ?KeyChainInterface throw new PlatformKeyChainException('The key path is not defined'); } - $publicKey = $this->getFileSystem()->read($publicKeyPath); - $privateKey = $this->getFileSystem()->read($privateKeyPath); + $publicKey = $this->readKey($publicKeyPath); + $privateKey = $this->readKey($privateKeyPath); if ($publicKey === false || $privateKey === false) { throw new PlatformKeyChainException('Impossible to read LTI keys'); @@ -140,8 +139,8 @@ public function findAll(KeyChainQuery $query): KeyChainCollection $privateKeyPassphrase = $configs[self::OPTION_DEFAULT_PRIVATE_KEY_PASSPHRASE] ?? null; if ($defaultKeyId && $publicKeyPath && $privateKeyPath) { - $publicKey = $this->getFileSystem()->read($publicKeyPath); - $privateKey = $this->getFileSystem()->read($privateKeyPath); + $publicKey = $this->readKey($publicKeyPath); + $privateKey = $this->readKey($privateKeyPath); $keyChains[] = new TaoKeyChain( $defaultKeyId, @@ -168,6 +167,15 @@ public function findByKeySetName(string $keySetName): array throw new common_exception_NoImplementation(); } + private function readKey(string $path): string + { + try { + return $this->getFileSystem()->read($path); + } catch (FilesystemException $e) { + throw new PlatformKeyChainException('Impossible to read LTI keys'); + } + } + private function getFileSystem(): FilesystemInterface { /** @var FileSystemService $fileSystemService */ diff --git a/test/unit/models/classes/Security/DataAccess/Repository/PlatformKeyChainRepositoryTest.php b/test/unit/models/classes/Security/DataAccess/Repository/PlatformKeyChainRepositoryTest.php index e16b5988..a7d03b4a 100644 --- a/test/unit/models/classes/Security/DataAccess/Repository/PlatformKeyChainRepositoryTest.php +++ b/test/unit/models/classes/Security/DataAccess/Repository/PlatformKeyChainRepositoryTest.php @@ -22,6 +22,8 @@ namespace oat\taoLti\test\unit\models\classes\Security\DataAccess\Repository; +use League\Flysystem\UnableToReadFile; +use League\Flysystem\UnableToWriteFile; use oat\generis\test\ServiceManagerMockTrait; use OAT\Library\Lti1p3Core\Security\Key\Key; use OAT\Library\Lti1p3Core\Security\Key\KeyChain; @@ -129,7 +131,7 @@ public function testFindFails(): void { $this->fileSystem ->method('read') - ->willReturn(false); + ->willThrowException(new UnableToReadFile()); $keyChain = $this->subject->find(''); @@ -147,8 +149,7 @@ public function testFindWithEmptyPathFails(): void public function testSaveDefaultKeyChain(): void { $this->fileSystem - ->method('put') - ->willReturn(true); + ->method('write'); $this->subject->saveDefaultKeyChain( new KeyChain('keyId', '', new Key(''), new Key('')) @@ -160,8 +161,8 @@ public function testSaveDefaultKeyChain(): void public function testSaveDefaultKeyChainFails(): void { $this->fileSystem - ->method('put') - ->willReturn(false); + ->method('write') + ->willThrowException(new UnableToWriteFile()); $this->expectException(PlatformKeyChainException::class); $this->expectExceptionMessage('Impossible to write LTI keys');