Add PBandGraphOauth scaffold and auth UI
This commit is contained in:
Generated
Vendored
+18
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* -------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
|
||||
* See License in the project root for license information.
|
||||
* -------------------------------------------------------------------------------------------
|
||||
*/
|
||||
import { GetTokenOptions } from "@azure/identity";
|
||||
|
||||
import { AuthenticationProviderOptions } from "../../IAuthenticationProviderOptions";
|
||||
|
||||
/**
|
||||
* @interface
|
||||
* A signature represents the Authentication provider options for Token Credentials
|
||||
* @property {getTokenOptions} [GetTokenOptions] - Defines options for TokenCredential.getToken.
|
||||
*/
|
||||
export interface TokenCredentialAuthenticationProviderOptions extends AuthenticationProviderOptions {
|
||||
getTokenOptions?: GetTokenOptions;
|
||||
}
|
||||
Generated
Vendored
+81
@@ -0,0 +1,81 @@
|
||||
/**
|
||||
* -------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
|
||||
* See License in the project root for license information.
|
||||
* -------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
import { TokenCredential } from "@azure/identity";
|
||||
|
||||
import { GraphClientError } from "../../GraphClientError";
|
||||
import { AuthenticationProvider } from "../../IAuthenticationProvider";
|
||||
import { TokenCredentialAuthenticationProviderOptions } from "./ITokenCredentialAuthenticationProviderOptions";
|
||||
|
||||
/**
|
||||
* @module TokenCredentialAuthenticationProvider
|
||||
*/
|
||||
|
||||
/**
|
||||
* @class
|
||||
* Class representing TokenCredentialAuthenticationProvider
|
||||
* This feature is introduced in Version 3.0.0
|
||||
* @extends AuthenticationProvider
|
||||
* Reference - https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/identity/identity/README.md
|
||||
*/
|
||||
export class TokenCredentialAuthenticationProvider implements AuthenticationProvider {
|
||||
/**
|
||||
* @private
|
||||
* A member holding an instance of @azure/identity TokenCredential
|
||||
*/
|
||||
private tokenCredential: TokenCredential;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* A member holding an instance of TokenCredentialAuthenticationProviderOptions
|
||||
*/
|
||||
private authenticationProviderOptions: TokenCredentialAuthenticationProviderOptions;
|
||||
|
||||
/**
|
||||
* @public
|
||||
* @constructor
|
||||
* Creates an instance of TokenCredentialAuthenticationProvider
|
||||
* @param {TokenCredential} tokenCredential - An instance of @azure/identity TokenCredential
|
||||
* @param {TokenCredentialAuthenticationProviderOptions} authenticationProviderOptions - An instance of TokenCredentialAuthenticationProviderOptions
|
||||
* @returns An instance of TokenCredentialAuthenticationProvider
|
||||
*/
|
||||
public constructor(tokenCredential: TokenCredential, authenticationProviderOptions: TokenCredentialAuthenticationProviderOptions) {
|
||||
if (!tokenCredential) {
|
||||
throw new GraphClientError("Please pass a token credential object to the TokenCredentialAuthenticationProvider class constructor");
|
||||
}
|
||||
if (!authenticationProviderOptions) {
|
||||
throw new GraphClientError("Please pass the TokenCredentialAuthenticationProviderOptions with scopes to the TokenCredentialAuthenticationProvider class constructor");
|
||||
}
|
||||
this.authenticationProviderOptions = authenticationProviderOptions;
|
||||
this.tokenCredential = tokenCredential;
|
||||
}
|
||||
|
||||
/**
|
||||
* @public
|
||||
* @async
|
||||
* To get the access token
|
||||
* @param {TokenCredentialAuthenticationProviderOptions} authenticationProviderOptions - The authentication provider options object
|
||||
* @returns The promise that resolves to an access token
|
||||
*/
|
||||
public async getAccessToken(): Promise<string> {
|
||||
const scopes = this.authenticationProviderOptions.scopes;
|
||||
const error = new GraphClientError();
|
||||
|
||||
if (!scopes || scopes.length === 0) {
|
||||
error.name = "Empty Scopes";
|
||||
error.message = "Scopes cannot be empty, Please provide scopes";
|
||||
throw error;
|
||||
}
|
||||
const response = await this.tokenCredential.getToken(scopes, this.authenticationProviderOptions.getTokenOptions);
|
||||
if (response) {
|
||||
return response.token;
|
||||
}
|
||||
error.message = "Cannot retrieve accessToken from the Token Credential object";
|
||||
error.name = "Access token is undefined";
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
Generated
Vendored
+8
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
* -------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
|
||||
* See License in the project root for license information.
|
||||
* -------------------------------------------------------------------------------------------
|
||||
*/
|
||||
export { TokenCredentialAuthenticationProviderOptions } from "./ITokenCredentialAuthenticationProviderOptions";
|
||||
export { TokenCredentialAuthenticationProvider } from "./TokenCredentialAuthenticationProvider";
|
||||
Generated
Vendored
+78
@@ -0,0 +1,78 @@
|
||||
/**
|
||||
* -------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
|
||||
* See License in the project root for license information.
|
||||
* -------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/**
|
||||
* @module AuthCodeMSALBrowserAuthenticationProvider
|
||||
*/
|
||||
|
||||
import { AuthenticationResult, InteractionRequiredAuthError, InteractionType, PublicClientApplication } from "@azure/msal-browser";
|
||||
|
||||
import { GraphClientError } from "../../GraphClientError";
|
||||
import { AuthenticationProvider } from "../../IAuthenticationProvider";
|
||||
import { AuthCodeMSALBrowserAuthenticationProviderOptions } from "../msalOptions/MSALAuthenticationProviderOptions";
|
||||
|
||||
/**
|
||||
* an AuthenticationProvider implementation supporting msal-browser library.
|
||||
* This feature is introduced in Version 3.0.0
|
||||
* @class
|
||||
* @extends AuthenticationProvider
|
||||
*/
|
||||
export class AuthCodeMSALBrowserAuthenticationProvider implements AuthenticationProvider {
|
||||
/**
|
||||
* @public
|
||||
* @constructor
|
||||
* Creates an instance of ImplicitMSALAuthenticationProvider
|
||||
* @param {PublicClientApplication} msalApplication - An instance of MSAL PublicClientApplication
|
||||
* @param {AuthCodeMSALBrowserAuthenticationProviderOptions} options - An instance of MSALAuthenticationProviderOptions
|
||||
* @returns An instance of ImplicitMSALAuthenticationProvider
|
||||
*/
|
||||
public constructor(private publicClientApplication: PublicClientApplication, private options: AuthCodeMSALBrowserAuthenticationProviderOptions) {
|
||||
if (!options || !publicClientApplication) {
|
||||
throw new GraphClientError("Please pass valid PublicClientApplication instance and AuthCodeMSALBrowserAuthenticationProviderOptions instance to instantiate MSALBrowserAuthenticationProvider");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @public
|
||||
* @async
|
||||
* To get the access token for the request
|
||||
* @returns The promise that resolves to an access token
|
||||
*/
|
||||
public async getAccessToken(): Promise<string> {
|
||||
const scopes = this.options && this.options.scopes;
|
||||
const account = this.options && this.options.account;
|
||||
const error = new GraphClientError();
|
||||
if (!scopes || scopes.length === 0) {
|
||||
error.name = "Empty Scopes";
|
||||
error.message = "Scopes cannot be empty, Please provide scopes";
|
||||
throw error;
|
||||
}
|
||||
try {
|
||||
const response: AuthenticationResult = await this.publicClientApplication.acquireTokenSilent({
|
||||
scopes,
|
||||
account,
|
||||
});
|
||||
if (!response || !response.accessToken) {
|
||||
error.name = "Access token is undefined";
|
||||
error.message = "Received empty access token from PublicClientApplication";
|
||||
throw error;
|
||||
}
|
||||
return response.accessToken;
|
||||
} catch (error) {
|
||||
if (error instanceof InteractionRequiredAuthError) {
|
||||
if (this.options.interactionType === InteractionType.Redirect) {
|
||||
this.publicClientApplication.acquireTokenRedirect({ scopes });
|
||||
} else if (this.options.interactionType === InteractionType.Popup) {
|
||||
const response: AuthenticationResult = await this.publicClientApplication.acquireTokenPopup({ scopes });
|
||||
return response.accessToken;
|
||||
}
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Generated
Vendored
+7
@@ -0,0 +1,7 @@
|
||||
/**
|
||||
* -------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
|
||||
* See License in the project root for license information.
|
||||
* -------------------------------------------------------------------------------------------
|
||||
*/
|
||||
export { AuthCodeMSALBrowserAuthenticationProvider } from "./AuthCodeMSALBrowserAuthenticationProvider";
|
||||
Generated
Vendored
+20
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* -------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
|
||||
* See License in the project root for license information.
|
||||
* -------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/**
|
||||
* @module MSALAuthenticationProviderOptions
|
||||
*/
|
||||
|
||||
import { AccountInfo, InteractionType } from "@azure/msal-browser";
|
||||
|
||||
import { AuthenticationProviderOptions } from "../../IAuthenticationProviderOptions";
|
||||
|
||||
export interface AuthCodeMSALBrowserAuthenticationProviderOptions extends AuthenticationProviderOptions {
|
||||
scopes: string[];
|
||||
account: AccountInfo;
|
||||
interactionType: InteractionType;
|
||||
}
|
||||
Reference in New Issue
Block a user