Skip to content

Commit e98d4fc

Browse files
committed
Update SignatureKeyPairBag
1 parent b6b6ed1 commit e98d4fc

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

src/ValueAbstracts/SignatureKeyPairBag.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
namespace SimpleSAML\OpenID\ValueAbstracts;
66

7+
use SimpleSAML\OpenID\Algorithms\SignatureAlgorithmEnum;
8+
use SimpleSAML\OpenID\Exceptions\OpenIdException;
9+
710
class SignatureKeyPairBag
811
{
912
/**
@@ -46,4 +49,47 @@ public function hasKeyId(string $keyId): bool
4649
{
4750
return isset($this->signatureKeyPairs[$keyId]);
4851
}
52+
53+
54+
public function getFirst(): ?SignatureKeyPair
55+
{
56+
return $this->signatureKeyPairs[array_key_first($this->signatureKeyPairs)] ?? null;
57+
}
58+
59+
60+
/**
61+
* @throws \SimpleSAML\OpenID\Exceptions\OpenIdException
62+
*/
63+
public function getFirstOrFail(): SignatureKeyPair
64+
{
65+
return $this->getFirst() ?? throw new OpenIdException(
66+
'Signature key pair is not set.',
67+
);
68+
}
69+
70+
71+
public function getFirstByAlgorithm(SignatureAlgorithmEnum $signatureAlgorithm): ?SignatureKeyPair
72+
{
73+
foreach ($this->signatureKeyPairs as $signatureKeyPair) {
74+
if ($signatureKeyPair->getSignatureAlgorithm() === $signatureAlgorithm) {
75+
return $signatureKeyPair;
76+
}
77+
}
78+
79+
return null;
80+
}
81+
82+
83+
/**
84+
* @throws \SimpleSAML\OpenID\Exceptions\OpenIdException
85+
*/
86+
public function getFirstByAlgorithmOrFail(SignatureAlgorithmEnum $signatureAlgorithm): SignatureKeyPair
87+
{
88+
return $this->getFirstByAlgorithm($signatureAlgorithm) ?? throw new OpenIdException(
89+
sprintf(
90+
'Signature key pair for algorithm %s is not set.',
91+
$signatureAlgorithm->value,
92+
),
93+
);
94+
}
4995
}

0 commit comments

Comments
 (0)