fix(oauth): persist the granted scope on every EMA mint

`trySilentEmaAuth` and `refreshEmaResourceTokens` saved tokens without
ever saving a scope. Both are reached from real paths -- initial EMA
authentication with a cached IdP session, refreshEnterpriseManagedTokens,
and EmaTransportOAuthProvider.tokens -- and each can be the moment a
newly configured or metadata-derived scope first takes effect, so a
silent token response left the previous stored scope standing.

Rather than a third copy of the rule, every EMA mint now ends in one
place: mintEmaResourceTokens reports the scope it requested, and
saveMintedTokens writes tokens and the RFC 6749 5.1 scope together.
The manager's EMA branch drops its own saveScope, which would now be a
redundant second write of the same value; the step-up branch keeps its
own, since it falls back to the challenge's authorizationScopes union
and runs last.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013hzwzS8UvBsr4yZm7o7sev
Signed-off-by: cliffhall <cliff@futurescale.com>
This commit is contained in:
cliffhall 2026-08-25 01:32:40 -04:00
commit c6029e1cbb
4 changed files with 104 additions and 136 deletions

View file

@ -70,6 +70,8 @@ function createMemoryStorage(
saveTokens: vi.fn(async (url: string, tokens) => { saveTokens: vi.fn(async (url: string, tokens) => {
savedTokens[url] = tokens; savedTokens[url] = tokens;
}), }),
getScope: vi.fn(async () => undefined),
saveScope: vi.fn(),
clear: vi.fn(), clear: vi.fn(),
clearEnterpriseManagedResourceServers: vi.fn(), clearEnterpriseManagedResourceServers: vi.fn(),
} as unknown as OAuthStorage; } as unknown as OAuthStorage;
@ -186,7 +188,7 @@ describe("emaFlow", () => {
const idToken = jwtWithExp(exp); const idToken = jwtWithExp(exp);
idpSessions[IDP_ISSUER] = { idToken }; idpSessions[IDP_ISSUER] = { idToken };
const tokens = await mintEmaResourceTokens( const { tokens } = await mintEmaResourceTokens(
{ ...baseConfig(storage), fetchFn: mockEmaFetch(idToken) }, { ...baseConfig(storage), fetchFn: mockEmaFetch(idToken) },
resourceContext(), resourceContext(),
); );
@ -318,7 +320,7 @@ describe("emaFlow", () => {
}, },
); );
const tokens = await mintEmaResourceTokens( const { tokens } = await mintEmaResourceTokens(
{ ...baseConfig(storage), fetchFn }, { ...baseConfig(storage), fetchFn },
resourceContext(), resourceContext(),
); );
@ -349,7 +351,7 @@ describe("emaFlow", () => {
const idToken = jwtWithExp(exp); const idToken = jwtWithExp(exp);
idpSessions[IDP_ISSUER] = { idToken }; idpSessions[IDP_ISSUER] = { idToken };
const tokens = await mintEmaResourceTokens({ const { tokens } = await mintEmaResourceTokens({
...baseConfig(storage), ...baseConfig(storage),
fetchFn: mockEmaFetch(idToken), fetchFn: mockEmaFetch(idToken),
}); });
@ -475,6 +477,45 @@ describe("emaFlow", () => {
); );
}); });
it("trySilentEmaAuth persists the scope its mint asked for (#2117)", async () => {
// The silent path is how an initial EMA authentication completes when the
// IdP session is still cached, so it is a moment a newly configured scope
// first takes effect -- and the mock AS answers without a `scope`.
const idToken = jwtWithExp(Math.floor(Date.now() / 1000) + 3600);
const storage = createMemoryStorage({ [IDP_ISSUER]: { idToken } });
const fetchFn = mockEmaFetch(idToken);
const result = await trySilentEmaAuth({
...baseConfig(storage),
scope: "mcp weather:read",
fetchFn,
});
expect(result.status).toBe("success");
expect(storage.saveScope).toHaveBeenCalledWith(
SERVER_URL,
"mcp weather:read",
);
});
it("refreshEmaResourceTokens persists the scope its mint asked for (#2117)", async () => {
const idToken = jwtWithExp(Math.floor(Date.now() / 1000) + 3600);
const storage = createMemoryStorage({ [IDP_ISSUER]: { idToken } });
const fetchFn = mockEmaFetch(idToken);
const tokens = await refreshEmaResourceTokens({
...baseConfig(storage),
scope: "mcp weather:read",
fetchFn,
});
expect(tokens?.access_token).toBe("resource-access-token");
expect(storage.saveScope).toHaveBeenCalledWith(
SERVER_URL,
"mcp weather:read",
);
});
it("refreshEmaResourceTokens returns undefined without an IdP session", async () => { it("refreshEmaResourceTokens returns undefined without an IdP session", async () => {
expect(await refreshEmaResourceTokens(baseConfig(storage))).toBeUndefined(); expect(await refreshEmaResourceTokens(baseConfig(storage))).toBeUndefined();
}); });

View file

@ -890,74 +890,6 @@ describe("OAuthManager", () => {
expect(manager.getOAuthFlowStep()).toBe("complete"); expect(manager.getOAuthFlowStep()).toBe("complete");
mintSpy.mockRestore(); mintSpy.mockRestore();
}); });
it("persists the minted scope request when the mint response omits scope (#2117)", async () => {
const params = createMockParams({
enterpriseManagedAuth: {
idp: {
issuer: "https://idp.example.com",
clientId: "app-client",
clientSecret: "secret",
},
},
});
params.initialConfig.scope = "mcp weather:read";
storageOf(params).getScope.mockResolvedValue(undefined);
const manager = new OAuthManager(params);
manager.setOAuthConfig({ enterpriseManaged: true });
const mintSpy = vi
.spyOn(emaFlow, "completeEmaIdpAuthorizationAndMint")
.mockResolvedValue({
tokens: { access_token: "EMA", token_type: "Bearer" },
requestedScope: "mcp weather:read",
});
storageOf(params).saveScope.mockClear();
await manager.completeOAuthFlow("ema-code");
expect(storageOf(params).saveScope).toHaveBeenCalledWith(
SERVER_URL,
"mcp weather:read",
);
mintSpy.mockRestore();
});
it("persists a mint scope that came from resource metadata, not config (#2117)", async () => {
// With neither a configured nor a stored scope, discoverEmaResourceContext
// resolves the request from the protected resource metadata's
// `scopes_supported`. That value never reaches the manager's own
// `scopeForMint`, so only what the mint reports can be persisted.
const params = createMockParams({
enterpriseManagedAuth: {
idp: {
issuer: "https://idp.example.com",
clientId: "app-client",
clientSecret: "secret",
},
},
});
params.initialConfig.scope = undefined;
storageOf(params).getScope.mockResolvedValue(undefined);
const manager = new OAuthManager(params);
manager.setOAuthConfig({ enterpriseManaged: true });
const mintSpy = vi
.spyOn(emaFlow, "completeEmaIdpAuthorizationAndMint")
.mockResolvedValue({
tokens: { access_token: "EMA", token_type: "Bearer" },
requestedScope: "mcp:read mcp:write",
});
storageOf(params).saveScope.mockClear();
await manager.completeOAuthFlow("ema-code");
expect(storageOf(params).saveScope).toHaveBeenCalledWith(
SERVER_URL,
"mcp:read mcp:write",
);
mintSpy.mockRestore();
});
}); });
describe("trySilentEnterpriseManagedAuth", () => { describe("trySilentEnterpriseManagedAuth", () => {
@ -1823,10 +1755,9 @@ describe("OAuthManager", () => {
"auth-code", "auth-code",
"https://idp.example.com", "https://idp.example.com",
); );
expect(storageOf(params).saveScope).toHaveBeenCalledWith( // Persisting the granted scope is the EMA flow's job (saveMintedTokens),
SERVER_URL, // and it is mocked here -- the union reaching its config, asserted just
"mcp tools:read weather:read", // above, is what this test owns. emaFlow.test.ts covers the persistence.
);
silentSpy.mockRestore(); silentSpy.mockRestore();
startSpy.mockRestore(); startSpy.mockRestore();

View file

@ -11,6 +11,7 @@ import {
type EmaResourceContext, type EmaResourceContext,
} from "./resourceContext.js"; } from "./resourceContext.js";
import { exchangeIdJag, redeemIdJagForAccessToken } from "./wire.js"; import { exchangeIdJag, redeemIdJagForAccessToken } from "./wire.js";
import { resolvePersistedScopeAfterGrant } from "../scopes.js";
export interface EmaFlowConfig { export interface EmaFlowConfig {
serverUrl: string; serverUrl: string;
@ -35,10 +36,24 @@ function wrapEmaMintError(err: unknown): Error {
}); });
} }
/**
* A completed EMA mint, and the scope it actually asked for.
*
* The request is not always `config.scope`: with neither a configured nor a
* stored scope, {@link discoverEmaResourceContext} resolves it from the
* protected resource metadata's `scopes_supported`, and that is what both
* exchanges send. Callers need the resolved value to apply RFC 6749 §5.1 — a
* token response that omits `scope` granted exactly what was requested.
*/
export interface EmaMintResult {
tokens: OAuthTokens;
requestedScope?: string;
}
export async function mintEmaResourceTokens( export async function mintEmaResourceTokens(
config: EmaFlowConfig, config: EmaFlowConfig,
resourceContext?: EmaResourceContext, resourceContext?: EmaResourceContext,
): Promise<OAuthTokens> { ): Promise<EmaMintResult> {
const ctx = const ctx =
resourceContext ?? resourceContext ??
(await discoverEmaResourceContext( (await discoverEmaResourceContext(
@ -78,7 +93,7 @@ export async function mintEmaResourceTokens(
fetchFn: config.fetchFn, fetchFn: config.fetchFn,
}); });
return redeemIdJagForAccessToken({ const tokens = await redeemIdJagForAccessToken({
resourceAsUrl: ctx.resourceAsUrl, resourceAsUrl: ctx.resourceAsUrl,
idJag, idJag,
resourceClientId: config.resourceClientId, resourceClientId: config.resourceClientId,
@ -87,6 +102,31 @@ export async function mintEmaResourceTokens(
scope: ctx.scope, scope: ctx.scope,
fetchFn: config.fetchFn, fetchFn: config.fetchFn,
}); });
return { tokens, requestedScope: ctx.scope };
}
/**
* Save a mint's tokens and the scope they carry, together.
*
* Every EMA mint ends here — silent, refresh, and interactive alike — because
* all three can be the moment a newly configured or metadata-derived scope
* first takes effect. Persisting tokens without the scope leaves the previous
* stored value standing as though it described the new token (#2117).
*/
async function saveMintedTokens(
config: EmaFlowConfig,
{ tokens, requestedScope }: EmaMintResult,
): Promise<void> {
await config.storage.saveTokens(config.serverUrl, tokens, {
enterpriseManaged: true,
});
const scopeToPersist = resolvePersistedScopeAfterGrant(
tokens.scope,
requestedScope,
);
if (scopeToPersist) {
await config.storage.saveScope(config.serverUrl, scopeToPersist);
}
} }
/** Silent path: cached IdP session + legs 2–3. */ /** Silent path: cached IdP session + legs 2–3. */
@ -102,10 +142,7 @@ export async function trySilentEmaAuth(
return { status: "no_idp_session" }; return { status: "no_idp_session" };
} }
try { try {
const tokens = await mintEmaResourceTokens(config); await saveMintedTokens(config, await mintEmaResourceTokens(config));
await config.storage.saveTokens(config.serverUrl, tokens, {
enterpriseManaged: true,
});
return { status: "success" }; return { status: "success" };
} catch (err) { } catch (err) {
return { status: "mint_failed", error: wrapEmaMintError(err) }; return { status: "mint_failed", error: wrapEmaMintError(err) };
@ -124,26 +161,11 @@ export async function startEmaIdpAuthorization(
return authorizationUrl; return authorizationUrl;
} }
/**
* The outcome of a completed EMA authorization.
*
* `requestedScope` is the scope the mint actually asked for, which is not
* always `config.scope`: when neither the server config nor storage supplies
* one, {@link discoverEmaResourceContext} resolves it from the protected
* resource metadata's `scopes_supported`. Callers need that resolved value to
* apply RFC 6749 §5.1 — a token response that omits `scope` granted exactly
* what was requested — so it is returned rather than left inside the flow.
*/
export interface EmaAuthorizationResult {
tokens: OAuthTokens;
requestedScope?: string;
}
export async function completeEmaIdpAuthorizationAndMint( export async function completeEmaIdpAuthorizationAndMint(
config: EmaFlowConfig, config: EmaFlowConfig,
authorizationCode: string, authorizationCode: string,
iss?: string, iss?: string,
): Promise<EmaAuthorizationResult> { ): Promise<EmaMintResult> {
try { try {
await completeIdpOidcAuthorization({ await completeIdpOidcAuthorization({
idp: config.idp, idp: config.idp,
@ -160,26 +182,15 @@ export async function completeEmaIdpAuthorizationAndMint(
}); });
} }
let tokens: OAuthTokens; let minted: EmaMintResult;
// Resolve the resource context here rather than inside the mint, so the
// scope it settles on is visible to the caller. It is passed straight
// through, so this costs no extra discovery.
let resourceContext: EmaResourceContext;
try { try {
resourceContext = await discoverEmaResourceContext( minted = await mintEmaResourceTokens(config);
config.serverUrl,
config.scope,
config.fetchFn,
);
tokens = await mintEmaResourceTokens(config, resourceContext);
} catch (err) { } catch (err) {
throw wrapEmaMintError(err); throw wrapEmaMintError(err);
} }
await config.storage.saveTokens(config.serverUrl, tokens, { await saveMintedTokens(config, minted);
enterpriseManaged: true, return minted;
});
return { tokens, requestedScope: resourceContext.scope };
} }
/** Re-run legs 2–3 on 401 when IdP session is still valid. */ /** Re-run legs 2–3 on 401 when IdP session is still valid. */
@ -194,9 +205,7 @@ export async function refreshEmaResourceTokens(
if (!idToken) { if (!idToken) {
return undefined; return undefined;
} }
const tokens = await mintEmaResourceTokens(config); const minted = await mintEmaResourceTokens(config);
await config.storage.saveTokens(config.serverUrl, tokens, { await saveMintedTokens(config, minted);
enterpriseManaged: true, return minted.tokens;
});
return tokens;
} }

View file

@ -376,26 +376,13 @@ export class OAuthManager {
const config = scopeForMint const config = scopeForMint
? { ...emaConfig, scope: scopeForMint } ? { ...emaConfig, scope: scopeForMint }
: emaConfig; : emaConfig;
const { tokens, requestedScope } = // The EMA flow persists the granted scope alongside the tokens, for
await completeEmaIdpAuthorizationAndMint( // every mint rather than only this one — see saveMintedTokens.
config, const { tokens } = await completeEmaIdpAuthorizationAndMint(
authorizationCode, config,
iss, authorizationCode,
); iss,
// The mint reports what it actually asked for, which is neither
// `pendingAuthorizationScope` (undefined on the ordinary leg) nor
// always `scopeForMint` — with no configured or stored scope the
// resource metadata's `scopes_supported` is what gets requested.
const scopeToPersist = resolvePersistedScopeAfterGrant(
tokens.scope,
requestedScope,
); );
if (scopeToPersist) {
await this.requireStorage().saveScope(
this.getServerUrl(),
scopeToPersist,
);
}
this.pendingAuthorizationScope = undefined; this.pendingAuthorizationScope = undefined;
const completedAt = Date.now(); const completedAt = Date.now();
this.oauthFlowState = { this.oauthFlowState = {