-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthHelper.js
More file actions
61 lines (52 loc) · 1.64 KB
/
authHelper.js
File metadata and controls
61 lines (52 loc) · 1.64 KB
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
/* Created by durka on 12/30/16. */
var credentials = {
client: {
id: 'xxxxxxxxxxxx',
secret: 'xxxxxxxxxxxx',
},
auth: {
tokenHost: 'xxxxxxxxxxxx',
authorizePath: 'xxxxxxxxxxxx',
tokenPath: 'xxxxxxxxxxxx'
}
};
var oauth2 = require('simple-oauth2').create(credentials);
var redirectUri = 'xxxxxxxxxxxx';
// The scopes the app requires
var scopes = [ 'openid',
'offline_access',
'https://outlook.office.com/mail.read',
'https://outlook.office.com/calendars.read',
'https://outlook.office.com/contacts.read' ];
function getAuthUrl() {
var returnVal = oauth2.authorizationCode.authorizeURL({
redirect_uri: redirectUri,
scope: scopes.join(' ')
});
console.log('Generated auth url: ' + returnVal);
return returnVal;
}
function getTokenFromCode(auth_code, callback, response) {
var token;
oauth2.authorizationCode.getToken({
code: auth_code,
redirect_uri: redirectUri,
scope: scopes.join(' ')
}, function (error, result) {
if (error) {
console.log('Access token error: ', error.message);
callback(response, error, null);
} else {
token = oauth2.accessToken.create(result);
console.log('Token created: ', token.token);
callback(response, null, token);
}
});
}
function refreshAccessToken(refreshToken, callback) {
var tokenObj = oauth2.accessToken.create({refresh_token: refreshToken});
tokenObj.refresh(callback);
}
exports.getAuthUrl = getAuthUrl;
exports.getTokenFromCode = getTokenFromCode;
exports.refreshAccessToken = refreshAccessToken;