From 7d0238190d15cc30d1260028bfcde27be1a50b9c Mon Sep 17 00:00:00 2001 From: Christer Edvartsen Date: Tue, 11 Jul 2023 18:47:58 +0200 Subject: [PATCH] Update usage of the JWT lib --- src/ArrayContainsComparator/Matcher/JWT.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/ArrayContainsComparator/Matcher/JWT.php b/src/ArrayContainsComparator/Matcher/JWT.php index 25e86e9..ddec7e3 100644 --- a/src/ArrayContainsComparator/Matcher/JWT.php +++ b/src/ArrayContainsComparator/Matcher/JWT.php @@ -4,6 +4,7 @@ use Firebase; use InvalidArgumentException; use Imbo\BehatApiExtension\ArrayContainsComparator as Comparator; +use UnexpectedValueException; /** * Match a JWT token @@ -76,10 +77,20 @@ public function __invoke($jwt, $name) { } $token = $this->jwtTokens[$name]; - $result = (array) Firebase\JWT\JWT::decode($jwt, $token['secret'], $this->allowedAlgorithms); - if (!$this->comparator->compare($token['payload'], $result)) { - throw new InvalidArgumentException('JWT mismatch.'); + foreach ($this->allowedAlgorithms as $algorithm) { + try { + $result = (array) Firebase\JWT\JWT::decode($jwt, new Firebase\JWT\Key($token['secret'], $algorithm)); + } catch (UnexpectedValueException $e) { + // try next algorithm + continue; + } + + if ($this->comparator->compare($token['payload'], $result)) { + return true; + } } + + throw new InvalidArgumentException('JWT mismatch.'); } }