forked from marklogic-community/demo-cat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
210 lines (187 loc) · 6.01 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
209
210
/*jshint node: true */
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var expressSession = require('express-session');
var http = require('http');
var path = require('path');
var ui = path.join(__dirname, 'ui/app');
function getAuth(options, session) {
'use strict';
if (session.user !== undefined && session.user.name !== undefined) {
return session.user.name + ':' + session.user.password;
} else {
return options.defaultUser + ':' + options.defaultPass;
}
}
exports.buildExpress = function(options) {
'use strict';
var express = require('express');
var app = express();
app.use(cookieParser());
app.use(expressSession({secret: '1234567890QWERTY'}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.set('views', ui);
app.set('view engine', 'ejs');
function proxy(req, res) {
var queryString = req.originalUrl.split('?')[1];
console.log(req.method + ' ' + req.path + ' proxied to ' + options.mlHost + ':' + options.mlPort + req.path + (queryString ? '?' + queryString : ''));
var mlReq = http.request({
hostname: options.mlHost,
port: options.mlPort,
method: req.method,
path: req.path + (queryString ? '?' + queryString : ''),
headers: req.headers,
auth: getAuth(options, req.session)
}, function(response) {
response.on('data', function(chunk) {
res.write(chunk);
});
response.on('end', function() {
res.end();
});
});
if (req.body !== undefined) {
mlReq.write(JSON.stringify(req.body));
mlReq.end();
}
mlReq.on('error', function(e) {
console.log('Problem with request: ' + e.message);
});
}
app.get('/user/status', function(req, res) {
if (req.session.user === undefined) {
res.send('{"authenticated": false}');
} else {
res.send({
authenticated: true,
username: req.session.user.name,
profile: req.session.user.profile
});
}
});
app.get('/user/login', function(req, res) {
// or maybe we can try to read the profile and distinguish between 401 and 404
// 404 - valid credentials, but no profile yet
// 401 - bad credentials
var login = http.get({
hostname: options.mlHost,
port: options.mlPort,
path: '/v1/documents?uri=/users/' + req.query.username + '.json',
headers: req.headers,
auth: req.query.username + ':' + req.query.password
}, function(response) {
if (response.statusCode === 401) {
res.statusCode = 401;
res.send('Unauthenticated');
} else if (response.statusCode === 404) {
// authentication successful, but no profile defined
req.session.user = {
name: req.query.username,
password: req.query.password
};
res.send(200, {
authenticated: true,
username: req.query.username
});
} else {
if (response.statusCode === 200) {
// authentication successful, remember the username
req.session.user = {
name: req.query.username,
password: req.query.password
};
response.on('data', function(chunk) {
var json = JSON.parse(chunk);
if (json.user !== undefined) {
req.session.user.profile = {
fullname: json.user.fullname,
emails: json.user.emails
};
res.send(200, {
authenticated: true,
username: req.query.username,
profile: req.session.user.profile
});
} else {
console.log('did not find chunk.user');
}
});
}
}
});
login.on('error', function(e) {
console.log('login failed: ' + e.statusCode);
});
});
app.get('/user/logout', function(req, res) {
delete req.session.user;
res.send();
});
app.post('/demo/create', function(req, res) {
var queryString = req.originalUrl.split('?')[1];
var mlReq = http.request({
hostname: options.mlHost,
port: options.mlPort,
method: 'POST',
path: '/v1/documents?' + queryString,
headers: req.headers,
auth: getAuth(options, req.session)
}, function(response) {
console.log('created demo at: ' + response.headers.location);
res.write(JSON.stringify({uri: response.headers.location.replace(/(.*\?uri=)/, '')}));
response.on('data', function(chunk) {
res.write(chunk);
});
response.on('end', function() {
res.end();
});
});
if (req.body !== undefined) {
mlReq.write(JSON.stringify(req.body));
mlReq.end();
}
mlReq.on('error', function(e) {
console.log('Problem with request: ' + e.message);
});
});
app.get('/v1*', function(req, res){
if (req.session.user === undefined) {
res.send(401, 'Unauthorized');
} else {
proxy(req, res);
}
});
app.put('/v1*', function(req, res){
var user = req.session.user;
var escapedUserName = (user && user.name) ? user.name.replace(/([\(\)[{*+.$^\\|?\-])/g, '\\$1') : '';
if (user === undefined) {
res.send(401, 'Unauthorized');
} else if (req.path === '/v1/documents' &&
req.query.uri.match('/users/') &&
req.query.uri.match(new RegExp('/users/[^(' + escapedUserName + ')]+.json'))) {
// The user is try to PUT to a profile document other than his/her own. Not allowed.
res.send(403, 'Forbidden');
} else {
if (req.path === '/v1/documents' && req.query.uri.match('/users/')) {
// TODO: The user is updating the profile. Update the session info.
}
proxy(req, res);
}
});
app.post('/v1*', function(req, res){
if (req.session.user === undefined) {
res.send(401, 'Unauthorized');
} else {
proxy(req, res);
}
});
// Redirect all other traffic to Angular
app.use(express.static(__dirname + '/ui/app'));
app.use('/*', function(req, res){
res.render('index');
});
return app;
};