-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
208 lines (186 loc) · 6.72 KB
/
server.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
'use strict';
const express = require('express');
const request = require('request');
const fs = require('fs');
const path = require('path');
const log = require('console-log-level')({
prefix: function (level) {
return new Date().toISOString() + ' - ' + level.toUpperCase() + ' - '
},
level: 'info'// TODO: Get from environment variable for docker
});
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const simpleOauthModule = require('simple-oauth2');
const port = 3000;
const app = express();
app.use(cookieParser());
app.use(bodyParser());
app.listen(port, () => {
log.info(`Server started and listening on port ${port}!`);
});
let oauth_settings = {};
// Read in oauth settings from either environment variables (docker) or settings file
// TODO: Get from environment variables first (for docker support)
oauth_settings = JSON.parse(fs.readFileSync('./config/oauth_settings.json', 'utf8'));
const flowUriHost = 'https://api.flowdock.com';
const BASE_PATH = '/notifier';
const BASE_PATH_API = `${BASE_PATH}/api`;
// Let files be served from the public directory
app.use(`${BASE_PATH}/app`, express.static(path.join(__dirname, './app')));
app.use(BASE_PATH, express.static(path.join(__dirname, './app')));
const oauth2 = simpleOauthModule.create({
client: {
id: oauth_settings.client_id,
secret: oauth_settings.client_secret,
},
auth: {
tokenHost: flowUriHost,
tokenPath: '/oauth/token',
authorizePath: '/oauth/authorize',
},
});
// Authorization uri definition
const authorizationUri = oauth2.authorizationCode.authorizeURL({
redirect_uri: oauth_settings.redirect_uri,
scope: 'flow private',
state: Math.random(),
});
// Initial page redirecting to auth
app.get(`${BASE_PATH_API}/auth`, (req, res) => {
// res.send('hello');
const tokenCookie = req.cookies['token_obj'];
if(tokenCookie !== undefined && tokenCookie !== null) {
let accessToken = {};
try {
accessToken = oauth2.accessToken.create(tokenCookie);
} catch(e) {
log.error(`It appears we have caught an error while creating ` +
`the token. Redirecting to ${oauth_settings.redirect_uri}`)
res.redirect(authorizationUri);
return;
}
// Test if the token is expired
if( accessToken.expired() ) {
log.debug('Token is expired');
accessToken.refresh().then((result) => {
accessToken = result;
res.cookie('token_obj', result, {httpOnly: false});
return res.redirect(BASE_PATH + '/app');
}).catch((error) => {
log.error('An error occurred while attempting to refresh the token. Having user re-auth. Error: ', error);
res.redirect(authorizationUri);
});
}else {
// Test that user can still access with this access_token.
const reqUri = flowUriHost + '/organizations?access_token=' + tokenCookie.access_token;
request.get(reqUri, function(error, resp, body) {
const messageFromBody = JSON.parse(body).message;
if(messageFromBody !== undefined) {
log.debug('Not authorized!');
res.redirect(authorizationUri);
}else {
return res.redirect(BASE_PATH + '/app');
}
});
}
}else {
log.debug('Cookie tokenCookie was null or doesn\'t exist thus not authorized!');
res.redirect(authorizationUri);
}
});
function retrieveToken(req, res, code) {
const options = {
client_id: oauth_settings.client_id,
client_secret: oauth_settings.client_secret,
code: code,
redirect_uri: oauth_settings.redirect_uri
};
oauth2.authorizationCode.getToken(options, (error, result) => {
try {
if (error) {
log.error('Access Token Error', error.message);
return res.json('Authentication failed');
}
res.cookie('token_obj', result, {httpOnly: false});
log.debug('getToken() result: ', result);
return res.redirect(BASE_PATH + '/app');
}catch(e) {
log.error('Something bad happened', e);
return res.status(500);
}
});
}
// Callback service parsing the authorization token and asking for the access token
app.get(`${BASE_PATH_API}/callback`, (req, res) => {
const code = req.query.code;
retrieveToken(req, res, code);
});
app.get(`${BASE_PATH_API}/FlowdockProxy`, (req, res) => {
const proxy = req.query.proxy;
if( !proxy.startsWith('https://api.flowdock.com/') ) {
return res.status(400).json('Only flowdock api is acceptable to proxy');
}else {
// Clone req.query so that we can delete properties without causing problems
let queryWithoutProxy = Object.assign({}, req.query);
delete queryWithoutProxy.proxy;
request.get({
url: proxy,
qs: queryWithoutProxy
}, function(error, resp, body) {
if(error) {
log.error('An error occurred while proxying Flowdock request. Error: ', error);
return res.status(500).json(error);
}else {
// Check if we need to re-auth again; if so, redirect to /notifier/api/auth
log.debug('body: ' + body);
const messageFromBody = JSON.parse(body).message;
log.debug('messageFromBody: ' + messageFromBody);
if(messageFromBody !== undefined) {
return res.status(500).json('Requires re-auth');
}
return res.status(200).send(body);
}
});
}
});
app.get(`${BASE_PATH_API}/get/user`, (req, res) => {
const proxy = req.query.proxy;
log.debug('proxy: ', proxy);
if( !proxy.startsWith('https://api.flowdock.com/') ) {
let returnMsg = 'Only flowdock api is acceptable to proxy';
log.error(msg);
return res.status(400).json(returnMsg);
}else {
log.debug('Fetching user from flowdock');
// Clone req.query so that we can delete properties without causing problems
let queryWithoutProxy = Object.assign({}, req.query);
delete queryWithoutProxy.proxy;
request.get({
url: proxy,
qs: queryWithoutProxy
}, function(error, resp, body) {
if(error) {
log.error('An error occurred while proxying Flowdock request. Error: ', error);
return res.status(500).json(error);
}else {
// Check if we need to re-auth again; if so, redirect to /notifier/api/auth
log.debug('body: ' + body);
const messageFromBody = JSON.parse(body).message;
log.debug('messageFromBody: ' + messageFromBody);
if(messageFromBody !== undefined && messageFromBody !== 'Access denied') {
return res.status(500).json('Requires re-auth');
}
const userObj = JSON.parse(body);
// return the body regardless of what is in it.
return res.status(200).send(body);
}
});
}
});
app.get(BASE_PATH + '/', (req, res) => {
res.redirect(`${BASE_PATH_API}/auth`);
});
app.get('/', (req, res) => {
res.redirect(`${BASE_PATH_API}/auth`);
});