Skip to content

Commit 96ae9b9

Browse files
committed
refactor: add signing key value object
1 parent 441fe92 commit 96ae9b9

File tree

6 files changed

+57
-27
lines changed

6 files changed

+57
-27
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Tempest\Cryptography\Signing\Exceptions;
4+
5+
use Exception;
6+
7+
final class SigningKeyWasInvalid extends Exception implements SigningException
8+
{
9+
public static function becauseItIsMissing(): self
10+
{
11+
return new self('The signing key is missing or empty. Ensure you have a `SIGNING_KEY` environment variable.');
12+
}
13+
}

packages/cryptography/src/Signing/Exceptions/SigningKeyWasMissing.php

Lines changed: 0 additions & 13 deletions
This file was deleted.

packages/cryptography/src/Signing/GenericSigner.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,8 @@ final class GenericSigner implements Signer
1212
get => $this->config->algorithm;
1313
}
1414

15-
private string $key {
16-
get {
17-
if (trim($this->config->key) === '') {
18-
throw new SigningKeyWasMissing();
19-
}
20-
21-
return $this->config->key;
22-
}
15+
private SigningKey $key {
16+
get => SigningKey::fromString($this->config->key);
2317
}
2418

2519
public function __construct(
@@ -32,16 +26,16 @@ public function sign(string $data): Signature
3226
return new Signature(hash_hmac(
3327
algo: $this->algorithm->value,
3428
data: $data,
35-
key: $this->key,
29+
key: $this->key->value,
3630
));
3731
}
3832

3933
public function verify(string $data, Signature $signature): bool
4034
{
4135
return $this->timelock->invoke(
4236
callback: fn () => hash_equals(
43-
known_string: $this->sign($data)->signature,
44-
user_string: $signature->signature,
37+
known_string: $this->sign($data)->value,
38+
user_string: $signature->value,
4539
),
4640
duration: $this->config->minimumExecutionDuration ?: Duration::zero(),
4741
);

packages/cryptography/src/Signing/Signature.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@
77
final readonly class Signature implements Stringable
88
{
99
public function __construct(
10-
public string $signature,
10+
public string $value,
1111
) {}
1212

1313
public function __toString(): string
1414
{
15-
return $this->signature;
15+
return $this->value;
16+
}
17+
18+
public static function from(string $value): self
19+
{
20+
return new self($value);
1621
}
1722
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Tempest\Cryptography\Signing;
4+
5+
use Stringable;
6+
use Tempest\Cryptography\Signing\Exceptions\SigningKeyWasInvalid;
7+
8+
final readonly class SigningKey implements Stringable
9+
{
10+
public function __construct(
11+
private(set) string $value,
12+
) {
13+
if (trim($value) === '') {
14+
throw SigningKeyWasInvalid::becauseItIsMissing();
15+
}
16+
}
17+
18+
/**
19+
* Creates a signing key from a string.
20+
*/
21+
public static function fromString(string $key): self
22+
{
23+
return new self($key);
24+
}
25+
26+
public function __toString(): string
27+
{
28+
return $this->value;
29+
}
30+
}

packages/cryptography/tests/Signing/SignerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Tempest\Clock\Clock;
77
use Tempest\Clock\GenericClock;
88
use Tempest\Clock\MockClock;
9+
use Tempest\Cryptography\Signing\Exceptions\SigningKeyWasInvalid;
910
use Tempest\Cryptography\Signing\Exceptions\SigningKeyWasMissing;
1011
use Tempest\Cryptography\Signing\GenericSigner;
1112
use Tempest\Cryptography\Signing\SigningAlgorithm;
@@ -90,7 +91,7 @@ public function test_different_algoritms(): void
9091

9192
public function test_no_signing_key(): void
9293
{
93-
$this->expectException(SigningKeyWasMissing::class);
94+
$this->expectException(SigningKeyWasInvalid::class);
9495

9596
$signer = $this->createSigner(new SigningConfig(
9697
algorithm: SigningAlgorithm::SHA256,

0 commit comments

Comments
 (0)