Skip to content

Commit f57bbb7

Browse files
committed
Switching main output from object to array, to simplify external iteration
1 parent c40d11e commit f57bbb7

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/BYUJWT.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,13 @@ public static function validateJWT($jwt)
170170
public static function decode($jwt)
171171
{
172172
$key = static::getPublicKey();
173-
$decoded = JWT::decode($jwt, $key, ['HS256','RS256','HS512','HS384']);
174-
if (empty($decoded->exp)) {
173+
$decodedObject = JWT::decode($jwt, $key, ['HS256','RS256','HS512','HS384']);
174+
175+
//JWT::decode returns at stdClass object, but iterating through keys is much
176+
//simpler with an array. So here's a quick Object-to-Array conversion
177+
$decoded = json_decode(json_encode($decodedObject), true);
178+
179+
if (empty($decoded['exp'])) {
175180
//Firebase\JWT\JWT::decode does not throw an error if "exp" does not exist,
176181
//although it does throw an error if it exists but has already passed
177182
//For BYU we want to ensure that it does exist, as well as being valid

tests/BYUJWTTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function testJWTWithNoExpiration()
115115
{
116116
//Validation should be false when Expiration does not exist in an otherwise valid JWT
117117
$decoded = BYUJWT::decode(static::$validJWT);
118-
unset($decoded->exp);
118+
unset($decoded['exp']);
119119
$badJwt = JWT::encode($decoded, BYUJWT::getPublicKey());
120120

121121
$this->assertSame(false, BYUJWT::validateJWT($badJwt));
@@ -137,7 +137,7 @@ public function testJTWDecodeSuccesful()
137137
{
138138
$decodedJwt = BYUJWT::decode(static::$validJWT);
139139
$this->assertNotEmpty($decodedJwt);
140-
$this->assertEquals('https://api.byu.edu', $decodedJwt->iss);
141-
$this->assertEquals(static::$credentials->client_id, $decodedJwt->{'http://wso2.org/claims/client_id'});
140+
$this->assertEquals('https://api.byu.edu', $decodedJwt['iss']);
141+
$this->assertEquals(static::$credentials->client_id, $decodedJwt['http://wso2.org/claims/client_id']);
142142
}
143143
}

0 commit comments

Comments
 (0)