Skip to content

Commit

Permalink
fix: handle JWT decode of non JWT tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
DeepDiver1975 committed Sep 5, 2024
1 parent 1e85443 commit 8ce5ac6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/OpenIDConnectClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -1220,11 +1220,11 @@ protected function urlEncode(string $str): string
/**
* @param string $jwt encoded JWT
* @param int $section the section we would like to decode
* @return object|null
* @return object|string|null
*/
protected function decodeJWT(string $jwt, int $section = 0) {
$parts = explode('.', $jwt);
return json_decode(base64url_decode($parts[$section]), false);
return json_decode(base64url_decode($parts[$section] ?? ''), false);
}

/**
Expand Down Expand Up @@ -1737,6 +1737,10 @@ public function getRefreshToken()
return $this->refreshToken;
}

public function setIdToken(string $idToken) {
$this->idToken = $idToken;
}

/**
* @return string|null
*/
Expand All @@ -1753,21 +1757,21 @@ public function getAccessTokenHeader() {
}

/**
* @return object
* @return object|string|null
*/
public function getAccessTokenPayload() {
return $this->decodeJWT($this->accessToken, 1);
}

/**
* @return object
* @return object|string|null
*/
public function getIdTokenHeader() {
return $this->decodeJWT($this->idToken);
}

/**
* @return object
* @return object|string|null
*/
public function getIdTokenPayload() {
return $this->decodeJWT($this->idToken, 1);
Expand Down
11 changes: 11 additions & 0 deletions tests/OpenIDConnectClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,20 @@ class OpenIDConnectClientTest extends TestCase
public function testJWTDecode()
{
$client = new OpenIDConnectClient();
# access token
$client->setAccessToken('');
$header = $client->getAccessTokenHeader();
self::assertEquals('', $header);
$payload = $client->getAccessTokenPayload();
self::assertEquals('', $payload);

# id token
$client->setIdToken('');
$header = $client->getIdTokenHeader();
self::assertEquals('', $header);
$payload = $client->getIdTokenPayload();
self::assertEquals('', $payload);

}

public function testGetNull()
Expand Down

0 comments on commit 8ce5ac6

Please sign in to comment.