-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathauth.js
More file actions
60 lines (51 loc) · 1.74 KB
/
auth.js
File metadata and controls
60 lines (51 loc) · 1.74 KB
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
// import { CommonClientCredentialRequest } from "@azure/msal-common";
const msalcommon = require('@azure/msal-common');
const msal = require('@azure/msal-node');
/**
* Configuration object to be passed to MSAL instance on creation.
* For a full list of MSAL Node configuration parameters, visit:
* https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-node/docs/configuration.md
*/
// const msalConfig = {
// auth: {
// clientId: process.env.CLIENT_ID,
// authority: process.env.AAD_ENDPOINT + process.env.TENANT_ID,
// clientSecret: process.env.CLIENT_SECRET,
// }
// };
const msalConfig = {
auth: {
clientId: $client_id,
authority: "https://login.microsoftonline.com/" + $tenant_id,
clientSecret: $client_secret,
}
};
/**
* 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'. For more, visit:
* https://docs.microsoft.com/azure/active-directory/develop/v2-oauth2-client-creds-grant-flow
*/
const tokenRequest = {
scopes: [$client_id + "/.default"],
skipCache: true
};
const apiConfig = {
uri: $client_id + "/v1.0/users",
};
/**
* Initialize a confidential client application. For more info, visit:
* https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-node/docs/initialize-confidential-client-application.md
*/
const cca = new msal.ConfidentialClientApplication(msalConfig);
/**
* Acquires token with client credentials.
* @param {object} tokenRequest
*/
async function getToken(tokenRequest) {
return await cca.acquireTokenByClientCredential(tokenRequest);
}
module.exports = {
apiConfig: apiConfig,
tokenRequest: tokenRequest,
getToken: getToken
};