mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-09 13:14:54 +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{
|
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
|
* TODO: replace this result with an object
|
||||||
*
|
*
|
||||||
@ -67,10 +80,7 @@ final class JwtUtils{
|
|||||||
* @throws JwtException
|
* @throws JwtException
|
||||||
*/
|
*/
|
||||||
public static function parse(string $token) : array{
|
public static function parse(string $token) : array{
|
||||||
$v = explode(".", $token);
|
$v = self::split($token);
|
||||||
if(count($v) !== 3){
|
|
||||||
throw new JwtException("Expected exactly 3 JWT parts, got " . count($v));
|
|
||||||
}
|
|
||||||
$header = json_decode(self::b64UrlDecode($v[0]), true);
|
$header = json_decode(self::b64UrlDecode($v[0]), true);
|
||||||
if(!is_array($header)){
|
if(!is_array($header)){
|
||||||
throw new JwtException("Failed to decode JWT header JSON: " . json_last_error_msg());
|
throw new JwtException("Failed to decode JWT header JSON: " . json_last_error_msg());
|
||||||
@ -87,11 +97,7 @@ final class JwtUtils{
|
|||||||
* @throws JwtException
|
* @throws JwtException
|
||||||
*/
|
*/
|
||||||
public static function verify(string $jwt, PublicKeyInterface $signingKey) : bool{
|
public static function verify(string $jwt, PublicKeyInterface $signingKey) : bool{
|
||||||
$parts = explode('.', $jwt);
|
[$header, $body, $signature] = self::split($jwt);
|
||||||
if(count($parts) !== 3){
|
|
||||||
throw new JwtException("Expected exactly 3 JWT parts, got " . count($parts));
|
|
||||||
}
|
|
||||||
[$header, $body, $signature] = $parts;
|
|
||||||
|
|
||||||
$plainSignature = self::b64UrlDecode($signature);
|
$plainSignature = self::b64UrlDecode($signature);
|
||||||
if(strlen($plainSignature) !== 96){
|
if(strlen($plainSignature) !== 96){
|
||||||
|
Loading…
x
Reference in New Issue
Block a user