-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauthHelper.js
59 lines (50 loc) · 1.81 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
58
59
var credentials = require('./credentials')
var oauth2 = require('simple-oauth2')(credentials.oauthcred);
// var redirectUri = "https://data.lohud.com/bots/trellobot/authorize";
// var redirectUri = "http://localhost:8080/authorize";
var redirectUri = "https://trellobot.lohudblogs.com/authorize";
// The Scopes the app requires
var scopes = [ "openid",
"https://outlook.office.com/mail.read",
"profile",
"offline_access" ];
var getAuthUrl = function() {
var returnVal = oauth2.authCode.authorizeURL({
redirect_uri: redirectUri,
scope: scopes.join(" ")
});
// console.log("Generated auth url: " + returnVal);
return returnVal;
};
var getTokenFromCode = function(auth_code, callback, res) {
var token;
// console.log(auth_code);
oauth2.authCode.getToken({
code: auth_code,
redirect_uri: redirectUri,
scope: scopes.join(" ")
}, function (error, result) {
if (error) {
console.log("Access token error: ", error.message);
callback(res, error, null);
} else {
token = oauth2.accessToken.create(result);
callback(res, null, token);
}
});
};
var getEmailFromIdToken = function(id_token) {
// JWT is in three parts, separated by a '.'
var token_parts = id_token.split('.');
// Token content is in the second part, in urlsafe base64
var encoded_token = new Buffer(token_parts[1].replace("-", "+").replace("_", "/"), 'base64');
var decoded_token = encoded_token.toString();
var jwt = JSON.parse(decoded_token);
// Email is in the preferred_username field
return jwt.preferred_username
};
module.exports = {
getAuthUrl: getAuthUrl,
getTokenFromCode: getTokenFromCode,
getEmailFromIdToken: getEmailFromIdToken
}