Skip to content
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

fix: handle JWT decode of non JWT tokens #428

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

[unreleased]
- Fix JWT decode of non JWT tokens #428
- Fix method signatures #427
- Updated CI to also test on PHP 8.3 #407
- Updated readme PHP requirement to PHP 7.0+ #407
- Added dependabot for GitHub Actions #407
Expand Down
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