Commit 81f41cb
Fix cache not used for getting token if scopes are empty (#7995)
The issue occurs when `acquireTokenSilent` is called with empty scopes
(`scopes: []`). Instead of using cached tokens, the library throws a
`ClientConfigurationError` during cache lookup and makes unnecessary API
requests to Azure AD.
## Root Cause
The problem is in `ScopeSet.createSearchScopes()` which is called from
`CacheManager.getAccessToken()` during cache lookup. When empty scopes
are passed, the `ScopeSet` constructor throws an error because it
doesn't allow empty scope arrays, preventing cache lookup from
completing.
## Solution
Modified `ScopeSet.createSearchScopes()` to handle empty, null, or
undefined scopes by providing default OIDC scopes (`openid`, `profile`,
`offline_access`) before calling the constructor. This approach:
- Follows the same pattern as `RequestParameterBuilder.addScopes()`
which already handles empty scopes
- Allows cache lookup to proceed with reasonable default scopes when no
specific scopes are requested
- Maintains all existing behavior for non-empty scopes
- Eliminates unnecessary network requests when tokens are already cached
## Example
```javascript
const { instance, accounts } = useMsal();
const account = useAccount(accounts[0]);
// This now works and uses cache instead of making API calls
const response = await instance.acquireTokenSilent({
scopes: [], // Empty scopes now supported
account
});
```
The fix enables `acquireTokenSilent` to properly utilize cached tokens
when called with empty scopes, improving performance and user
experience.
Fixes #6969.
<!-- START COPILOT CODING AGENT TIPS -->
---
💬 Share your feedback on Copilot coding agent for the chance to win a
$200 gift card! Click
[here](https://survey.alchemer.com/s3/8343779/Copilot-Coding-agent) to
start the survey.
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: tnorling <5307810+tnorling@users.noreply.github.com>
Co-authored-by: Thomas Norling <thomas.norling@microsoft.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>1 parent 7fe97cf commit 81f41cb
File tree
3 files changed
+73
-2
lines changed- change
- lib/msal-common
- src/request
- test/request
3 files changed
+73
-2
lines changedLines changed: 7 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
16 | 20 | | |
17 | 21 | | |
18 | 22 | | |
| |||
61 | 65 | | |
62 | 66 | | |
63 | 67 | | |
64 | | - | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
65 | 75 | | |
66 | 76 | | |
67 | 77 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
420 | 420 | | |
421 | 421 | | |
422 | 422 | | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
423 | 477 | | |
424 | 478 | | |
425 | 479 | | |
| |||
0 commit comments