refactor MicrosoftAuth

to allow overriding some behaviors in subsequent commits

Signed-off-by: Jamie Klassen <jklassen@vmware.com>
This commit is contained in:
Jamie Klassen
2023-02-08 15:26:20 -05:00
parent 475abd1dc3
commit c5a12022d8
2 changed files with 85 additions and 9 deletions
+19
View File
@@ -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<string>;
// (undocumented)
getBackstageIdentity(
options?: AuthRequestOptions,
): Promise<BackstageIdentityResponse | undefined>;
// (undocumented)
getIdToken(options?: AuthRequestOptions): Promise<string>;
// (undocumented)
getProfile(options?: AuthRequestOptions): Promise<ProfileInfo | undefined>;
// (undocumented)
sessionState$(): Observable<SessionState>;
// (undocumented)
signIn(): Promise<void>;
// (undocumented)
signOut(): Promise<void>;
}
// @public
@@ -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<string, OAuth2>;
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$();
}
}