-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patho365.js
177 lines (159 loc) · 5.01 KB
/
o365.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
var OAuth = require('oauth'),
https = require('https');
var endpoint = {
"authority": "https://login.microsoftonline.com/common",
"authorize_endpoint": "/oauth2/v2.0/authorize",
"token_endpoint": "/oauth2/v2.0/token",
"scope": "user.read directory.read.all offline_access",
"state": "o3651234abcd",
"redirectUriPath": "/oauth2callback",
"graphUri": "graph.microsoft.com"
};
/**
* Gets a token for a given resource.
* @param {string} code An authorization code returned from a client.
* @param {AcquireTokenCallback} callback The callback function.
*/
function getTokenFromCode( code, state, reqUrl, config, callback ) {
var redirectUri = reqUrl + endpoint.redirectUriPath;
var OAuth2 = OAuth.OAuth2;
var oauth2 = new OAuth2(
config.office365.client_id,
config.office365.client_secret,
endpoint.authority,
endpoint.authorize_endpoint,
endpoint.token_endpoint
);
oauth2.getOAuthAccessToken(
code,
{
grant_type: 'authorization_code',
redirect_uri: redirectUri,
response_mode: 'form_post',
nonce: _guid(),
state: state
},
function (e, accessToken, refreshToken) {
callback(e, accessToken, refreshToken);
}
);
}
/**
* Gets a new access token via a previously issued refresh token.
* @param {string} refreshToken A refresh token returned in a token response
* from a previous result of an authentication flow.
* @param {AcquireTokenCallback} callback The callback function.
*/
function getTokenFromRefreshToken( refreshToken, reqUrl, config, callback ) {
var redirectUri = reqUrl + endpoint.redirectUriPath;
var OAuth2 = OAuth.OAuth2;
var oauth2 = new OAuth2(
config.office365.client_id,
config.office365.client_secret,
endpoint.authority,
endpoint.authorize_endpoint,
endpoint.token_endpoint
);
oauth2.getOAuthAccessToken(
refreshToken,
{
grant_type: 'refresh_token',
redirect_uri: redirectUri,
response_mode: 'form_post',
nonce: _guid(),
state: endpoint.state
},
function ( e, accessToken ) {
callback(e, accessToken);
}
);
}
/**
* Gets office 365 login url
*/
function getAuthUrl( reqUrl, config ) {
var redirectUri = reqUrl + endpoint.redirectUriPath;
return endpoint.authority + endpoint.authorize_endpoint +
"?response_type=code" +
"&client_id=" + config.office365.client_id +
"&redirect_uri=" + redirectUri +
"&scope=" + endpoint.scope +
"&response_mode=query" +
"&nonce=" + _guid() +
"&state=" + endpoint.state;
}
/**
* Gets user Groups from user data in Office 365.
* @param {string} accessToken
* @param {Callback} callback The callback function.
*/
function getUserGroups( accessToken, callback ) {
_request( {
path: "/v1.0/me/memberOf",
headers: { "Authorization": "Bearer " + accessToken },
}, function( err, response ) {
if ( err ) {
callback(err, null);
} else {
callback( null, response );
}
} );
}
/**
* Gets userId from user data in Office 365.
* @param {string} accessToken
* @param {Callback} callback The callback function.
*/
function getUser( accessToken, callback ) {
_request( {
path: "/v1.0/me",
headers: { "Authorization": "Bearer " + accessToken },
}, function( err, response ) {
if ( err ) {
callback(err, null);
} else {
callback( null, response );
}
} );
}
function _request( options, callback ) {
options.host = options.host || endpoint.graphUri;
options.agent = options.agent || false;
options.method = options.method || "GET";
var req = https.request (options, function( response ) {
var str = ''
response.on( 'data', function (chunk) {
str += chunk;
});
response.on( 'end', function () {
callback( null, JSON.parse(str) );
} );
} );
req.on( 'error', function (err) {
callback( err, null );
} );
req.on( 'timeout', function () {
// Timeout happend. Server received request, but not handled it
// (i.e. doesn't send any response or it took to long).
// You don't know what happend.
// It will emit 'error' message as well (with ECONNRESET code).
req.abort();
callback( "timeout", null );
} );
req.end();
}
function _guid() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
}
// ------ LIB exports ------- //
exports.getUser = getUser;
exports.getUserGroups = getUserGroups;
exports.getAuthUrl = getAuthUrl;
exports.getTokenFromCode = getTokenFromCode;
exports.getTokenFromRefreshToken = getTokenFromRefreshToken;