diff --git a/src/Key/AbstractKey.php b/src/Key/AbstractKey.php index c583ce6..9ba9d2d 100644 --- a/src/Key/AbstractKey.php +++ b/src/Key/AbstractKey.php @@ -66,7 +66,7 @@ public function getKeyType(): string * * @since 1.0.0 */ - public function getKeyId(): string + public function getKeyId(): ?string { return $this->kid; } @@ -76,7 +76,7 @@ public function getKeyId(): string * * @since 1.0.0 */ - public function getPublicKeyUse(): string + public function getPublicKeyUse(): ?string { return $this->use; } diff --git a/src/Key/KeyInterface.php b/src/Key/KeyInterface.php index 6d966c1..716f163 100644 --- a/src/Key/KeyInterface.php +++ b/src/Key/KeyInterface.php @@ -52,7 +52,7 @@ public function getKeyId(): ?string; * * @since 1.0.0 */ - public function getPublicKeyUse(): string; + public function getPublicKeyUse(): ?string; /** * Gets the cryptographic algorithm used to sign the key, ie. the value of the `alg` field. diff --git a/src/KeySet.php b/src/KeySet.php index 3a7b073..c739fb7 100644 --- a/src/KeySet.php +++ b/src/KeySet.php @@ -54,23 +54,27 @@ public function setKeyFactory(KeyFactory $keyFactory): self } /** - * @since 1.0.0 + * @since 1.0.0 Only $kid parameter + * @since 1.1.0 Added optional $use parameter */ - public function containsKey(string $kid): bool + public function containsKey(string $kid, string $use = KeyInterface::PUBLIC_KEY_USE_SIGNATURE): bool { - return \array_key_exists($kid, $this->keys); + return null !== $this->getKeyById($kid, $use); } /** * @since 1.0.0 + * @since 1.1.0 Added optional $use parameter */ - public function getKeyById(string $kid): ?KeyInterface + public function getKeyById(string $kid, string $use = KeyInterface::PUBLIC_KEY_USE_SIGNATURE): ?KeyInterface { - if (!$this->containsKey($kid)) { - return null; + foreach ($this->getKeys() as $key) { + if ($key->getKeyId() === $kid && $key->getPublicKeyUse() === $use) { + return $key; + } } - return $this->keys[$kid]; + return null; } /** @@ -80,15 +84,23 @@ public function getKeyById(string $kid): ?KeyInterface */ public function addKey(KeyInterface $key): self { - if ($this->containsKey($key->getKeyId())) { - throw new \InvalidArgumentException(\sprintf('Key with id `%s` already exists in the set', $key->getKeyId())); + if ($this->containsKey($key->getKeyId(), $key->getPublicKeyUse())) { + throw new \InvalidArgumentException(\sprintf('Key with id `%s` and use `%s` already exists in the set', $key->getKeyId(), $key->getPublicKeyUse())); } - $this->keys[$key->getKeyId()] = $key; + $this->keys[] = $key; return $this; } + /** + * @return KeyInterface[] + */ + public function getKeys(): array + { + return \array_values($this->keys); + } + /** * @since 1.0.0 */ @@ -96,7 +108,7 @@ public function jsonSerialize(): array { $ret = []; - foreach ($this->keys as $key) { + foreach ($this->getKeys() as $key) { $ret[$key->getKeyId()] = $key->jsonSerialize(); }