-
Notifications
You must be signed in to change notification settings - Fork 35
/
MSGraphTokenHelper.ts
117 lines (99 loc) · 5.73 KB
/
MSGraphTokenHelper.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import Consts from "../Consts";
import * as adal from 'adal-node';
import { AuthenticationContext } from 'adal-node';
import * as vscode from 'vscode';
export default class MSGraphTokenHelper {
public static acquireToken(tenantId: string): Thenable<adal.TokenResponse> {
let yourTenantReplaced: string = "";
// Replace the samples yourtenant with the default tenant name
if (tenantId.toLowerCase().indexOf(Consts.SamplesYourTenant) >= 0 && MSGraphTokenHelper.getMSGraphDefaultTenantID() != "") {
tenantId = tenantId.toLowerCase().replace(Consts.SamplesYourTenant, MSGraphTokenHelper.getMSGraphDefaultTenantID() + ".onmicrosoft.com")
yourTenantReplaced = "The policy '" + Consts.SamplesYourTenant + "' tenant name has been replaced with " + MSGraphTokenHelper.getMSGraphDefaultTenantID() + ".onmicrosoft.com.";
}
let clientID = MSGraphTokenHelper.getMSGraphClientID();
let authURL = Consts.ADALauthURLPrefix + tenantId;
let authcontext = new adal.AuthenticationContext(authURL);
return new Promise((resolve, reject) => {
authcontext.acquireToken(Consts.ADALresource, "", clientID, function (err, tokenResponse) {
//reauthenticate if the access token is invalid
if (err) {
MSGraphTokenHelper.deviceCodeLogin(tenantId, yourTenantReplaced, clientID)
.then(
tokenResponse => resolve(tokenResponse),
err => reject(err));
}
else {
resolve(tokenResponse as adal.TokenResponse);
}
});
})
}
static deviceCodeLogin(tenantId: string, yourTenantReplaced: string, ClientId: string): Thenable<adal.TokenResponse> {
var authorityUrl = Consts.ADALauthURLPrefix + tenantId;
var resource = 'https://graph.microsoft.com';
var context = new AuthenticationContext(authorityUrl);
return new Promise((resolve, reject) => context.acquireUserCode(resource, ClientId, 'es-mx',
function (err, response) {
if (err) {
if (yourTenantReplaced) {
vscode.window.showWarningMessage(yourTenantReplaced)
}
console.log('well that didn\'t work: ' + err.message);
if (err.message == "Error login in - The clientId parameter is required.") {
vscode.window.showErrorMessage("The Graph API ClientId has not been set in Settings.");
}
else if (err.message.indexOf("AADSTS700016") >= 0 && tenantId.indexOf(Consts.SamplesYourTenant) >= 0)
vscode.window.showErrorMessage("Your policy uses '" + Consts.SamplesYourTenant + "' tenant name. Replace the yourtenant with your tenant name. Or configure the 'aadb2c.graph.replaceSamplesYourTenantWith' with your tenant name.");
else if (err.message.indexOf("AADSTS700016") >= 0)
vscode.window.showErrorMessage("Application with identifier '" + ClientId + " ' was not found in the directory '" + tenantId + "'. You may have sent your authentication request to the wrong tenant. Or the application has not been installed by the administrator of the tenant or consented to by any user in the tenant.");
else if (err.message.indexOf("AADSTS90002") >= 0) +
vscode.window.showErrorMessage("Tenant '" + tenantId + " ' not found.");
else {
vscode.window.showErrorMessage(err.message);
}
reject(err);
} else {
console.log(response.userCode);
var usercode = response.userCode;
var ncp = require("copy-paste");
ncp.copy(usercode, function () {
// complete...
})
vscode.window.showErrorMessage("Please login to '" + tenantId + " ' tenant with the following code (" + response.userCode + ")", "Login").then(selection => {
if (selection == "Login") {
vscode.commands.executeCommand('vscode.open', vscode.Uri.parse("https://www.microsoft.com/devicelogin"));
}
});
console.log('calling acquire token with device code');
context.acquireTokenWithDeviceCode(resource, ClientId, response, function (Error, tokenResponse) {
if (err) {
console.log('error happens when acquiring token with device code');
console.log(err);
vscode.window.showErrorMessage('An error happens when acquiring token with device code');
reject(err);
}
else {
resolve(tokenResponse as adal.TokenResponse);
}
});
}
}));
}
static getMSGraphClientID() {
var config = vscode.workspace.getConfiguration('aadb2c.graph');
var RtnVal = config.has("clientid");
if (!RtnVal) {
vscode.window.showErrorMessage("The ClientId setting is not set");
return "";
}
return "" + config.get("clientid");
}
static getMSGraphDefaultTenantID() {
var config = vscode.workspace.getConfiguration('aadb2c.graph');
var RtnVal = config.has("replaceSamplesYourTenantWith");
if (!RtnVal) {
return "";
}
return "" + config.get("replaceSamplesYourTenantWith");
}
}