/
login.js
349 lines (330 loc) · 14.8 KB
/
login.js
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
var adal= require('adal-node');
var async = require('async');
var util = require('util');
var azureConstants = require('./constants');
var ApplicationTokenCredentials = require('./credentials/applicationTokenCredentials');
var DeviceTokenCredentials = require('./credentials/deviceTokenCredentials');
var UserTokenCredentials = require('./credentials/userTokenCredentials');
var AzureEnvironment = require('./azureEnvironment');
var SubscriptionClient = require('azure-arm-resource').SubscriptionClient;
// It will create a DeviceTokenCredentials object by default
function _createCredentials(parameters) {
var options = {};
options.environment = this.environment;
options.domain = this.domain;
options.clientId = this.clientId;
options.tokenCache = this.tokenCache;
options.username = this.username;
options.authorizationScheme = this.authorizationScheme;
options.tokenAudience = this.tokenAudience;
if (parameters) {
if (parameters.domain) {
options.domain = parameters.domain;
}
if (parameters.environment) {
options.environment = parameters.environment;
}
if (parameters.userId) {
options.username = parameters.userId;
}
if (parameters.tokenCache) {
options.tokenCache = parameters.tokenCache;
}
if (parameters.tokenAudience) {
options.tokenAudience = parameters.tokenAudience;
}
}
var credentials;
if (UserTokenCredentials.prototype.isPrototypeOf(this)) {
credentials = new UserTokenCredentials(options.clientId, options.domain, options.username, this.password, options);
} else if (ApplicationTokenCredentials.prototype.isPrototypeOf(this)) {
credentials = new ApplicationTokenCredentials(options.clientId, options.domain, this.secret, options);
} else {
credentials = new DeviceTokenCredentials(options);
}
return credentials;
}
function buildTenantList(credentials, callback) {
var tenants = [];
if (credentials.domain && credentials.domain !== azureConstants.AAD_COMMON_TENANT) {
return callback(null, [credentials.domain]);
}
var client = new SubscriptionClient(credentials, credentials.environment.resourceManagerEndpointUrl);
client.tenants.list(function (err, result) {
async.eachSeries(result, function (tenantInfo, cb) {
tenants.push(tenantInfo.tenantId);
cb(err);
}, function (err) {
callback(err, tenants);
});
});
}
function getSubscriptionsFromTenants(tenantList, callback) {
var self = this;
var subscriptions = [];
var userType = 'user';
var username = self.username;
if (ApplicationTokenCredentials.prototype.isPrototypeOf(self)) {
userType = 'servicePrincipal';
username = self.clientId;
}
async.eachSeries(tenantList, function (tenant, cb) {
var creds = _createCredentials.call(self, { domain: tenant });
var client = new SubscriptionClient(creds, creds.environment.resourceManagerEndpointUrl);
client.subscriptions.list(function (err, result) {
if (!err) {
if (result && result.length > 0) {
subscriptions = subscriptions.concat(result.map(function (s) {
s.tenantId = tenant;
s.user = { name: username, type: userType };
s.environmentName = creds.environment.name;
s.name = s.displayName;
s.id = s.subscriptionId;
delete s.displayName;
delete s.subscriptionId;
delete s.subscriptionPolicies;
return s;
}));
}
}
return cb(err);
});
}, function (err) {
callback(err, subscriptions);
});
}
function _turnOnLogging() {
var log = adal.Logging;
log.setLoggingOptions(
{
level : log.LOGGING_LEVEL.VERBOSE,
log : function (level, message, error) {
console.info(message);
if (error) {
console.error(error);
}
}
});
}
if (process.env['AZURE_ADAL_LOGGING_ENABLED']) {
_turnOnLogging();
}
function _crossCheckUserNameWithToken(usernameFromMethodCall, userIdFromToken) {
//to maintain the casing consistency between 'azureprofile.json' and token cache. (RD 1996587)
//use the 'userId' here, which should be the same with "username" except the casing.
if (usernameFromMethodCall.toLowerCase() === userIdFromToken.toLowerCase()) {
return userIdFromToken;
} else {
throw new Error(util.format('The userId of \'%s\' in access token doesn\'t match the username from method call \'%s\'',
userIdFromToken, usernameFromMethodCall));
}
}
/**
* Provides a url and code that needs to be copy and pasted in a browser and authenticated over there. If successful, the user will get a
* DeviceTokenCredentials object and the list of subscriptions associated with that userId across all the applicable tenants.
*
* @param {object} [options] Object representing optional parameters.
*
* @param {string} [options.clientId] The active directory application client id.
* See {@link https://azure.microsoft.com/en-us/documentation/articles/active-directory-devquickstarts-dotnet/ Active Directory Quickstart for .Net}
* for an example.
*
* @param {string} [options.tokenAudience] The audience for which the token is requested. Valid value is 'graph'.If tokenAudience is provided
* then domain should also be provided its value should not be the default 'common' tenant. It must be a string (preferrably in a guid format).
*
* @param {string} [options.domain] The domain or tenant id containing this application. Default value is 'common'.
*
* @param {AzureEnvironment} [options.environment] The azure environment to authenticate with. Default environment is "Public Azure".
*
* @param {object} [options.tokenCache] The token cache. Default value is the MemoryCache object from adal.
*
* @param {object} [options.language] The language code specifying how the message should be localized to. Default value 'en-us'.
*
* @param {function} callback
*
* @returns {function} callback(err, credentials)
*
* {Error} [err] - The Error object if an error occurred, null otherwise.
* {DeviceTokenCredentials} [credentials] - The DeviceTokenCredentials object
* {Array} [subscriptions] - List of associated subscriptions across all the applicable tenants.
*/
exports.interactive = function interactive(options, callback) {
if(!callback && typeof options === 'function') {
callback = options;
options = {};
}
if (!options.environment) {
options.environment = AzureEnvironment.Azure;
}
if (!options.domain) {
options.domain = azureConstants.AAD_COMMON_TENANT;
}
if (!options.clientId) {
options.clientId = azureConstants.DEFAULT_ADAL_CLIENT_ID;
}
if (!options.tokenCache) {
options.tokenCache = new adal.MemoryCache();
}
if (!options.language) {
options.language = azureConstants.DEFAULT_LANGUAGE;
}
this.tokenAudience = options.tokenAudience;
this.environment = options.environment;
this.domain = options.domain;
this.clientId = options.clientId;
this.tokenCache = options.tokenCache;
this.language = options.language;
var authorityUrl = this.environment.activeDirectoryEndpointUrl + this.domain;
this.context = new adal.AuthenticationContext(authorityUrl, this.environment.validateAuthority, this.tokenCache);
var self = this;
var tenantList = [];
async.waterfall([
//acquire usercode
function (callback) {
self.context.acquireUserCode(self.environment.activeDirectoryResourceId, self.clientId, self.language, function (err, userCodeResponse) {
if (err) return callback(err);
console.log(userCodeResponse.message);
return callback(null, userCodeResponse);
});
},
//acquire token with device code and set the username to userId received from tokenResponse.
function (userCodeResponse, callback) {
self.context.acquireTokenWithDeviceCode(self.environment.activeDirectoryResourceId, self.clientId, userCodeResponse, function (err, tokenResponse) {
if (err) return callback(err);
self.username = tokenResponse.userId;
self.authorizationScheme = tokenResponse.tokenType;
return callback(null);
});
},
//get the list of tenants
function (callback) {
var credentials = _createCredentials.call(self);
buildTenantList(credentials, callback);
},
//build the token cache by getting tokens for all the tenants. We will acquire token from adal only when a request is sent. This is good as we also need
//to build the list of subscriptions across all tenants. So let's build both at the same time :).
function (tenants, callback) {
tenantList = tenants;
if (self.tokenAudience && self.tokenAudience.toLowerCase() === 'graph') {
// we dont need to get the subscriptionList if the tokenAudience is graph as graph clients are tenant based.
return callback(null, []);
} else {
return getSubscriptionsFromTenants.call(self, tenants, callback);
}
}
], function(err, subscriptions) {
if (err) return callback(err);
return callback(null, _createCredentials.call(self), subscriptions);
});
};
/**
* Provides a UserTokenCredentials object and the list of subscriptions associated with that userId across all the applicable tenants.
* This method is applicable only for organizational ids that are not 2FA enabled otherwise please use interactive login.
*
* @param {string} username The user name for the Organization Id account.
* @param {string} password The password for the Organization Id account.
* @param {object} [options] Object representing optional parameters.
* @param {string} [options.clientId] The active directory application client id.
* See {@link https://azure.microsoft.com/en-us/documentation/articles/active-directory-devquickstarts-dotnet/ Active Directory Quickstart for .Net}
* for an example.
* @param {string} [options.tokenAudience] The audience for which the token is requested. Valid value is 'graph'. If tokenAudience is provided
* then domain should also be provided and its value should not be the default 'common' tenant. It must be a string (preferrably in a guid format).
* @param {string} [options.domain] The domain or tenant id containing this application. Default value 'common'.
* @param {AzureEnvironment} [options.environment] The azure environment to authenticate with.
* @param {string} [options.authorizationScheme] The authorization scheme. Default value is 'bearer'.
* @param {object} [options.tokenCache] The token cache. Default value is the MemoryCache object from adal.
* @param {function} callback
*
* @returns {function} callback(err, credentials)
*
* {Error} [err] - The Error object if an error occurred, null otherwise.
* {UserTokenCredentials} [credentials] - The UserTokenCredentials object
* {Array} [subscriptions] - List of associated subscriptions across all the applicable tenants.
*/
exports.withUsernamePassword = function withUsernamePassword(username, password, options, callback) {
if(!callback && typeof options === 'function') {
callback = options;
options = {};
}
if (!options.domain) {
options.domain = azureConstants.AAD_COMMON_TENANT;
}
if (!options.clientId) {
options.clientId = azureConstants.DEFAULT_ADAL_CLIENT_ID;
}
var creds;
var tenantList = [];
try {
creds = new UserTokenCredentials(options.clientId, options.domain, username, password, options);
} catch (err) {
return callback(err);
}
creds.getToken(function (err, result) {
if (err) return callback(err);
creds.username = _crossCheckUserNameWithToken(username, result.userId);
async.waterfall([
function (callback) {
buildTenantList(creds, callback);
},
function (tenants, callback) {
tenantList = tenants;
if (options.tokenAudience && options.tokenAudience.toLowerCase() === 'graph') {
// we dont need to get the subscriptionList if the tokenAudience is graph as graph clients are tenant based.
return callback(null, []);
} else {
return getSubscriptionsFromTenants.call(options, tenants, callback);
}
},
], function (err, subscriptions) {
return callback(null, creds, subscriptions);
});
});
};
/**
* Provides an ApplicationTokenCredentials object and the list of subscriptions associated with that servicePrinicpalId/clientId across all the applicable tenants.
*
* @param {string} clientId The active directory application client id also known as the SPN (ServicePrincipal Name).
* See {@link https://azure.microsoft.com/en-us/documentation/articles/active-directory-devquickstarts-dotnet/ Active Directory Quickstart for .Net}
* for an example.
* @param {string} secret The application secret for the service principal.
* @param {string} domain The domain or tenant id containing this application.
* @param {object} [options] Object representing optional parameters.
* @param {string} [options.tokenAudience] The audience for which the token is requested. Valid value is 'graph'.
* @param {AzureEnvironment} [options.environment] The azure environment to authenticate with.
* @param {string} [options.authorizationScheme] The authorization scheme. Default value is 'bearer'.
* @param {object} [options.tokenCache] The token cache. Default value is the MemoryCache object from adal.
* @param {function} callback
*
* @returns {function} callback(err, credentials)
*
* {Error} [err] - The Error object if an error occurred, null otherwise.
* {UserTokenCredentials} [credentials] - The UserTokenCredentials object
* {Array} [subscriptions] - List of associated subscriptions across all the applicable tenants.
*/
exports.withServicePrincipalSecret = function withServicePrincipalSecret(clientId, secret, domain, options, callback) {
if(!callback && typeof options === 'function') {
callback = options;
options = {};
}
var creds;
try {
creds = new ApplicationTokenCredentials(clientId, domain, secret, options);
} catch (err) {
return callback(err);
}
creds.getToken(function (err) {
if (err) return callback(err);
if (options.tokenAudience && options.tokenAudience.toLowerCase() === 'graph') {
// we dont need to get the subscriptionList if the tokenAudience is graph as graph clients are tenant based.
return callback(null, creds, []);
} else {
getSubscriptionsFromTenants.call(creds, [domain], function (err, subscriptions) {
if (err) return callback(err);
return callback(null, creds, subscriptions);
});
}
});
};
exports = module.exports;