1 |
|
2 |
|
3 |
|
4 |
|
5 | var util = require('util');
|
6 | var OAuth2Strategy = require('passport-oauth2');
|
7 | var InternalOAuthError = require('passport-oauth2').InternalOAuthError;
|
8 | var querystring = require('query-string');
|
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 | function MicrosoftStrategy(options, verify) {
|
48 | options = options || {};
|
49 | options.authorizationURL = options.authorizationURL || 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize';
|
50 | options.tokenURL = options.tokenURL || 'https://login.microsoftonline.com/common/oauth2/v2.0/token';
|
51 | options.scopeSeparator = options.scopeSeparator || ' ';
|
52 | options.customHeaders = options.customHeaders || {};
|
53 |
|
54 | OAuth2Strategy.call(this, options, verify);
|
55 | this.name = 'microsoft';
|
56 |
|
57 | this._userProfileURL =
|
58 | options.userProfileURL || 'https://graph.microsoft.com/v1.0/me/';
|
59 |
|
60 | this._oauth2.getOAuthAccessToken = function (code, params, callback) {
|
61 | params = params || {};
|
62 | var codeParam =
|
63 | params.grant_type === 'refresh_token' ? 'refresh_token' : 'code';
|
64 | params[ codeParam ] = code;
|
65 | params[ 'client_id' ] = this._clientId;
|
66 | params[ 'client_secret' ] = this._clientSecret;
|
67 |
|
68 | var post_data = querystring.stringify(params);
|
69 | var post_headers = {
|
70 | 'Content-Type': 'application/x-www-form-urlencoded'
|
71 | };
|
72 |
|
73 | this._request(
|
74 | 'POST',
|
75 | this._getAccessTokenUrl(),
|
76 | post_headers,
|
77 | post_data,
|
78 | null,
|
79 | function (error, data, response) {
|
80 | if ( error ) callback(error);
|
81 | else {
|
82 | var results = JSON.parse(data);
|
83 | console.log('results', results);
|
84 | var access_token = results.access_token;
|
85 | var refresh_token = results.refresh_token;
|
86 | var expires_in = results.expires_in;
|
87 | delete results.refresh_token;
|
88 | callback(null, access_token, refresh_token, expires_in, results);
|
89 | }
|
90 | }
|
91 | );
|
92 | };
|
93 | }
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | util.inherits(MicrosoftStrategy, OAuth2Strategy);
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | MicrosoftStrategy.prototype.userProfile = function (accessToken, done) {
|
116 | this._oauth2.useAuthorizationHeaderforGET(true);
|
117 | this._oauth2.get(
|
118 | this._userProfileURL,
|
119 | accessToken,
|
120 | function (err, body, res) {
|
121 |
|
122 | if ( err ) {
|
123 | return done(new InternalOAuthError('failed to fetch user profile', err));
|
124 | }
|
125 | try {
|
126 | var json = JSON.parse(body);
|
127 |
|
128 | var profile = {
|
129 | provider: 'microsoft',
|
130 | name: {}
|
131 | };
|
132 | profile.id = json.id;
|
133 | profile.displayName = json.displayName;
|
134 | profile.name.familyName = json.surname;
|
135 | profile.name.givenName = json.givenName;
|
136 | profile.emails = [ { type: 'work', value: json.mail } ];
|
137 |
|
138 | profile._raw = body;
|
139 | profile._json = json;
|
140 |
|
141 | done(null, profile);
|
142 | } catch (e) {
|
143 | done(e);
|
144 | }
|
145 | }
|
146 | );
|
147 | };
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 | module.exports = MicrosoftStrategy;
|