-
Notifications
You must be signed in to change notification settings - Fork 0
/
authHelper.js
57 lines (50 loc) · 1.6 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
56
57
var credentials = {
client: {
id: process.env.APP_ID,
secret: process.env.APP_SECRET,
},
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',
'User.Read',
'Sites.Read.All']; // /me/insights/used
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;