From c5a12022d88d63e52e78c4a847fabeba02330988 Mon Sep 17 00:00:00 2001 From: Jamie Klassen Date: Wed, 8 Feb 2023 15:26:20 -0500 Subject: [PATCH] refactor MicrosoftAuth to allow overriding some behaviors in subsequent commits Signed-off-by: Jamie Klassen --- packages/core-app-api/api-report.md | 19 +++++ .../auth/microsoft/MicrosoftAuth.ts | 75 ++++++++++++++++--- 2 files changed, 85 insertions(+), 9 deletions(-) diff --git a/packages/core-app-api/api-report.md b/packages/core-app-api/api-report.md index d7e4a22873..f2df03d806 100644 --- a/packages/core-app-api/api-report.md +++ b/packages/core-app-api/api-report.md @@ -442,6 +442,25 @@ export class LocalStorageFeatureFlags implements FeatureFlagsApi { export class MicrosoftAuth { // (undocumented) static create(options: OAuthApiCreateOptions): typeof microsoftAuthApiRef.T; + // (undocumented) + getAccessToken( + scope?: string | string[], + options?: AuthRequestOptions, + ): Promise; + // (undocumented) + getBackstageIdentity( + options?: AuthRequestOptions, + ): Promise; + // (undocumented) + getIdToken(options?: AuthRequestOptions): Promise; + // (undocumented) + getProfile(options?: AuthRequestOptions): Promise; + // (undocumented) + sessionState$(): Observable; + // (undocumented) + signIn(): Promise; + // (undocumented) + signOut(): Promise; } // @public 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 30cfde7c6e..3f08ecaac7 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 @@ -14,7 +14,13 @@ * limitations under the License. */ -import { microsoftAuthApiRef } from '@backstage/core-plugin-api'; +import { + microsoftAuthApiRef, + AuthRequestOptions, + AuthProviderInfo, + DiscoveryApi, + OAuthRequestApi, +} from '@backstage/core-plugin-api'; import { OAuth2 } from '../oauth2'; import { OAuthApiCreateOptions } from '../types'; @@ -30,7 +36,18 @@ const DEFAULT_PROVIDER = { * @public */ export default class MicrosoftAuth { + private oauth2: Record; + private environment: string; + private provider: AuthProviderInfo; + private oauthRequestApi: OAuthRequestApi; + private discoveryApi: DiscoveryApi; + + private static MicrosoftGraphID = '00000003-0000-0000-c000-000000000000'; + static create(options: OAuthApiCreateOptions): typeof microsoftAuthApiRef.T { + return new MicrosoftAuth(options); + } + private constructor(options: OAuthApiCreateOptions) { const { configApi, environment = 'development', @@ -46,13 +63,53 @@ export default class MicrosoftAuth { ], } = options; - return OAuth2.create({ - configApi, - discoveryApi, - oauthRequestApi, - provider, - environment, - defaultScopes, - }); + this.configApi = configApi; + this.environment = environment; + this.provider = provider; + this.oauthRequestApi = oauthRequestApi; + this.discoveryApi = discoveryApi; + + this.oauth2 = { + [MicrosoftAuth.MicrosoftGraphID]: OAuth2.create({ + configApi: this.configApi, + discoveryApi: this.discoveryApi, + oauthRequestApi: this.oauthRequestApi, + provider: this.provider, + environment: this.environment, + defaultScopes, + }), + }; + } + + private microsoftGraph(): OAuth2 { + return this.oauth2[MicrosoftAuth.MicrosoftGraphID]; + } + + getAccessToken(scope?: string | string[], options?: AuthRequestOptions) { + return this.microsoftGraph().getAccessToken(scope, options); + } + + getIdToken(options?: AuthRequestOptions) { + return this.microsoftGraph().getIdToken(options); + } + + getProfile(options?: AuthRequestOptions) { + return this.microsoftGraph().getProfile(options); + } + + getBackstageIdentity(options?: AuthRequestOptions) { + return this.microsoftGraph().getBackstageIdentity(options); + } + + signIn() { + return this.microsoftGraph().signIn(); + } + + signOut() { + return this.microsoftGraph().signOut(); + } + + sessionState$() { + return this.microsoftGraph().sessionState$(); } }