diff --git a/packages/core-app-api/api-report.md b/packages/core-app-api/api-report.md index e2c6b3a939..20d8a5a770 100644 --- a/packages/core-app-api/api-report.md +++ b/packages/core-app-api/api-report.md @@ -394,7 +394,7 @@ export type GithubSession = { providerInfo: { accessToken: string; scopes: Set; - expiresAt: Date; + expiresAt?: Date; }; profile: ProfileInfo; backstageIdentity: BackstageIdentity; diff --git a/packages/core-app-api/src/apis/implementations/auth/github/GithubAuth.ts b/packages/core-app-api/src/apis/implementations/auth/github/GithubAuth.ts index 21ebc5699a..85e3e79ad5 100644 --- a/packages/core-app-api/src/apis/implementations/auth/github/GithubAuth.ts +++ b/packages/core-app-api/src/apis/implementations/auth/github/GithubAuth.ts @@ -29,15 +29,17 @@ import { import { SessionManager } from '../../../../lib/AuthSessionManager/types'; import { AuthSessionStore, + RefreshingAuthSessionManager, StaticAuthSessionManager, } from '../../../../lib/AuthSessionManager'; import { OAuthApiCreateOptions } from '../types'; +import { OptionalRefreshSessionManagerMux } from '../../../../lib/AuthSessionManager/OptionalRefreshSessionManagerMux'; export type GithubAuthResponse = { providerInfo: { accessToken: string; scope: string; - expiresInSeconds: number; + expiresInSeconds?: number; }; profile: ProfileInfo; backstageIdentity: BackstageIdentity; @@ -68,27 +70,46 @@ class GithubAuth implements OAuthApi, SessionApi { providerInfo: { accessToken: res.providerInfo.accessToken, scopes: GithubAuth.normalizeScope(res.providerInfo.scope), - expiresAt: new Date( - Date.now() + res.providerInfo.expiresInSeconds * 1000, - ), + expiresAt: res.providerInfo.expiresInSeconds + ? new Date(Date.now() + res.providerInfo.expiresInSeconds * 1000) + : undefined, }, }; }, }); - const sessionManager = new StaticAuthSessionManager({ + const refreshingSessionManager = new RefreshingAuthSessionManager({ connector, defaultScopes: new Set(defaultScopes), sessionScopes: (session: GithubSession) => session.providerInfo.scopes, + sessionShouldRefresh: (session: GithubSession) => { + const { expiresAt } = session.providerInfo; + if (!expiresAt) { + return false; + } + const expiresInSec = (expiresAt.getTime() - Date.now()) / 1000; + return expiresInSec < 60 * 5; + }, }); - const authSessionStore = new AuthSessionStore({ - manager: sessionManager, + const staticSessionManager = new AuthSessionStore({ + manager: new StaticAuthSessionManager({ + connector, + defaultScopes: new Set(defaultScopes), + sessionScopes: (session: GithubSession) => session.providerInfo.scopes, + }), storageKey: `${provider.id}Session`, sessionScopes: (session: GithubSession) => session.providerInfo.scopes, }); - return new GithubAuth(authSessionStore); + const sessionManagerMux = new OptionalRefreshSessionManagerMux({ + refreshingSessionManager, + staticSessionManager, + sessionCanRefresh: session => + session.providerInfo.expiresAt !== undefined, + }); + + return new GithubAuth(sessionManagerMux); } constructor(private readonly sessionManager: SessionManager) {} diff --git a/packages/core-app-api/src/apis/implementations/auth/github/types.ts b/packages/core-app-api/src/apis/implementations/auth/github/types.ts index 88df25b49d..f5dae3a064 100644 --- a/packages/core-app-api/src/apis/implementations/auth/github/types.ts +++ b/packages/core-app-api/src/apis/implementations/auth/github/types.ts @@ -20,7 +20,7 @@ export type GithubSession = { providerInfo: { accessToken: string; scopes: Set; - expiresAt: Date; + expiresAt?: Date; }; profile: ProfileInfo; backstageIdentity: BackstageIdentity;