Repository navigation
[v1.x] fix(client): start a new authorization when the step-up needs a scope that the token lacks - #3114
Conversation
…a scope that the token lacks A refresh cannot add scopes, so the retry of a 403 insufficient_scope got the same 403 when the client had a refresh token. The new authorization also asks for the scopes requested and granted before. Same change as modelcontextprotocol#2356 on main. Fixes modelcontextprotocol#2255.
🦋 Changeset detectedLatest commit: 33fc5f3 The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
commit: |
The 403-scope-upgrade scenario now gives its refreshed token the challenged scope, so that it still covers the refresh and the loop guard.
| } | ||
| // Keep the scopes requested and granted before, so that the new token doesn't lose them. | ||
| const tokens = await this._authProvider.tokens(); | ||
| this._scope = computeScopeUnion(this._scope, tokens?.scope, scope); |
There was a problem hiding this comment.
The 401 branch above (line 544) still does this._scope = scope, so after a step-up a later 401 narrows _scope back to whatever that challenge lists, and the new authorization would drop the scopes this union added. On main the 401 path also does this._scope = computeScopeUnion(this._scope, scope) for that reason. Could that part come along with this backport too?
There was a problem hiding this comment.
Good catch, done in 33fc5f3: the 401 branch keeps the union too, with a test where a 401 after the step-up challenges a narrower scope.
There was a problem hiding this comment.
thanks, checked 33fc5f3, the scope union is kept on a later 401 now and the new test covers it
|
|
||
| // Handle token refresh or new authorization | ||
| if (tokens?.refresh_token) { | ||
| if (tokens?.refresh_token && !forceReauthorization) { |
There was a problem hiding this comment.
For providers without redirectUrl (client_credentials, jwt-bearer), the step-up returns earlier at line 650 and fetchToken still takes the scope from provider.clientMetadata.scope, not resolvedScope, so the challenged scope never reaches prepareTokenRequest and the second 403 hits the upscoping guard. Main passes scope: resolvedScope into fetchToken there. Fine if that's meant for a separate PR, just mentioning it since the changeset reads as step-up in general.
There was a problem hiding this comment.
Done in the same commit: fetchToken() takes a scope option and auth() passes resolvedScope, as on main. This also changes the first token request of a client credentials provider when the protected resource metadata lists scopes_supported: it asks for them instead of clientMetadata.scope, as the authorization request already does on v1.x. The changeset says so.
There was a problem hiding this comment.
thanks, checked 33fc5f3, fetchToken takes a scope now and auth() passes the resolved scope through, with a test for it
…ctive token requests
Backport of the step-up part of #2356 to
v1.x. Fixes #2255.On a
403 insufficient_scope,auth()refreshes the token when the provider has a refresh token. A refresh cannot add scopes (RFC 6749 §6), so the retry gets the same 403 and the transport throws "Server returned 403 after trying upscoping". The user is never asked for the missing scope.Now the transport passes
forceReauthorizationtoauth()when the token lacks a challenged scope.auth()then starts a new authorization instead of refreshing. Like onmain, this authorization also asks for the scopes requested and granted before, so the new token keeps them (#1582 forv1.x). I left out the retry cap of #2356, and kept the existing guard against a repeated challenge. A later401keeps these scopes too, and providers without aredirectUrl(client credentials, JWT bearer) send them in their token request, as onmain(spotted by the review below).The redirects test and the
403-scope-upgradee2e scenario now give the token the challenged scope, so that they still cover a refresh. The e2e suite also gets thestepup:scope-unionandstepup:refresh-bypass-on-supersetscenarios ofmain.I found this while building an MCP server for the Symfony showcase app, with Keycloak as authorization server. With 1.32.2, the step-up refreshes the token and fails. With this branch, it asks for
openid profile email proposals:writeand the call succeeds.