-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
index.js
83 lines (72 loc) · 3.06 KB
/
index.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
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
var msal = require('@azure/msal-node');
/**
* Command line arguments can be used to configure:
* - The port the application runs on
* - The cache file location
* - The authentication scenario/configuration file name
*/
const argv = require("../cliArgs");
const cacheLocation = argv.c || "./data/cache.json";
const cachePlugin = require('../cachePlugin')(cacheLocation);
/**
* The scenario string is the name of a .json file which contains the MSAL client configuration
* For an example of what a configuration file should look like, check out the customConfig.json file in the
* /config directory.
*
* You can create your own configuration file and replace the path inside the "config" require statement below
* with the path to your custom configuraiton.
*/
const runtimeOptions = argv.ro || null;
const config = require(`./config/AAD.json`);
function getClientCredentialsToken(cca, clientCredentialRequestScopes, ro) {
// With client credentials flows permissions need to be granted in the portal by a tenant administrator.
// The scope is always in the format "<resource>/.default"
const clientCredentialRequest = {
scopes: clientCredentialRequestScopes,
azureRegion: ro ? ro.region : null, // (optional) specify the region you will deploy your application to here (e.g. "westus2")
skipCache: true, // (optional) this skips the cache and forces MSAL to get a new token from Azure AD
};
return cca
.acquireTokenByClientCredential(clientCredentialRequest)
.then((response) => {
// Uncomment to see the successful response logged
// console.log("Response: ", response);
}).catch((error) => {
// Uncomment to see the errors logges
// console.log(JSON.stringify(error));
});
}
/**
* The code below checks if the script is being executed manually or in automation.
* If the script was executed manually, it will initialize a ConfidentialClientApplication object
* and execute the sample client credentials application.
*/
if(argv.$0 === "index.js") {
const loggerOptions = {
loggerCallback(loglevel, message, containsPii) {
console.log(message);
},
piiLoggingEnabled: false,
logLevel: msal.LogLevel.Verbose,
}
// Build MSAL ClientApplication Configuration object
const clientConfig = {
auth: config.authOptions,
cache: {
cachePlugin
},
// Uncomment or comment the code below to enable or disable the MSAL logger respectively
// system: {
// loggerOptions,
// }
};
// Create msal application object
const confidentialClientApplication = new msal.ConfidentialClientApplication(clientConfig);
// Execute sample application with the configured MSAL PublicClientApplication
return getClientCredentialsToken(confidentialClientApplication, runtimeOptions);
}
module.exports = getClientCredentialsToken;