Skip to content

feat: add decodeHeaderAndPayload method #510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 43 additions & 3 deletions src/JWT.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class JWT
* 'HS256', 'HS384', 'HS512', 'RS256', 'RS384'
* and 'RS512'.
*
* @return stdClass The JWT's payload as a PHP object
* @return stdClass The JWT's header and payload as PHP objects
*
* @throws InvalidArgumentException Provided key/key-array was empty or malformed
* @throws DomainException Provided JWT is malformed
Expand All @@ -92,7 +92,7 @@ class JWT
* @uses jsonDecode
* @uses urlsafeB64Decode
*/
public static function decode(
public static function decodeHeaderAndPayload(
string $jwt,
$keyOrKeyArray
): stdClass {
Expand Down Expand Up @@ -167,7 +167,47 @@ public static function decode(
throw new ExpiredException('Expired token');
}

return $payload;
$decodedJwt = new stdClass();
$decodedJwt->header = $header;
$decodedJwt->payload = $payload;

return $decodedJwt;
}

/**
* Decodes the payload from a JWT string into a PHP object.
*
* @param string $jwt The JWT
* @param Key|ArrayAccess<string,Key>|array<string,Key> $keyOrKeyArray The Key or associative array of key IDs
* (kid) to Key objects.
* If the algorithm used is asymmetric, this is
* the public key.
* Each Key object contains an algorithm and
* matching key.
* Supported algorithms are 'ES384','ES256',
* 'HS256', 'HS384', 'HS512', 'RS256', 'RS384'
* and 'RS512'.
*
* @return stdClass The JWT's payload as a PHP object
*
* @throws InvalidArgumentException Provided key/key-array was empty or malformed
* @throws DomainException Provided JWT is malformed
* @throws UnexpectedValueException Provided JWT was invalid
* @throws SignatureInvalidException Provided JWT was invalid because the signature verification failed
* @throws BeforeValidException Provided JWT is trying to be used before it's eligible as defined by 'nbf'
* @throws BeforeValidException Provided JWT is trying to be used before it's been created as defined by 'iat'
* @throws ExpiredException Provided JWT has since expired, as defined by the 'exp' claim
*
* @uses jsonDecode
* @uses urlsafeB64Decode
*/
public static function decode(
string $jwt,
$keyOrKeyArray
): stdClass {
$decodedJwt = self::decodeHeaderAndPayload($jwt, $keyOrKeyArray);

return $decodedJwt->payload;
}

/**
Expand Down