Skip to content

Commit

Permalink
Add KeySet::getByThumbnail()
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvinmo committed Aug 27, 2023
1 parent 92a2990 commit 8aeee6a
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/SimpleJWT/Keys/KeySet.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,30 @@ function getById($kid, $fuzzy = false) {
return null;
}

/**
* Finds a key matching the specified thumbnail.
*
* In addition to exact match, this method also supports *fuzzy search*, which
* matches the beginning of the key ID string
*
* @param string $thumb the thumbnail to search
* @param bool $fuzzy whether fuzzy search is to be used
* @return KeyInterface|null the found key, or null
*/
function getByThumbnail($thumb, $fuzzy = false) {
$fuzzy_keys = [];

foreach ($this->keys as $key) {
if ($key->getThumbnail() == $thumb) {
return $key;
} elseif ($fuzzy && (strpos($key->getThumbnail(), $thumb) === 0)) {
$fuzzy_keys[] = $key;
}
}
if (count($fuzzy_keys) == 1) return $fuzzy_keys[0];
return null;
}

/**
* Finds a key matching specified criteria.
*
Expand Down

0 comments on commit 8aeee6a

Please sign in to comment.