diff --git a/CHANGELOG.md b/CHANGELOG.md index b89431d..f384c0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ * Added option to specify custom PDF Factory. * Changed pdf generator to FPDF. * Add methods `XEK` order type for EBICS 3.0 +* Add method to check keyring. ## 2.1 diff --git a/src/Models/Keyring.php b/src/Models/Keyring.php index 3834d85..58ae6fd 100644 --- a/src/Models/Keyring.php +++ b/src/Models/Keyring.php @@ -6,7 +6,7 @@ use AndrewSvirin\Ebics\Exceptions\PasswordEbicsException; /** - * EBICS key ring representation. + * EBICS keyring representation. * * @license http://www.opensource.org/licenses/mit-license.html MIT License * @author Andrew Svirin diff --git a/src/Services/ArrayKeyringManager.php b/src/Services/ArrayKeyringManager.php index c21f666..ab4a4e6 100644 --- a/src/Services/ArrayKeyringManager.php +++ b/src/Services/ArrayKeyringManager.php @@ -6,7 +6,7 @@ use LogicException; /** - * EBICS Keyring representation manage one key ring stored in the array. + * EBICS Keyring representation manage one keyring stored in the array. * * @license http://www.opensource.org/licenses/mit-license.html MIT License * @author Andrew Svirin @@ -22,13 +22,13 @@ public function loadKeyring($resource, string $passphrase, string $defaultVersio throw new LogicException('Expects array.'); } if (!empty($resource)) { - $result = $this->keyringFactory->createKeyringFromData($resource); + $keyring = $this->keyringFactory->createKeyringFromData($resource); } else { - $result = new Keyring($defaultVersion); + $keyring = new Keyring($defaultVersion); } - $result->setPassword($passphrase); + $keyring->setPassword($passphrase); - return $result; + return $keyring; } /** diff --git a/src/Services/CryptService.php b/src/Services/CryptService.php index a096fbe..5e7a84f 100644 --- a/src/Services/CryptService.php +++ b/src/Services/CryptService.php @@ -482,4 +482,22 @@ public function generateOrderId(string $partnerId): string return implode($chrs); } + + /** + * Check keyring is valid. + * + * @param Keyring $keyring + * + * @return bool + */ + public function checkKeyring(Keyring $keyring): bool + { + try { + $this->rsaFactory->createPrivate($keyring->getUserSignatureX()->getPrivateKey(), $keyring->getPassword()); + } catch (LogicException $exception) { + return false; + } + + return true; + } } diff --git a/src/Services/FileKeyringManager.php b/src/Services/FileKeyringManager.php index e11e611..57a5069 100644 --- a/src/Services/FileKeyringManager.php +++ b/src/Services/FileKeyringManager.php @@ -6,7 +6,7 @@ use LogicException; /** - * EBICS Keyring representation manage one key ring stored in the file. + * EBICS Keyring representation manage one keyring stored in the file. * * @license http://www.opensource.org/licenses/mit-license.html MIT License * @author Andrew Svirin @@ -25,13 +25,13 @@ public function loadKeyring($resource, string $passphrase, string $defaultVersio && ($content = file_get_contents($resource)) && is_string($content) ) { - $result = $this->keyringFactory->createKeyringFromData(json_decode($content, true)); + $keyring = $this->keyringFactory->createKeyringFromData(json_decode($content, true)); } else { - $result = new Keyring($defaultVersion); + $keyring = new Keyring($defaultVersion); } - $result->setPassword($passphrase); + $keyring->setPassword($passphrase); - return $result; + return $keyring; } /** diff --git a/tests/Services/CryptServiceTest.php b/tests/Services/CryptServiceTest.php index 4519ca5..8d47458 100644 --- a/tests/Services/CryptServiceTest.php +++ b/tests/Services/CryptServiceTest.php @@ -17,7 +17,7 @@ class CryptServiceTest extends AbstractEbicsTestCase { /** - * @group crypt-services-generate-keys + * @group crypt-service-generate-keys */ public function testGenerateKeys() { @@ -31,4 +31,22 @@ public function testGenerateKeys() self::assertArrayHasKey('publickey', $keys); self::assertArrayHasKey('partialkey', $keys); } + + /** + * @group crypt-service-check-password + */ + public function testCheckPassword() + { + $credentialsId = 2; + $client = $this->setupClientV25($credentialsId); + $cryptService = new CryptService(); + + $keyring = $client->getKeyring(); + + $this->assertTrue($cryptService->checkKeyring($keyring)); + + $keyring->setPassword('incorrect_password'); + + $this->assertFalse($cryptService->checkKeyring($keyring)); + } }