-
Notifications
You must be signed in to change notification settings - Fork 1
/
azure_ad_server.js
executable file
·67 lines (56 loc) · 1.63 KB
/
azure_ad_server.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
AzureAd.allowlistFields = [
'id',
'userPrincipalName',
'mail',
'displayName',
'surname',
'givenName',
];
const hasOwn = Object.prototype.hasOwnProperty;
AzureAd.retrieveCredential = (credentialToken, credentialSecret) =>
OAuth.retrieveCredential(credentialToken, credentialSecret);
const getTokensFromCode = ({ code, tenantId, rootUrlFromRequest }) =>
AzureAd.http.getAccessTokensBase(
{
grant_type: 'authorization_code',
code,
},
tenantId,
rootUrlFromRequest
);
OAuth.registerService('azureAd', 2, null, requestData => {
const { tenantId } = requestData;
const tokens = getTokensFromCode(requestData);
const graphUser = AzureAd.resources.graph.getUser(tokens.accessToken);
const serviceData = {
accessToken: tokens.accessToken,
expiresAt: +new Date() + 1000 * tokens.expiresIn,
};
const fields = {};
AzureAd.allowlistFields.forEach(allowlistedField => {
if (hasOwn.call(graphUser, allowlistedField)) {
fields[allowlistedField] = graphUser[allowlistedField];
}
});
Object.assign(serviceData, fields);
// only set the token in serviceData if it's there. this ensures
// that we don't lose old ones (since we only get this on the first
// log in attempt)
if (tokens.refreshToken) serviceData.refreshToken = tokens.refreshToken;
const emailAddress = graphUser.mail || graphUser.userPrincipalName;
const options = {
tenantId,
profile: {
name: graphUser.displayName,
},
};
if (emailAddress) {
options.emails = [
{
address: emailAddress,
verified: true,
},
];
}
return { serviceData, options };
});