-
Notifications
You must be signed in to change notification settings - Fork 1
/
daemon.js
245 lines (207 loc) · 5.75 KB
/
daemon.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
var express = require('express');
var cors = require('cors');
var bodyParser = require('body-parser');
var expressJwt = require('express-jwt');
var jwt = require('jsonwebtoken');
var serveStatic = require('serve-static');
var hbs = require('hbs');
var Promise = require('es6-promise').Promise;
var _ = require('lodash');
var UUID = require('uuid');
var request = require('superagent');
var levelgraph = require('levelgraph');
var Errors = require('./lib/errors');
var Persona = require('./lib/persona');
var Authorization = require('./lib/authorization');
var Verification = require('./lib/verification');
var FileStore = require('./lib/fileStore');
var config = require('./config');
var daemon = express();
daemon.use(cors({ origin: true, credentials: true }));
daemon.options('*', cors());
daemon.use(bodyParser.json({ type: 'application/json' }));
daemon.use(bodyParser.json({ type: 'application/ld+json' }));
daemon.use(restricted(expressJwt({ secret: config.secrets.jwt, userProperty: 'agent'})));
daemon.use(function (err, req, res, next) {
if (err.name === 'UnauthorizedError') {
res.send(401, 'authentication required');
}
});
daemon.use(addUri());
// HTML
// set view engine to handlebars
daemon.use(serveStatic('.'));
daemon.set('view engine', 'hbs');
hbs.registerHelper('j', function(val){
if(val && val.join){
return val.join(" ");
} else {
return val;
}
});
/*
* no favicon for now!
*/
daemon.get('/favicon.ico', function(req, res){
res.send(404);
});
// TODO refactor storage to dataset.profiles and use LevelGraph
var storage = new FileStore(config.dataDir);
var authorization = new Authorization(levelgraph(process.env.NODE_ENV || 'tmp/' + UUID.v4()));
var verification = new Verification(storage);
daemon.post('/auth/login', function(req, res){
/*
* check audience
* https://developer.mozilla.org/en/Persona/Security_Considerations#Explicitly_specify_the_audience_parameter
*/
if(_.contains(config.audiences, req.headers.origin)){
// persona verify
Persona.verify(req.body.assertion, req.headers.origin)
.then(function(verification){
// TODO generate UUID for logout
var agent = { email: verification.email };
var token = jwt.sign(agent, config.secrets.jwt, { expiresInMinutes: 60*5 });
res.json({ token: token });
})
.catch(function(err){
// TODO add error reporting
console.log(err);
res.send(500);
});
} else {
res.send(403);
}
});
daemon.post('/auth/logout', function(req, res){
// TODO
res.send(200);
});
daemon.get('/:uuid', function(req, res){
storage.get(req.uri) // -> doc
.then(function(doc){
res.format({
'text/html': function(){
var type = doc.type;
// handle case of array
if(typeof type == 'object') type = type[0];
res.render(type, doc);
},
'application/json': function(){
res.type('application/ld+json');
res.send(JSON.stringify(doc));
},
'application/ld+json': function(){
res.type('application/ld+json');
res.send(JSON.stringify(doc));
}
});
})
.catch(function(err){
res.send(statusCode(err), err.message);
});
});
/**
* authentication handled by express-jwt
*/
daemon.post('/', function(req, res){
verification.new(req) // -> req
.then(generateId) // -> req
.then(authorization.create) // -> doc
.then(storage.save) // -> doc
.then(function(profile){
var min = {
"@context": profile["@context"], //TODO handle when no context
"id": profile.id,
"type": profile.type
};
res.type('application/ld+json');
res.send(min);
})
.catch(function(err){
res.send(statusCode(err), err.message);
});
});
/**
* authentication handled by express-jwt
*/
daemon.put('/:uuid', function(req, res){
authorize(req) // -> req
.then(verification.target) // -> doc
.then(storage.save) // -> doc
.then(function(){
res.send(200, 'updated!');
})
.catch(function(err){
res.send(statusCode(err), err.message);
});
});
/**
* authentication handled by express-jwt
*/
daemon.delete('/:uuid', function(req, res){
authorize(req) // -> req
.then(verification.exists) // -> doc
.then(storage.delete) // -> doc
.then(authorization.delete) // -> null
.then(function(){
res.send(204, 'deleted!');
})
.catch(function(err){
res.send(statusCode(err), err.message);
});
});
module.exports = daemon;
/**
* GET requests don't require authentication
* attribution: http://stackoverflow.com/a/19337607/2968245
*/
function restricted(fn) {
return function(req, res, next) {
if (req.method === 'GET' || req.path.match(/\/auth/)) {
next();
} else {
fn(req, res, next);
}
};
}
/**
* TODO get domain from req?
*/
function addUri() {
return function(req, res, next) {
if(req.method !== 'POST') {
req.uri = 'http://' + config.domain + req.path;
}
next();
};
}
function generateId(req) {
return new Promise(function(resolve, reject){
req.body.id = 'http://' + config.domain + '/' + UUID.v4();
resolve(req);
});
}
function authorize(req) {
return new Promise(function(resolve, reject){
authorization.get(req.uri)
.then(function(auth) {
if(!auth || req.agent.email !== auth.object) reject(new Errors.Authorization());
resolve(req);
})
.catch(reject);
});
}
/**
* 401 Unauthorized handled by express-jwt
*/
function statusCode(error) {
var code = 500;
if(error instanceof Errors.Authorization) code = 403;
if(error instanceof Errors.Existance) code = 404;
if(error instanceof Errors.Mismatch) code = 400;
if(error instanceof Errors.Novelity) code = 409;
if(code === 500) {
// TODO add error reporting
}
return code;
}