-
Notifications
You must be signed in to change notification settings - Fork 19
/
app.js
395 lines (361 loc) · 13.1 KB
/
app.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
'use strict';
const envx = require("envx");
const restify = require('restify');
const builder = require('botbuilder');
const passport = require('passport');
const OIDCStrategy = require('passport-azure-ad').OIDCStrategy;
const expressSession = require('express-session');
const crypto = require('crypto');
const querystring = require('querystring');
const https = require('https');
const request = require('request');
//bot application identity
const MICROSOFT_APP_ID = envx("MICROSOFT_APP_ID");
const MICROSOFT_APP_PASSWORD = envx("MICROSOFT_APP_PASSWORD");
//oauth details
const AZUREAD_APP_ID = envx("AZUREAD_APP_ID");
const AZUREAD_APP_PASSWORD = envx("AZUREAD_APP_PASSWORD");
const AZUREAD_APP_REALM = envx("AZUREAD_APP_REALM");
const AUTHBOT_CALLBACKHOST = envx("AUTHBOT_CALLBACKHOST");
const AUTHBOT_STRATEGY = envx("AUTHBOT_STRATEGY");
//=========================================================
// Bot Setup
//=========================================================
// Setup Restify Server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3979, function () {
console.log('%s listening to %s', server.name, server.url);
});
// Create chat bot
console.log('started...')
console.log(MICROSOFT_APP_ID);
var connector = new builder.ChatConnector({
appId: MICROSOFT_APP_ID,
appPassword: MICROSOFT_APP_PASSWORD
});
var bot = new builder.UniversalBot(connector);
server.post('/api/messages', connector.listen());
server.get('/', restify.serveStatic({
'directory': __dirname,
'default': 'index.html'
}));
//=========================================================
// Auth Setup
//=========================================================
server.use(restify.queryParser());
server.use(restify.bodyParser());
server.use(expressSession({ secret: 'keyboard cat', resave: true, saveUninitialized: false }));
server.use(passport.initialize());
server.get('/login', function (req, res, next) {
passport.authenticate('azuread-openidconnect', { failureRedirect: '/login', customState: req.query.address, resourceURL: process.env.MICROSOFT_RESOURCE }, function (err, user, info) {
console.log('login');
if (err) {
console.log(err);
return next(err);
}
if (!user) {
return res.redirect('/login');
}
req.logIn(user, function (err) {
if (err) {
return next(err);
} else {
return res.send('Welcome ' + req.user.displayName);
}
});
})(req, res, next);
});
server.get('/api/OAuthCallback/',
passport.authenticate('azuread-openidconnect', { failureRedirect: '/login' }),
(req, res) => {
console.log('OAuthCallback');
console.log(req);
const address = JSON.parse(req.query.state);
const magicCode = crypto.randomBytes(4).toString('hex');
const messageData = { magicCode: magicCode, accessToken: req.user.accessToken, refreshToken: req.user.refreshToken, userId: address.user.id, name: req.user.displayName, email: req.user.preferred_username };
var continueMsg = new builder.Message().address(address).text(JSON.stringify(messageData));
console.log(continueMsg.toMessage());
bot.receive(continueMsg.toMessage());
res.send('Welcome ' + req.user.displayName + '! Please copy this number and paste it back to your chat so your authentication can complete: ' + magicCode);
});
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(id, done) {
done(null, id);
});
// Use the v2 endpoint (applications configured by apps.dev.microsoft.com)
// For passport-azure-ad v2.0.0, had to set realm = 'common' to ensure authbot works on azure app service
var realm = AZUREAD_APP_REALM;
let oidStrategyv2 = {
redirectUrl: AUTHBOT_CALLBACKHOST + '/api/OAuthCallback',
realm: realm,
clientID: AZUREAD_APP_ID,
clientSecret: AZUREAD_APP_PASSWORD,
identityMetadata: 'https://login.microsoftonline.com/' + realm + '/v2.0/.well-known/openid-configuration',
skipUserProfile: false,
validateIssuer: false,
//allowHttpForRedirectUrl: true,
responseType: 'code',
responseMode: 'query',
scope:['email', 'profile', 'offline_access', 'https://outlook.office.com/mail.read'],
passReqToCallback: true
};
// Use the v1 endpoint (applications configured by manage.windowsazure.com)
// This works against Azure AD
let oidStrategyv1 = {
redirectUrl: AUTHBOT_CALLBACKHOST +'/api/OAuthCallback',
realm: realm,
clientID: AZUREAD_APP_ID,
clientSecret: AZUREAD_APP_PASSWORD,
validateIssuer: false,
//allowHttpForRedirectUrl: true,
oidcIssuer: undefined,
identityMetadata: 'https://login.microsoftonline.com/' + realm + '/.well-known/openid-configuration',
skipUserProfile: true,
responseType: 'code',
responseMode: 'query',
passReqToCallback: true
};
let strategy = null;
if ( AUTHBOT_STRATEGY == 'oidStrategyv1') {
strategy = oidStrategyv1;
}
if ( AUTHBOT_STRATEGY == 'oidStrategyv2') {
strategy = oidStrategyv2;
}
passport.use(new OIDCStrategy(strategy,
(req, iss, sub, profile, accessToken, refreshToken, done) => {
if (!profile.displayName) {
return done(new Error("No oid found"), null);
}
// asynchronous verification, for effect...
process.nextTick(() => {
profile.accessToken = accessToken;
profile.refreshToken = refreshToken;
return done(null, profile);
});
}
));
//=========================================================
// Bots Dialogs
//=========================================================
function login(session) {
// Generate signin link
const address = session.message.address;
// TODO: Encrypt the address string
const link = AUTHBOT_CALLBACKHOST + '/login?address=' + querystring.escape(JSON.stringify(address));
var msg = new builder.Message(session)
.attachments([
new builder.SigninCard(session)
.text("Please click this link to sign in first.")
.button("signin", link)
]);
session.send(msg);
builder.Prompts.text(session, "You must first sign into your account.");
}
bot.dialog('signin', [
(session, results) => {
console.log('signin callback: ' + results);
session.endDialog();
}
]);
bot.dialog('/', [
(session, args, next) => {
if (!(session.userData.userName && session.userData.accessToken && session.userData.refreshToken)) {
session.send("Welcome! This bot retrieves the latest email for you after you login.");
session.beginDialog('signinPrompt');
} else {
next();
}
},
(session, results, next) => {
if (session.userData.userName && session.userData.accessToken && session.userData.refreshToken) {
// They're logged in
builder.Prompts.text(session, "Welcome " + session.userData.userName + "! You are currently logged in. To get the latest email, type 'email'. To quit, type 'quit'. To log out, type 'logout'. ");
} else {
session.endConversation("Goodbye.");
}
},
(session, results, next) => {
var resp = results.response;
if (resp === 'email') {
session.beginDialog('workPrompt');
} else if (resp === 'quit') {
session.endConversation("Goodbye.");
} else if (resp === 'logout') {
session.userData.loginData = null;
session.userData.userName = null;
session.userData.accessToken = null;
session.userData.refreshToken = null;
session.endConversation("You have logged out. Goodbye.");
} else {
next();
}
},
(session, results) => {
session.replaceDialog('/');
}
]);
bot.dialog('workPrompt', [
(session) => {
getUserLatestEmail(session.userData.accessToken,
function (requestError, result) {
if (result && result.value && result.value.length > 0) {
const responseMessage = 'Your latest email is: "' + result.value[0].Subject + '"';
session.send(responseMessage);
builder.Prompts.confirm(session, "Retrieve the latest email again?");
}else{
console.log('no user returned');
if(requestError){
console.log('requestError');
console.error(requestError);
// Get a new valid access token with refresh token
getAccessTokenWithRefreshToken(session.userData.refreshToken, (err, body, res) => {
if (err || body.error) {
console.log(err);
session.send("Error while getting a new access token. Please try logout and login again. Error: " + err);
session.endDialog();
}else{
session.userData.accessToken = body.accessToken;
getUserLatestEmail(session.userData.accessToken,
function (requestError, result) {
if (result && result.value && result.value.length > 0) {
const responseMessage = 'Your latest email is: "' + result.value[0].Subject + '"';
session.send(responseMessage);
builder.Prompts.confirm(session, "Retrieve the latest email again?");
}
}
);
}
});
}
}
}
);
},
(session, results) => {
var prompt = results.response;
if (prompt) {
session.replaceDialog('workPrompt');
} else {
session.endDialog();
}
}
]);
bot.dialog('signinPrompt', [
(session, args) => {
if (args && args.invalid) {
// Re-prompt the user to click the link
builder.Prompts.text(session, "please click the signin link.");
} else {
login(session);
}
},
(session, results) => {
//resuming
session.userData.loginData = JSON.parse(results.response);
if (session.userData.loginData && session.userData.loginData.magicCode && session.userData.loginData.accessToken) {
session.beginDialog('validateCode');
} else {
session.replaceDialog('signinPrompt', { invalid: true });
}
},
(session, results) => {
if (results.response) {
//code validated
session.userData.userName = session.userData.loginData.name;
session.endDialogWithResult({ response: true });
} else {
session.endDialogWithResult({ response: false });
}
}
]);
bot.dialog('validateCode', [
(session) => {
builder.Prompts.text(session, "Please enter the code you received or type 'quit' to end. ");
},
(session, results) => {
const code = results.response;
if (code === 'quit') {
session.endDialogWithResult({ response: false });
} else {
if (code === session.userData.loginData.magicCode) {
// Authenticated, save
session.userData.accessToken = session.userData.loginData.accessToken;
session.userData.refreshToken = session.userData.loginData.refreshToken;
session.endDialogWithResult({ response: true });
} else {
session.send("hmm... Looks like that was an invalid code. Please try again.");
session.replaceDialog('validateCode');
}
}
}
]);
function getAccessTokenWithRefreshToken(refreshToken, callback){
console.log("getAccessTokenWithRefreshToken");
var data = 'grant_type=refresh_token'
+ '&refresh_token=' + refreshToken
+ '&client_id=' + AZUREAD_APP_ID
+ '&client_secret=' + encodeURIComponent(AZUREAD_APP_PASSWORD)
var options = {
method: 'POST',
url: 'https://login.microsoftonline.com/common/oauth2/v2.0/token',
body: data,
json: true,
headers: { 'Content-Type' : 'application/x-www-form-urlencoded' }
};
request(options, function (err, res, body) {
if (err) return callback(err, body, res);
if (parseInt(res.statusCode / 100, 10) !== 2) {
if (body.error) {
return callback(new Error(res.statusCode + ': ' + (body.error.message || body.error)), body, res);
}
if (!body.access_token) {
return callback(new Error(res.statusCode + ': refreshToken error'), body, res);
}
return callback(null, body, res);
}
callback(null, {
accessToken: body.access_token,
refreshToken: body.refresh_token
}, res);
});
}
function getUserLatestEmail(accessToken, callback) {
var options = {
host: 'outlook.office.com', //https://outlook.office.com/api/v2.0/me/messages
path: '/api/v2.0/me/MailFolders/Inbox/messages?$top=1',
method: 'GET',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: 'Bearer ' + accessToken
}
};
https.get(options, function (response) {
var body = '';
response.on('data', function (d) {
body += d;
});
response.on('end', function () {
var error;
if (response.statusCode === 200) {
callback(null, JSON.parse(body));
} else {
error = new Error();
error.code = response.statusCode;
console.log(response.statusMessage);
error.message = response.statusMessage;
// The error body sometimes includes an empty space
// before the first character, remove it or it causes an error.
// body = body.trim();
// console.log(body);
// error = body;
callback(response.statusMessage, null);
}
});
}).on('error', function (e) {
callback(e, null);
});
}