-
Notifications
You must be signed in to change notification settings - Fork 0
/
authHelper.js
55 lines (49 loc) · 1.5 KB
/
authHelper.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
var credentials = {
client: {
id: 'YOUR APP ID HERE',
secret: 'YOUR APP PASSWORD HERE'
},
auth: {
tokenHost: 'https://login.microsoftonline.com',
authorizePath: 'common/oauth2/v2.0/authorize',
tokenPath: 'common/oauth2/v2.0/token'
}
};
var oauth2 = require('simple-oauth2').create(credentials);
var redirectUri = 'http://localhost:8000/authorize';
// The scopes the app requires
var scopes = [ 'openid',
'offline_access',
'https://outlook.office.com/mail.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;