From 89d13e56183b36cc45fda7ff492b93f8bf841dd0 Mon Sep 17 00:00:00 2001 From: Tomasz Szuba Date: Wed, 18 Oct 2023 14:06:23 +0200 Subject: [PATCH 1/2] Add current and defaultScopes when refreshing session Otherwise google auth always fails, as it lacks userinfo.profile Signed-off-by: Tomasz Szuba --- .changeset/red-yaks-press.md | 5 +++++ .../lib/AuthSessionManager/RefreshingAuthSessionManager.ts | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 .changeset/red-yaks-press.md diff --git a/.changeset/red-yaks-press.md b/.changeset/red-yaks-press.md new file mode 100644 index 0000000000..7166ac345e --- /dev/null +++ b/.changeset/red-yaks-press.md @@ -0,0 +1,5 @@ +--- +'@backstage/core-app-api': patch +--- + +Add current and default scopes when refreshing session diff --git a/packages/core-app-api/src/lib/AuthSessionManager/RefreshingAuthSessionManager.ts b/packages/core-app-api/src/lib/AuthSessionManager/RefreshingAuthSessionManager.ts index c04eb637de..c92fe9fb20 100644 --- a/packages/core-app-api/src/lib/AuthSessionManager/RefreshingAuthSessionManager.ts +++ b/packages/core-app-api/src/lib/AuthSessionManager/RefreshingAuthSessionManager.ts @@ -137,7 +137,9 @@ export class RefreshingAuthSessionManager implements SessionManager { return this.refreshPromise; } - this.refreshPromise = this.connector.refreshSession(scopes); + this.refreshPromise = this.connector.refreshSession( + this.helper.getExtendedScope(this.currentSession, scopes), + ); try { const session = await this.refreshPromise; From 56baf8763ad4840351a4a42fbb6b49645e0815ea Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Mon, 23 Oct 2023 17:37:18 +0200 Subject: [PATCH 2/2] core-app-api: test fixes for including default scopes in initial refresh Signed-off-by: Patrik Oldsberg --- .../auth/microsoft/MicrosoftAuth.test.ts | 12 ++++++------ .../RefreshingAuthSessionManager.test.ts | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) 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 1c51dd0698..b6d59bd3f2 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 @@ -44,9 +44,7 @@ describe('MicrosoftAuth', () => { return res( ctx.json({ providerInfo: { - accessToken: scopeParam - ? 'tokenForOtherResource' - : 'tokenForGrantScopes', + accessToken: `token:${scopeParam}`, scope: scopeParam || 'grant-resource/scope', }, }), @@ -59,7 +57,9 @@ describe('MicrosoftAuth', () => { it('gets access token with requested scopes for grant', async () => { const accessToken = await microsoftAuth.getAccessToken(); - expect(accessToken).toEqual('tokenForGrantScopes'); + expect(accessToken).toEqual( + 'token:openid offline_access profile email User.Read', + ); }); it('gets access token for other consented scopes besides those directly granted', async () => { @@ -67,7 +67,7 @@ describe('MicrosoftAuth', () => { 'azure-resource/scope', ); - expect(accessToken).toEqual('tokenForOtherResource'); + expect(accessToken).toEqual('token:azure-resource/scope offline_access'); }); it('fails when requesting scopes for multiple resources at once', async () => { @@ -86,7 +86,7 @@ describe('MicrosoftAuth', () => { ); await expect(accessTokenPromise).resolves.toEqual( - 'tokenForOtherResource', + 'token:same-resource/one-scope same-resource/other-scope offline_access', ); }); }); diff --git a/packages/core-app-api/src/lib/AuthSessionManager/RefreshingAuthSessionManager.test.ts b/packages/core-app-api/src/lib/AuthSessionManager/RefreshingAuthSessionManager.test.ts index 23432856cb..e26934f393 100644 --- a/packages/core-app-api/src/lib/AuthSessionManager/RefreshingAuthSessionManager.test.ts +++ b/packages/core-app-api/src/lib/AuthSessionManager/RefreshingAuthSessionManager.test.ts @@ -47,7 +47,7 @@ describe('RefreshingAuthSessionManager', () => { await manager.getSession({}); expect(createSession).toHaveBeenCalledTimes(1); - expect(refreshSession).toHaveBeenCalledWith(undefined); + expect(refreshSession).toHaveBeenCalledWith(new Set()); expect(stateSubscriber.mock.calls).toEqual([ [SessionState.SignedOut], [SessionState.SignedIn], @@ -134,7 +134,7 @@ describe('RefreshingAuthSessionManager', () => { expect(await manager.getSession({ optional: true })).toBe(undefined); expect(createSession).toHaveBeenCalledTimes(0); - expect(refreshSession).toHaveBeenCalledWith(undefined); + expect(refreshSession).toHaveBeenCalledWith(new Set()); }); it('should forward option to instantly show auth popup and not attempt refresh', async () => {