@microsoft/mgt
Version:
The Microsoft Graph Toolkit
116 lines • 4.6 kB
JavaScript
/**
* -------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { Graph } from '../Graph';
import { IProvider, ProviderState } from './IProvider';
/**
* Wam provider authentication
*
* @export
* @class WamProvider
* @extends {IProvider}
*/
export class WamProvider extends IProvider {
constructor(clientId, authority) {
super();
this.graphResource = 'https://graph.microsoft.com';
this.clientId = clientId;
this.authority = authority || 'https://login.microsoftonline.com/common';
this.graph = new Graph(this);
this.printRedirectUriToConsole();
}
/**
* returns if Windows is the OS based on window
*
* @readonly
* @static
* @type {boolean}
* @memberof WamProvider
*/
static get isAvailable() {
return !!window.Windows;
}
/**
* returns accessToken
*
* @readonly
* @type {boolean}
* @memberof WamProvider
*/
get isLoggedIn() {
return !!this.accessToken;
}
/**
* login promise for the WamProvider
*
* @returns {Promise<void>}
* @memberof WamProvider
*/
login() {
return __awaiter(this, void 0, void 0, function* () {
if (WamProvider.isAvailable) {
const webCore = window.Windows.Security.Authentication.Web.Core;
const wap = yield webCore.WebAuthenticationCoreManager.findAccountProviderAsync('https://login.microsoft.com', this.authority);
if (!wap) {
return;
}
const wtr = new webCore.WebTokenRequest(wap, '', this.clientId);
wtr.properties.insert('resource', this.graphResource);
const wtrr = yield webCore.WebAuthenticationCoreManager.requestTokenAsync(wtr);
switch (wtrr.responseStatus) {
case webCore.WebTokenRequestStatus.success:
const account = wtrr.responseData[0].webAccount;
this.accessToken = wtrr.responseData[0].token;
this.setState(this.accessToken ? ProviderState.SignedIn : ProviderState.SignedOut);
break;
case webCore.WebTokenRequestStatus.userCancel:
case webCore.WebTokenRequestStatus.accountSwitch:
case webCore.WebTokenRequestStatus.userInteractionRequired:
case webCore.WebTokenRequestStatus.accountProviderNotAvailable:
case webCore.WebTokenRequestStatus.providerError:
break;
}
}
});
}
/**
* console logs the redirect url for auth
*
* @memberof WamProvider
*/
printRedirectUriToConsole() {
if (WamProvider.isAvailable) {
const web = window.Windows.Security.Authentication.Web;
const redirectUri = `ms-appx-web://Microsoft.AAD.BrokerPlugIn/${web.WebAuthenticationBroker.getCurrentApplicationCallbackUri()
.host.toUpperCase()}`;
}
}
/**
* returns access token Promise
*
* @param {AuthenticationProviderOptions} options
* @returns {Promise<string>}
* @memberof WamProvider
*/
getAccessToken(options) {
if (this.isLoggedIn) {
return Promise.resolve(this.accessToken);
}
else {
throw new Error('Not logged in');
}
}
}
//# sourceMappingURL=WamProvider.js.map