Skip to content

Commit

Permalink
Make kid unique only in combination with public use (#4)
Browse files Browse the repository at this point in the history
* Make kid unique only in combination with public use

* Minor tweaks
  • Loading branch information
Strobotti authored Mar 28, 2020
1 parent 51b4123 commit 88d0edd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/Key/AbstractKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function getKeyType(): string
*
* @since 1.0.0
*/
public function getKeyId(): string
public function getKeyId(): ?string
{
return $this->kid;
}
Expand All @@ -76,7 +76,7 @@ public function getKeyId(): string
*
* @since 1.0.0
*/
public function getPublicKeyUse(): string
public function getPublicKeyUse(): ?string
{
return $this->use;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Key/KeyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
34 changes: 23 additions & 11 deletions src/KeySet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -80,23 +84,31 @@ 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
*/
public function jsonSerialize(): array
{
$ret = [];

foreach ($this->keys as $key) {
foreach ($this->getKeys() as $key) {
$ret[$key->getKeyId()] = $key->jsonSerialize();
}

Expand Down

0 comments on commit 88d0edd

Please sign in to comment.