Support upscoping on insufficient_scope 403 #1115
Open
+321
−11
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request implements support for upscoping when an API request fails with a 403 Forbidden response due to insufficient_scope.
Motivation and Context
Before: When a request failed due to missing scopes, the SDK halted with a hard error, forcing users to manually determine the correct permissions and re-authenticate.
Now: We've introduced graceful error handling for insufficient_scope 403 responses. This change supports upscoping, allowing the SDK to either automatically prompt for the required scopes or guide the user, significantly enhancing resilience and user experience.
What changed (high level)
Extract scope and resource_metadata_url from WWW-Authenticate on 403 responses and use them to trigger an upscoping auth flow via the configured OAuthClientProvider.
Add a guard that records the last WWW-Authenticate header used for an upscoping attempt and prevents retrying an identical directive repeatedly; the guard is reset after successful responses.
Fixes
This resolves the issue described in:
#1039
How Has This Been Tested?
From running npm run test
Test Suites: 40 passed, 40 total
Tests: 836 passed, 836 total
Snapshots: 0 total
Time: 16.599 s
Breaking Changes
No
Types of changes
Checklist
Additional context