-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
192 lines (163 loc) · 5.99 KB
/
index.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
/*
Copyright (c) 2017 Unify Inc.
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.
*/
'use strict';
const http = require('http');
const https = require('https');
const fs = require('fs');
const express = require('express');
const bodyParser = require('body-parser');
const Session = require('express-session');
const request = require('request');
const randomstring = require('randomstring');
const OAuth2 = require('simple-oauth2');
const linkifier = require('./linkifier');
const store = require('./store');
const log = require('./logger').log;
// Load configuration
const config = require('./config.json');
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
let port = process.env.PORT || config.app.port || 3000;
// Init storage
store.init();
// Initialize linkifier
linkifier.init(config.circuit.domain);
// simple-oauth2 configuration
const oauth2 = OAuth2.create({
client: {
id: config.circuit.client_id,
secret: config.circuit.client_secret
},
auth: {
tokenHost: `https://${config.circuit.domain}`
}
});
// OAuth2 redirect uri
let portInUrl = config.app.includePortInRedirectURL ? `:${port}` : '';
const redirectUri = `${config.app.domain}${portInUrl}/oauthCallback`
// Create app
var app = express();
// Configure view engine, render EJS templates and serve static assets
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.static('public'));
// Create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })
// Setup express session. No store used in this example.
app.use(Session({
secret: 'secret-5492',
resave: true,
saveUninitialized: true
}));
// Middleware to ensure user is authenticated
function auth(req, res, next) {
req.session.isAuthenticated ? next() : res.redirect('/');
}
app.get('/login', (req, res) => {
// Create state parameter to prevent CSRF attacks. Save in session.
req.session.oauthState = randomstring.generate(12);
// Redirect to OAuth2 authorize url
let url = oauth2.authorizationCode.authorizeURL({
redirect_uri: redirectUri,
scope: config.circuit.scope,
state: req.session.oauthState
});
res.redirect(url);
});
// Routes
app.get('/', (req, res) => {
if (req.session.isAuthenticated) {
// Read settings and show currently active linkifiers
config.linkify.forEach(item => {
let settings = store.getSettings(req.session.userId);
item.active = settings && !!settings[item.id];
});
}
res.render('manage', {
domain: config.circuit.domain,
data: config.linkify,
authenticated: req.session.isAuthenticated,
displayName: req.session.displayName
});
});
app.get('/logout', (req, res) => {
req.session.isAuthenticated = false;
res.redirect('/');
});
app.get('/oauthCallback', (req, res) => {
// Verify code is present and state matches to prevent CSRF attacks
if (req.query.code && req.session.oauthState === req.query.state) {
// Get the access token using the code
oauth2.authorizationCode.getToken({
code: req.query.code,
redirect_uri: redirectUri
})
.then(result => {
// Save access token in session
if (result.error) {
Promise.reject(result.error_description);
return;
}
return oauth2.accessToken.create(result).token;
})
.then(token => {
req.session.isAuthenticated = true;
// Get the userId & displayName and store in session
request.get(`https://${config.circuit.domain}/rest/v2/users/profile`, {
'auth': { 'bearer': token.access_token }
}, (err, httpResponse, body) => {
if (err) {
Promise.reject(err);
}
let user = JSON.parse(body);
req.session.userId = user.userId;
req.session.displayName = user.displayName;
store.saveToken(user.userId, token);
res.redirect('/');
});
})
.catch(err => {
log.error(err);
res.render('error', {error: err});
});
} else {
// Access denied
res.render('error', {error: 'Access Denied'});
}
});
app.post('/activate', urlencodedParser, auth, (req, res) => {
// Save the new settings
store.saveSettings(req.session.userId, req.body)
.then(settings => linkifier.update(req.session.userId))
.then(() => res.redirect('/success'))
.catch(log.error);
});
app.get('/success', auth, (req, res) => {
// Show success message
res.render('success', {domain: config.circuit.domain});
});
// Start app
var server = http.createServer(app);
/* For https
var server = https.createServer({
key: fs.readFileSync('cert/server.key'),
cert: fs.readFileSync('cert/server-cert.pem')
}, app); */
server.listen(port);
server.on('listening', () =>
log.info(`listening on ${port}`));