mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-09 05:08:36 +00:00
JwtUtils: added a split() function to reduce code duplication
This commit is contained in:
parent
222399d178
commit
755e53cd71
@ -58,6 +58,19 @@ use const STR_PAD_LEFT;
|
||||
|
||||
final class JwtUtils{
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
* @phpstan-return array{string, string, string}
|
||||
* @throws JwtException
|
||||
*/
|
||||
public static function split(string $jwt) : array{
|
||||
$v = explode(".", $jwt);
|
||||
if(count($v) !== 3){
|
||||
throw new JwtException("Expected exactly 3 JWT parts, got " . count($v));
|
||||
}
|
||||
return [$v[0], $v[1], $v[2]]; //workaround phpstan bug
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO: replace this result with an object
|
||||
*
|
||||
@ -67,10 +80,7 @@ final class JwtUtils{
|
||||
* @throws JwtException
|
||||
*/
|
||||
public static function parse(string $token) : array{
|
||||
$v = explode(".", $token);
|
||||
if(count($v) !== 3){
|
||||
throw new JwtException("Expected exactly 3 JWT parts, got " . count($v));
|
||||
}
|
||||
$v = self::split($token);
|
||||
$header = json_decode(self::b64UrlDecode($v[0]), true);
|
||||
if(!is_array($header)){
|
||||
throw new JwtException("Failed to decode JWT header JSON: " . json_last_error_msg());
|
||||
@ -87,11 +97,7 @@ final class JwtUtils{
|
||||
* @throws JwtException
|
||||
*/
|
||||
public static function verify(string $jwt, PublicKeyInterface $signingKey) : bool{
|
||||
$parts = explode('.', $jwt);
|
||||
if(count($parts) !== 3){
|
||||
throw new JwtException("Expected exactly 3 JWT parts, got " . count($parts));
|
||||
}
|
||||
[$header, $body, $signature] = $parts;
|
||||
[$header, $body, $signature] = self::split($jwt);
|
||||
|
||||
$plainSignature = self::b64UrlDecode($signature);
|
||||
if(strlen($plainSignature) !== 96){
|
||||
|
Loading…
x
Reference in New Issue
Block a user