-
Notifications
You must be signed in to change notification settings - Fork 0
/
azure_ad_server.js
61 lines (50 loc) · 1.53 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
AzureAd.whitelistedFields = [
'id',
'userPrincipalName',
'mail',
'displayName',
'surname',
'givenName',
];
OAuth.registerService('azureAd', 2, null, function (query) {
var tokens = getTokensFromCode(redirectUrlFromQuery(query), query.code);
var graphUser = AzureAd.resources.graph.getUser(tokens.accessToken);
var serviceData = {
...tokens,
expiresAt: +new Date() + 1000 * parseInt(tokens.expiresIn, 10),
};
var fields = _.pick(graphUser, AzureAd.whitelistedFields);
_.extend(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;
var emailAddress = graphUser.mail || graphUser.userPrincipalName;
var options = {
profile: {
name: graphUser.displayName,
},
};
if (!!emailAddress) {
options.emails = [
{
address: emailAddress,
verified: true,
},
];
}
return { serviceData: serviceData, options: options };
});
function redirectUrlFromQuery(query) {
const state = OAuth._stateFromQuery(query) || {};
return decodeURIComponent(state.redirectUrl);
}
function getTokensFromCode(redirectUrl, code) {
return AzureAd.http.getAccessTokensBase(redirectUrl, {
grant_type: 'authorization_code',
code: code,
});
}
AzureAd.retrieveCredential = function (credentialToken, credentialSecret) {
return OAuth.retrieveCredential(credentialToken, credentialSecret);
};