Skip to content
This repository has been archived by the owner on Jan 3, 2023. It is now read-only.

Kontrola existence cesty k veřejnému klíči #24

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Cryptography/CryptographyService.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ class CryptographyService

public function __construct(string $privateKeyFile, string $publicKeyFile, string $privateKeyPassword = '')
{
if (!file_exists($privateKeyFile)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spíš is_file file_exists vrací true i pro adresáře

throw new PrivateKeyFileNotFoundException($privateKeyFile);
}
if (!file_exists($publicKeyFile)) {
throw new PublicKeyFileNotFoundException($publicKeyFile);
}
$this->privateKeyFile = $privateKeyFile;
$this->publicKeyFile = $publicKeyFile;
$this->privateKeyPassword = $privateKeyPassword;
Expand Down
8 changes: 8 additions & 0 deletions src/Cryptography/PrivateKeyFileNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types = 1);

namespace SlevomatEET\Cryptography;

class PrivateKeyFileNotFoundException extends PrivateKeyFileException
{

}
28 changes: 28 additions & 0 deletions src/Cryptography/PublicKeyFileException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types = 1);

namespace SlevomatEET\Cryptography;

class PublicKeyFileException extends \Exception
{

/**
* @var string
*/
private $publicKeyFile;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anotaci na jeden řádek


public function __construct(string $publicKeyFile, \Throwable $previous = null)
{
parent::__construct(sprintf(
'Public key could not be loaded from file \'%s\'. Please make sure that the file contains valid public key in PEM format.',
$publicKeyFile
), 0, $previous);

$this->publicKeyFile = $publicKeyFile;
}

public function getPublicKeyFile(): string
{
return $this->publicKeyFile;
}

}
8 changes: 8 additions & 0 deletions src/Cryptography/PublicKeyFileNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types = 1);

namespace SlevomatEET\Cryptography;

class PublicKeyFileNotFoundException extends PublicKeyFileException
{

}
26 changes: 26 additions & 0 deletions tests/SlevomatEET/Cryptography/CryptographyServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,32 @@ public function testGetCodes()
self::assertSame(self::EXPECTED_BKP, $crypto->getBkpCode($pkpCode));
}

/**
* @dataProvider provideInvalidKeyPaths
*/
public function testInvalidKeyPaths(string $privateKeyPath, string $publicKeyPath, string $expectedExceptionType)
{
try {
new CryptographyService($privateKeyPath, $publicKeyPath);
$this->fail('Exception ' . $expectedExceptionType . ' expected');
} catch (\PHPUnit\Framework\AssertionFailedError $exception) {
throw $exception;
} catch (\Throwable $exception) {
$this->assertInstanceOf($expectedExceptionType, $exception);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tady by se hodilo spíš $this->expectException

}
}

/**
* @return array[]
*/
public function provideInvalidKeyPaths(): array
{
return [
[self::PRIVATE_KEY_WITHOUT_PASSWORD_PATH, './foo/path', PublicKeyFileNotFoundException::class],
['./foo/path', self::PUBLIC_KEY_PATH, PrivateKeyFileNotFoundException::class],
];
}

public function testInvalidPrivateKeyInPkpCalculation()
{
$cryptoService = new CryptographyService(
Expand Down