forked from classcraft/azure-active-directory
-
Notifications
You must be signed in to change notification settings - Fork 1
/
azure_ad_server.js
executable file
·52 lines (39 loc) · 1.46 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
AzureAd.whitelistedFields = ['id', 'userPrincipalName', 'mail', 'displayName', 'surname', 'givenName'];
OAuth.registerService('azureAd', 2, null, function (query) {
var tokens = getTokensFromCode(AzureAd.resources.microsoftGraph.resourceUri, query.code);
var microsoftGraphUser = AzureAd.resources.microsoftGraph.getUser(tokens.accessToken);
var emailAddress = microsoftGraphUser.mail || microsoftGraphUser.userPrincipalName;
var serviceData = {
accessToken: tokens.accessToken,
expiresAt: (+new Date) + (1000 * tokens.expiresIn),
email: emailAddress
};
var fields = _.pick(microsoftGraphUser, 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 options = {
profile: {
name: microsoftGraphUser.displayName
}
};
if (!!emailAddress) {
options.emails = [{
address: emailAddress,
verified: true
}];
}
return { serviceData: serviceData, options: options };
});
function getTokensFromCode(resource, code) {
return AzureAd.http.getAccessTokensBase(resource, {
grant_type: 'authorization_code',
code: code
});
};
AzureAd.retrieveCredential = function (credentialToken, credentialSecret) {
return OAuth.retrieveCredential(credentialToken, credentialSecret);
};