1 |
|
2 |
|
3 |
|
4 |
|
5 | var util = require('util');
|
6 | var OAuth2Strategy = require('passport-oauth2');
|
7 | var InternalOAuthError = require('passport-oauth2').InternalOAuthError;
|
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 | function MicrosoftStrategy(options, verify) {
|
47 | options = options || {};
|
48 | const tenant = options.tenant || 'common';
|
49 | options.authorizationURL = options.authorizationURL || `https://login.microsoftonline.com/${tenant}/oauth2/v2.0/authorize`;
|
50 | options.tokenURL = options.tokenURL || `https://login.microsoftonline.com/${tenant}/oauth2/v2.0/token`;
|
51 | options.scopeSeparator = options.scopeSeparator || ' ';
|
52 | options.customHeaders = options.customHeaders || {};
|
53 | options.apiEntryPoint = options.apiEntryPoint || 'https://graph.microsoft.com';
|
54 |
|
55 | OAuth2Strategy.call(this, options, verify);
|
56 | this.name = 'microsoft';
|
57 | this._graphApiVersion = options.graphApiVersion || 'v1.0';
|
58 | this._addUPNAsEmail = ('addUPNAsEmail' in options) ? options.addUPNAsEmail : false;
|
59 | this._apiEntryPoint = options.apiEntryPoint;
|
60 | }
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | util.inherits(MicrosoftStrategy, OAuth2Strategy);
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | MicrosoftStrategy.prototype.authorizationParams = function(options) {
|
83 | var params = {};
|
84 |
|
85 | ['locale', 'display', 'prompt', 'login_hint', 'domain_hint'].forEach(function(name) {
|
86 | if (options[name]) {
|
87 | params[name] = options[name];
|
88 | }
|
89 | });
|
90 |
|
91 | return params;
|
92 | };
|
93 |
|
94 | MicrosoftStrategy.prototype.userProfile = function (accessToken, done) {
|
95 | var strategy = this;
|
96 | strategy._oauth2.useAuthorizationHeaderforGET(true);
|
97 | strategy._oauth2.get(
|
98 | `${strategy._apiEntryPoint}/${strategy._graphApiVersion}/me/`,
|
99 | accessToken,
|
100 |
|
101 | function (err, body, res) {
|
102 |
|
103 | if (err) {
|
104 | return done(new InternalOAuthError('failed to fetch user profile', err));
|
105 | }
|
106 | try {
|
107 | var json = JSON.parse(body);
|
108 |
|
109 | var profile = {
|
110 | provider: 'microsoft',
|
111 | name: {}
|
112 | };
|
113 | profile.id = json.id;
|
114 | profile.displayName = json.displayName;
|
115 | profile.name.familyName = json.surname;
|
116 | profile.name.givenName = json.givenName;
|
117 | profile.userPrincipalName = json.userPrincipalName;
|
118 | profile.emails = [];
|
119 | var isNotEmpty = str => str && typeof str === 'string' && str.trim().length;
|
120 | if (isNotEmpty(json.mail)) {
|
121 | profile.emails.push({ type: 'work', value: json.mail });
|
122 | }
|
123 |
|
124 | if (strategy._addUPNAsEmail && isNotEmpty(json.userPrincipalName)) {
|
125 | profile.emails.push({ type: 'work', value: json.userPrincipalName});
|
126 | }
|
127 |
|
128 | profile._raw = body;
|
129 | profile._json = json;
|
130 |
|
131 | done(null, profile);
|
132 | }
|
133 | catch (e) {
|
134 | done(e);
|
135 | }
|
136 | }
|
137 | );
|
138 | };
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 | module.exports = MicrosoftStrategy; |
\ | No newline at end of file |