diff --git a/packages/core-app-api/src/apis/implementations/auth/microsoft/MicrosoftAuth.test.ts b/packages/core-app-api/src/apis/implementations/auth/microsoft/MicrosoftAuth.test.ts index 0d2e016b41..c71799ed98 100644 --- a/packages/core-app-api/src/apis/implementations/auth/microsoft/MicrosoftAuth.test.ts +++ b/packages/core-app-api/src/apis/implementations/auth/microsoft/MicrosoftAuth.test.ts @@ -69,6 +69,16 @@ describe('MicrosoftAuth', () => { expect(accessToken).toEqual('tokenForOtherResource'); }); + + it('fails when requesting scopes for multiple resources at once', async () => { + const accessTokenPromise = microsoftAuth.getAccessToken( + 'one-resource/scope other-resource/scope', + ); + + await expect(accessTokenPromise).rejects.toThrow( + 'Requested access token with scopes from multiple Azure resources: one-resource, other-resource. Access tokens can only have a single audience.', + ); + }); }); describe('without a refresh token', () => { diff --git a/packages/core-app-api/src/apis/implementations/auth/microsoft/MicrosoftAuth.ts b/packages/core-app-api/src/apis/implementations/auth/microsoft/MicrosoftAuth.ts index fa74979c6b..fdffb3fc45 100644 --- a/packages/core-app-api/src/apis/implementations/auth/microsoft/MicrosoftAuth.ts +++ b/packages/core-app-api/src/apis/implementations/auth/microsoft/MicrosoftAuth.ts @@ -91,11 +91,20 @@ export default class MicrosoftAuth { } private static resourceForScopes(scope: string): Promise { - const audience = - scope - .split(' ') - .map(MicrosoftAuth.resourceForScope) - .find(aud => aud !== 'openid') ?? MicrosoftAuth.MicrosoftGraphID; + const audiences = scope + .split(' ') + .map(MicrosoftAuth.resourceForScope) + .filter(aud => aud !== 'openid'); + if (audiences.length > 1) { + return Promise.reject( + new Error( + `Requested access token with scopes from multiple Azure resources: ${audiences.join( + ', ', + )}. Access tokens can only have a single audience.`, + ), + ); + } + const audience = audiences[0] ?? MicrosoftAuth.MicrosoftGraphID; return Promise.resolve(audience); }