forked from shykes/haste-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
218 lines (192 loc) · 6.68 KB
/
server.js
File metadata and controls
218 lines (192 loc) · 6.68 KB
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
var http = require('http');
var fs = require('fs');
var winston = require('winston');
var passport = require('passport');
var express = require('express')
var session = require('express-session')
var jwt = require('jsonwebtoken')
require('dotenv').config();
var DocumentHandler = require('./lib/document_handler');
// Load the configuration and set some defaults
var config = JSON.parse(fs.readFileSync('./config.js', 'utf8'));
config.port = process.env.PORT || config.port || 7777;
config.host = process.env.HOST || config.host || 'localhost';
config.session_secret = process.env.SESSION_SECRET;
config.scheme = process.env.SCHEME || config.scheme || 'https'
config.origin = config.scheme + '://' + config.host + ":" + config.port + "/";
config.restrict_domain = process.env.RESTRICT_DOMAIN
// Set up the logger
if (config.logging) {
try {
winston.remove(winston.transports.Console);
} catch(err) {
console.log(err)
}
var detail, type;
for (var i = 0; i < config.logging.length; i++) {
detail = config.logging[i];
type = detail.type;
delete detail.type;
winston.add(winston.transports[type], detail);
}
}
// build the store from the config on-demand - so that we don't load it
// for statics
if (!config.storage) {
config.storage = { type: 'file' };
}
if (!config.storage.type) {
config.storage.type = 'file';
}
var Store, preferredStore;
if (process.env.REDISTOGO_URL && config.storage.type === 'redis') {
var redisClient = require('redis-url').connect(process.env.REDISTOGO_URL);
Store = require('./lib/document_stores/redis');
preferredStore = new Store(config.storage, redisClient);
}
else {
Store = require('./lib/document_stores/' + config.storage.type);
preferredStore = new Store(config.storage);
}
// Compress the static javascript assets
if (config.recompressStaticAssets) {
var jsp = require("uglify-js").parser;
var pro = require("uglify-js").uglify;
var list = fs.readdirSync('./static');
var dest;
for (var i = 0; i < list.length; i++) {
var item = list[i];
var orig_code, ast;
if ((item.indexOf('.js') === item.length - 3) &&
(item.indexOf('.min.js') === -1)) {
dest = item.substring(0, item.length - 3) + '.min' +
item.substring(item.length - 3);
orig_code = fs.readFileSync('./static/' + item, 'utf8');
ast = jsp.parse(orig_code);
ast = pro.ast_mangle(ast);
ast = pro.ast_squeeze(ast);
fs.writeFileSync('./static/' + dest, pro.gen_code(ast), 'utf8');
winston.info('compressed ' + item + ' into ' + dest);
}
}
}
// Send the static documents into the preferred store, skipping expirations
var path, data;
for (var name in config.documents) {
path = config.documents[name];
data = fs.readFileSync(path, 'utf8');
winston.info('loading static document', { name: name, path: path });
if (data) {
preferredStore.set(name, data, function(cb) {
winston.debug('loaded static document', { success: cb });
}, true);
}
else {
winston.warn('failed to load static document', { name: name, path: path });
}
}
// Pick up a key generator
var pwOptions = config.keyGenerator || {};
pwOptions.type = pwOptions.type || 'random';
var gen = require('./lib/key_generators/' + pwOptions.type);
var keyGenerator = new gen(pwOptions);
// Configure the document handler
var documentHandler = new DocumentHandler({
store: preferredStore,
maxLength: config.maxLength,
keyLength: config.keyLength,
keyGenerator: keyGenerator
});
var app = express();
// Rate limit all requests
var GoogleStrategy = require('passport-google-oauth20').Strategy;
var OAUTH_SCOPE = ['profile', 'email', 'openid']
passport.serializeUser(function(user, cb) {
cb(null, user);
});
passport.deserializeUser(function(obj, cb) {
cb(null, obj);
});
passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: config.origin + 'auth/google/callback',
},
//function(accessToken, refreshToken, profile, cb) {
function( accessToken, refreshToken, params, profile, cb){
if (! params.id_token ){
winston.error("no id_token in response")
return
}
var jwtObject = jwt.decode(params.id_token)
if(! (jwtObject && jwtObject.hd && matchDomain(jwtObject.hd)) ){
// domain doesn't validate
winston.info("domain does not validate")
return cb('Your domain is not permitted')
}
return cb(null, profile);
}
));
app.use(session({ secret: config.session_secret, name: 'tt' , resave:true, saveUnitialized: true}));
// first look at API calls
app.use(passport.initialize());
app.use(passport.session());
var router = app;
// get raw documents - support getting with extension
router.get('/', ensureAuthenticatedWeb);
router.get('/login', passport.authenticate('google', { scope: OAUTH_SCOPE }));
router.get( '/auth/google/callback', function(req,res,next){
var successRedirectURL = '/'
if(req.query.state){
successRedirectURL = req.query.state
}
passport.authenticate( 'google', { scope: OAUTH_SCOPE,
successRedirect: successRedirectURL,
failureRedirect: '/auth/failure' } )(req,res,next);
})
router.get('/raw/:id', ensureAuthenticatedWeb, function(request, response, next) {
var skipExpire = !!config.documents[request.params.id];
var key = request.params.id.split('.')[0];
return documentHandler.handleRawGet(key, response, skipExpire);
});
// add documents
router.post('/documents', ensureAuthenticatedAPI, function(request, response, next) {
return documentHandler.handlePost(request, response);
});
router.get('/documents/:id', ensureAuthenticatedAPI, function(request, response, next) {
var skipExpire = !!config.documents[request.params.id];
return documentHandler.handleGet(
request.params.id,
response,
skipExpire
);
});
router.get('/users/me', ensureAuthenticatedAPI, function(req, res, next) {
return res.json(req.user);
});
function matchDomain(domain){
var pattern = new RegExp(config.restrict_domain)
return pattern.test(domain)
}
function ensureAuthenticatedWeb(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
// set state = req.path to support redirect after login
passport.authenticate(
'google', { scope: OAUTH_SCOPE, state : req.path }
)(req,res,next)
}
function ensureAuthenticatedAPI(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
res.sendStatus(401);
}
app.use( express.static(__dirname + '/static' ) )
// a request for a record returns the index
app.get('/:id', ensureAuthenticatedWeb, function(request, response) {
response.sendFile(__dirname + '/static/index.html')
});
http.createServer(app).listen(config.port, '0.0.0.0');
winston.info('listening on ' + config.host + ':' + config.port);