Skip to content

Commit 8856a7a

Browse files
shadowhandevansims
andauthored
Allow access tokens to be decoded (#571)
* Allow access tokens to be decoded * Always allow clientId to be a valid claim for `aud` * test: Fix validation test Co-authored-by: Evan Sims <[email protected]>
1 parent 57caf1c commit 8856a7a

File tree

4 files changed

+34
-18
lines changed

4 files changed

+34
-18
lines changed

src/Auth0.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -246,28 +246,28 @@ public function decode(
246246
?string $tokenNonce = null,
247247
?int $tokenMaxAge = null,
248248
?int $tokenLeeway = null,
249-
?int $tokenNow = null
249+
?int $tokenNow = null,
250+
?int $tokenType = null
250251
): Token {
251-
// instantiate Token handler using the provided JWT, expecting an ID token, using the SDK configuration.
252-
$token = new Token($this->configuration, $token, Token::TYPE_ID_TOKEN);
252+
$tokenType = $tokenType ?? Token::TYPE_ID_TOKEN;
253+
$tokenNonce = $tokenNonce ?? $this->getTransientStore()->getOnce('nonce') ?? null;
254+
$tokenMaxAge = $tokenMaxAge ?? $this->getTransientStore()->getOnce('max_age') ?? null;
255+
$tokenIssuer = null;
253256

254-
// Verify token signature.
257+
$token = new Token($this->configuration, $token, $tokenType);
255258
$token->verify();
256259

257-
$tokenMaxAge = $tokenMaxAge ?? $this->getTransientStore()->getOnce('max_age') ?? null;
258-
259-
// If pulling from transient storage, $tokenMaxAge might be a string.
260+
// If pulled from transient storage, $tokenMaxAge might be a string.
260261
if ($tokenMaxAge !== null) {
261262
$tokenMaxAge = (int) $tokenMaxAge;
262263
}
263264

264-
// Validate token claims.
265265
$token->validate(
266-
null,
266+
$tokenIssuer,
267267
$tokenAudience,
268268
$tokenOrganization,
269-
$tokenNonce ?? $this->getTransientStore()->getOnce('nonce') ?? null,
270-
$tokenMaxAge ?? null,
269+
$tokenNonce,
270+
$tokenMaxAge,
271271
$tokenLeeway,
272272
$tokenNow
273273
);

src/Token.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,8 @@ public function validate(
139139
$tokenNonce = $tokenNonce ?? null;
140140
$tokenMaxAge = $tokenMaxAge ?? $this->configuration->getTokenMaxAge() ?? null;
141141
$tokenLeeway = $tokenLeeway ?? $this->configuration->getTokenLeeway() ?? 60;
142-
143-
// If 'aud' claim check isn't defined, fallback to client id, if configured.
144-
if (count($tokenAudience) === 0 && $this->configuration->hasClientId()) {
145-
$tokenAudience[] = (string) $this->configuration->getClientId();
146-
}
142+
$tokenAudience[] = (string) $this->configuration->getClientId();
143+
$tokenAudience = array_unique($tokenAudience);
147144

148145
$validator = $this->parser->validate();
149146
$now = $tokenNow ?? time();

tests/Unit/Auth0Test.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,26 @@ public function defer(
391391
$auth0->decode($token);
392392
})->throws(\Auth0\SDK\Exception\InvalidTokenException::class);
393393

394+
test('decode() can be used with access tokens', function (): void {
395+
$token = (new \Auth0\Tests\Utilities\TokenGenerator())->withHs256();
396+
397+
$auth0 = new \Auth0\SDK\Auth0($this->configuration + [
398+
'tokenAlgorithm' => 'HS256',
399+
]);
400+
401+
$decoded = $auth0->decode($token,
402+
null,
403+
null,
404+
null,
405+
null,
406+
null,
407+
null,
408+
\Auth0\SDK\Token::TYPE_TOKEN,
409+
);
410+
411+
expect($decoded->getAudience())->toContain('__test_client_id__');
412+
});
413+
394414
test('exchange() throws an exception if no code is present', function(): void {
395415
$auth0 = new \Auth0\SDK\Auth0($this->configuration);
396416
$auth0->exchange();

tests/Unit/TokenTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,11 @@ function(): SdkConfiguration {
158158
array $claims
159159
): void {
160160
$token = new Token($configuration, $jwt->token, Token::TYPE_ID_TOKEN);
161-
162161
$token->validate(null, [ $claims['aud'] ]);
163162
})->with(['mocked data' => [
164163
function(): SdkConfiguration {
165164
$this->configuration->setDomain('__test_domain__');
166-
$this->configuration->setClientId('__test_client_id__');
165+
$this->configuration->setClientId('__diff_client_id__');
167166
$this->configuration->setTokenAlgorithm('HS256');
168167
return $this->configuration;
169168
},

0 commit comments

Comments
 (0)