-
Notifications
You must be signed in to change notification settings - Fork 0
/
requestHandlers.js
155 lines (130 loc) · 5.83 KB
/
requestHandlers.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
// Copyright (c) Microsoft. All rights reserved. Licensed under the MIT license. See full license at the bottom of this file.
var querystring = require("querystring"),
url = require("url"),
exchange = require("./lib/exchange"),
authHelper = require("./authHelper"),
SessionManager = require("./sessionManager").SessionManager;
var sessionManager = new SessionManager();
function mail(response, request) {
console.log("Request handler 'mail' was called.");
var session = sessionManager.getSession(request, response);
console.log("Session: " + session);
if (session.token) {
// We have a token, render the mail list
renderMailPage(response, session);
}
else {
// Need to kick off code grant flow
render#Page(response);
}
}
function calendar(response, request) {
console.log("Request handler 'calendar' was called.");
var session = sessionManager.getSession(request, response);
console.log("Session: " + session);
if (session.token){
// We have a token, render the calendar
renderCalendarPage(response, session);
}
else {
// Need to kick off code grant flow
render#Page(response, session);
}
}
function contacts(response, request) {
console.log("Request handler 'contacts' was called.");
response.writeHead(200, {"Content-Type": "text/html"});
response.write('<p>Nothing to see here. <a href="mail">Go back to mail</a></p>');
response.end();
}
function authorize(response, request) {
console.log("Request handler 'authorize' was called.");
var session = sessionManager.getSession(request, response);
console.log("Session: " + session);
var url_parts = url.parse(request.url, true);
var code = url_parts.query.code;
console.log("Code: " + code);
if (code && code !== ""){
var result = authHelper.getTokenFromCode(code, "https://outlook.office365.com",
response, '/calendar', session);
}
else {
console.log("Bad code.");
}
}
function writeSession(response, session) {
response.write('<table style="width:100%"><tr><th>Session field</th><th>Value</th></tr>');
response.write("<tr><td>Session ID</td><td>" + session + "</td></tr>");
var accessToken = session.token ? session.token.token.access_token : "NONE";
var refreshToken = session.token ? session.token.token.refresh_token : "NONE";
var expireDate = session.token ? new Date(session.token.token.expires_on * 1000) : "NONE";
response.write("<tr><td>Access Token</td><td>" + accessToken + "</td></tr>");
response.write("<tr><td>Refresh Token</td><td>" + refreshToken + "</td></tr>");
response.write("<tr><td>Access Token Expires</td><td>" + expireDate + "</td></tr>");
response.write('</table>');
}
function render#Page(response, session) {
response.writeHead(200, {"Content-Type": "text/html"});
response.write('<p>Please <a href="' + authHelper.getAuthUrl() + '">#</a> with your Office 365 account.</p>');
writeSession(response, session);
response.end();
}
function renderMailPage(response, session) {
var outlookClient = new exchange.Microsoft.OutlookServices.Client('https://outlook.office365.com/api/v1.0',
authHelper.getAccessTokenFn('https://outlook.office365.com/', session));
response.writeHead(200, {"Content-Type": "text/html"});
outlookClient.me.folders.getFolder('Inbox').messages.getMessages()
.orderBy('DateTimeReceived desc').fetchAll(10).then(function (result) {
result.forEach(function (message) {
response.write("<p>Message: " + message.subject + ", Received: " + message.dateTimeReceived.toString() + "</p>");
});
response.end();
}, function (error) {
console.log(error);
response.write("<p>ERROR: " + error + "</p>");
response.end();
});
}
function renderCalendarPage(response, session) {
var outlookClient = new exchange.Microsoft.OutlookServices.Client('https://outlook.office365.com/api/v1.0',
authHelper.getAccessTokenFn('https://outlook.office365.com/', session));
response.writeHead(200, {"Content-Type": "text/html"});
response.write('<table style="width:100%"><tr><th>Subject</th><th>Start</th><th>End</th></tr>');
outlookClient.me.events.getEvents().fetch().then(function (result) {
result.currentPage.forEach(function (event) {
response.write("<tr><td>" + event.subject + "</td><td>" + event.start.toString() + "</td><td>" + event.end.toString() + "</td></tr>");
});
response.write('</table>');
writeSession(response, session);
response.end();
}, function (error) {
console.log(error);
response.write('</table');
response.write("<p>ERROR: " + error + "</p>");
writeSession(response, session);
response.end();
});
}
exports.mail = mail;
exports.calendar = calendar;
exports.contacts = contacts;
exports.authorize = authorize;
/*
MIT License:
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
""Software""), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/