From e262738b8a0cd43ce4cfea116adc05aef8c9061c Mon Sep 17 00:00:00 2001 From: Adi Krhan Date: Mon, 13 Mar 2023 09:23:59 +0100 Subject: [PATCH] Handle token difference in Microsoft provider Signed-off-by: Adi Krhan --- .changeset/many-carpets-act.md | 5 +++++ plugins/auth-backend/src/providers/microsoft/provider.ts | 9 ++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 .changeset/many-carpets-act.md diff --git a/.changeset/many-carpets-act.md b/.changeset/many-carpets-act.md new file mode 100644 index 0000000000..5285eeebbd --- /dev/null +++ b/.changeset/many-carpets-act.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-auth-backend': patch +--- + +Handle difference in expiration time between Microsoft session and Backstage session which caused the Backstage token to be invalid during a time frame. diff --git a/plugins/auth-backend/src/providers/microsoft/provider.ts b/plugins/auth-backend/src/providers/microsoft/provider.ts index fd2a1529a7..9b14a3c91b 100644 --- a/plugins/auth-backend/src/providers/microsoft/provider.ts +++ b/plugins/auth-backend/src/providers/microsoft/provider.ts @@ -50,6 +50,8 @@ import { import { Logger } from 'winston'; import fetch from 'node-fetch'; +const BACKSTAGE_SESSION_EXPIRATION = 3600; + type PrivateInfo = { refreshToken: string; }; @@ -145,12 +147,17 @@ export class MicrosoftAuthProvider implements OAuthHandlers { const { profile } = await this.authHandler(result, this.resolverContext); + const expiresInSeconds = + result.params.expires_in === undefined + ? BACKSTAGE_SESSION_EXPIRATION + : Math.min(result.params.expires_in, BACKSTAGE_SESSION_EXPIRATION); + const response: OAuthResponse = { providerInfo: { idToken: result.params.id_token, accessToken: result.accessToken, scope: result.params.scope, - expiresInSeconds: result.params.expires_in, + expiresInSeconds, }, profile, };