File tree Expand file tree Collapse file tree 6 files changed +57
-27
lines changed
Expand file tree Collapse file tree 6 files changed +57
-27
lines changed Original file line number Diff line number Diff line change 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+ }
Load Diff This file was deleted.
Original file line number Diff line number Diff 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 );
Original file line number Diff line number Diff line change 77final 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}
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 66use Tempest \Clock \Clock ;
77use Tempest \Clock \GenericClock ;
88use Tempest \Clock \MockClock ;
9+ use Tempest \Cryptography \Signing \Exceptions \SigningKeyWasInvalid ;
910use Tempest \Cryptography \Signing \Exceptions \SigningKeyWasMissing ;
1011use Tempest \Cryptography \Signing \GenericSigner ;
1112use 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 ,
You can’t perform that action at this time.
0 commit comments