Skip to content

Commit

Permalink
Add method to check keyring.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-svirin committed Oct 4, 2024
1 parent ffea861 commit 94609f5
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/Models/Keyring.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/Services/ArrayKeyringManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

/**
Expand Down
18 changes: 18 additions & 0 deletions src/Services/CryptService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
10 changes: 5 additions & 5 deletions src/Services/FileKeyringManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

/**
Expand Down
20 changes: 19 additions & 1 deletion tests/Services/CryptServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class CryptServiceTest extends AbstractEbicsTestCase
{

/**
* @group crypt-services-generate-keys
* @group crypt-service-generate-keys
*/
public function testGenerateKeys()
{
Expand All @@ -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));
}
}

0 comments on commit 94609f5

Please sign in to comment.