@mattbillfred/mgt-msal2-provider
Version:
The Microsoft Graph Toolkit Msal 2.0 Provider
210 lines (183 loc) • 4.61 kB
text/typescript
/**
* -------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/
import { property } from 'lit/decorators.js';
import { Providers, LoginType, MgtBaseProvider, registerComponent } from '@microsoft/mgt-element';
import { Msal2Config, Msal2Provider, PromptType } from './Msal2Provider';
export const registerMgtMsal2Provider = () => {
registerComponent('msal2-provider', MgtMsal2Provider);
};
/**
* Authentication Library Provider for Microsoft personal accounts
*
* @export
* @class MgtMsalProvider
* @extends {MgtBaseProvider}
*/
class MgtMsal2Provider extends MgtBaseProvider {
/**
* String alphanumerical value relation to a specific user
*
* @memberof MgtMsalProvider
*/
public clientId = '';
/**
* login hint string
*
* @memberof MgtMsal2Provider
*/
public loginHint: string;
/**
* domain hint string
*
* @memberof MgtMsal2Provider
*/
public domainHint: string;
/**
* The login type that should be used: popup or redirect
*
* @memberof MgtMsal2Provider
*/
public loginType: string;
/**
* The authority to use.
*
* @memberof MgtMsal2Provider
*/
public authority: string;
/**
* Comma separated list of scopes
*
* @memberof MgtMsal2Provider
*/
public scopes: string;
/**
* The redirect uri to use
*
* @memberof MgtMsal2Provider
*/
public redirectUri: string;
/**
* Type of prompt for login
*
* @memberof MgtMsal2Provider
*/
public prompt: string;
/**
* Disables incremental consent
*
* @memberof MgtMsal2Provider
*/
public isIncrementalConsentDisabled: boolean;
/**
* Disables multiple account capability
*
* @memberof MgtMsal2Provider
*/
public isMultiAccountDisabled;
/**
* Gets whether this provider can be used in this environment
*
* @readonly
* @memberof MgtMsal2Provider
*/
public get isAvailable() {
return true;
}
/**
* method called to initialize the provider. Each derived class should provide their own implementation.
*
* @protected
* @memberof MgtMsal2Provider
*/
protected initializeProvider() {
if (this.clientId) {
const config: Msal2Config = {
clientId: this.clientId
};
if (this.loginType && this.loginType.length > 1) {
let loginType: string = this.loginType.toLowerCase();
loginType = loginType[0].toUpperCase() + loginType.slice(1);
const loginTypeEnum = LoginType[loginType] as LoginType;
config.loginType = loginTypeEnum;
}
if (this.authority) {
config.authority = this.authority;
}
if (this.scopes) {
const scope = this.scopes.split(',');
if (scope && scope.length > 0) {
config.scopes = scope;
}
}
if (this.redirectUri) {
config.redirectUri = this.redirectUri;
}
if (this.loginHint) {
config.loginHint = this.loginHint;
}
if (this.domainHint) {
config.domainHint = this.domainHint;
}
if (this.prompt) {
const prompt: string = this.prompt.toUpperCase();
const promptEnum = PromptType[prompt] as PromptType;
config.prompt = promptEnum;
}
if (this.isIncrementalConsentDisabled) {
config.isIncrementalConsentDisabled = true;
}
if (this.isMultiAccountDisabled) {
config.isMultiAccountEnabled = false;
}
if (this.baseUrl) {
config.baseURL = this.baseUrl;
}
if (this.customHosts) {
config.customHosts = this.customHosts;
}
this.provider = new Msal2Provider(config);
Providers.globalProvider = this.provider;
}
}
}