Skip to content

Commit 066c791

Browse files
author
nicola
committed
adding idp
1 parent 3d809ec commit 066c791

6 files changed

Lines changed: 336 additions & 16 deletions

File tree

‎lib/handlers/delete.js‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ function handler(req, res, next) {
1111
debug('DELETE -- ' + req.originalUrl);
1212

1313
var ldp = req.app.locals.ldp;
14-
var filename = utils.uriToFilename(req.path, ldp.root);
1514

16-
ldp.delete(filename, function(err) {
15+
ldp.delete(req.path, function(err) {
1716
if (err) {
1817
debug("DELETE -- error: " + err);
1918
return next(err);

‎lib/handlers/get.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function get(req, res, next) {
4040
debug(req.method + ' -- ' + req.originalUrl);
4141

4242
// Get resource or container
43-
ldp.get(filename, uri, includeBody, function(err, data, container) {
43+
ldp.get(req.path, uri, includeBody, function(err, data, container) {
4444

4545
// This should be implemented in LDP.prototype.get
4646
if (err && err.status === 404 && glob.hasMagic(filename)) {

‎lib/handlers/put.js‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ function handler(req, res, next) {
1616
debug("PUT -- Text length: " + (req.text ? req.text.length : 'undefined'));
1717
res.header('MS-Author-Via' , 'SPARQL' );
1818

19-
var filePath = utils.uriToFilename(req.path, ldp.root);
20-
21-
ldp.put(filePath, req.text, function(err) {
19+
ldp.put(req.path, req.text, function(err) {
2220
if (err) {
2321
debug("PUT -- Write error: " + err.message);
2422
err.message = "Can't write file: " + err.message;

‎lib/identity-provider.js‎

Lines changed: 297 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,297 @@
1+
module.exports = IdentityProvider
2+
3+
var webid = require('webid')
4+
var $rdf = require('rdflib')
5+
var uuid = require('uuid')
6+
var sym = $rdf.sym
7+
var lit = $rdf.lit
8+
9+
function defaultBuildURI (account, host, port) {
10+
var hostAndPort = host
11+
if (port) {
12+
hostAndPort = hostAndPort + port
13+
}
14+
if (!hostAndPort) hostAndPort = 'localhost'
15+
return 'https://' + account.toLowerCase() + '.' + hostAndPort + '/'
16+
}
17+
18+
function IdentityProvider (options) {
19+
if (!(this instanceof IdentityProvider)) {
20+
return new IdentityProvider(options)
21+
}
22+
23+
var self = this
24+
25+
options = options || {}
26+
self.store = options.store
27+
self.pathCard = options.pathCard || 'profile/card'
28+
self.suffixURI = options.suffixURI || '#me'
29+
self.buildURI = options.buildURI || defaultBuildURI
30+
}
31+
32+
IdentityProvider.prototype.create = function (options, callback) {
33+
if (!options || !options.account) {
34+
var err = new Error('You must enter an account name!')
35+
err.statusCode = 406 // TODO
36+
return callback(err)
37+
}
38+
39+
var self = this
40+
options.url = options.url || self.buildURI(options.account, self.host)
41+
options.card = options.url + self.pathCard
42+
options.agent = options.card + self.suffixURI
43+
44+
// TODO maybe use promises
45+
self.setupCard(options, function (err) {
46+
if (err) {
47+
return callback(err)
48+
}
49+
50+
self.setupSpace(options, function () {
51+
if (err) {
52+
callback(err)
53+
}
54+
55+
callback(null, options.agent)
56+
})
57+
})
58+
}
59+
60+
IdentityProvider.prototype.setupSpace = function (options, callback) {
61+
var self = this
62+
63+
self.createRootAcl(options, function (err) {
64+
if (err) {
65+
err.statusCode = 500
66+
return callback(err)
67+
}
68+
69+
callback()
70+
})
71+
}
72+
73+
IdentityProvider.prototype.createRootAcl = function (options, callback) {
74+
var email = options.email
75+
var url = options.url
76+
var acl = options.url + options.suffixAcl
77+
var owner = acl + '#owner'
78+
var graph = $rdf.graph()
79+
var agent = options.agent
80+
81+
graph.add(
82+
sym(owner),
83+
sym('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
84+
sym('http://www.w3.org/ns/auth/acl#Authorization'))
85+
graph.add(
86+
sym(owner),
87+
sym('http://www.w3.org/ns/auth/acl#accessTo'),
88+
sym(url))
89+
graph.add(
90+
sym(owner),
91+
sym('http://www.w3.org/ns/auth/acl#accessTo'),
92+
sym(url))
93+
graph.add(
94+
sym(owner),
95+
sym('http://www.w3.org/ns/auth/acl#agent'),
96+
sym(agent))
97+
98+
if (email.length > 0) {
99+
graph.add(
100+
sym(owner),
101+
sym('http://www.w3.org/ns/auth/acl#agent'),
102+
sym('mailto:' + email))
103+
}
104+
105+
graph.add(
106+
sym(owner),
107+
sym('http://www.w3.org/ns/auth/acl#defaultForNew'),
108+
sym(url))
109+
graph.add(
110+
sym(owner),
111+
sym('http://www.w3.org/ns/auth/acl#mode'),
112+
sym('http://www.w3.org/ns/auth/acl#Read'))
113+
graph.add(
114+
sym(owner),
115+
sym('http://www.w3.org/ns/auth/acl#mode'),
116+
sym('http://www.w3.org/ns/auth/acl#Write'))
117+
graph.add(
118+
sym(owner),
119+
sym('http://www.w3.org/ns/auth/acl#mode'),
120+
sym('http://www.w3.org/ns/auth/acl#Control'))
121+
122+
var content = graph.serialize(acl, 'text/turtle')
123+
return this.store.put(acl, content, callback)
124+
}
125+
126+
IdentityProvider.prototype.setupCard = function (options, callback) {
127+
var self = this
128+
129+
self.createCard(options, function (err) {
130+
if (err) {
131+
err.statusCode = 500 // TODO this should send 406 if taken
132+
return callback(err)
133+
}
134+
135+
// TODO pass the right options
136+
self.createCardAcl(options, function (err) {
137+
if (err) {
138+
err.statusCode = 500 // TODO
139+
return callback(err)
140+
}
141+
142+
// TODO create all the needed folders?
143+
return callback(err)
144+
})
145+
})
146+
}
147+
148+
IdentityProvider.prototype.createCard = function (options, callback) {
149+
var self = this
150+
151+
// TODO implement generate in webid
152+
var graph = webid.generate({
153+
// TODO list all the attributes
154+
// if any
155+
})
156+
157+
var card = options.card
158+
159+
var content = graph.serialize(card, 'text/turtle')
160+
return self.store.put(card, content, callback)
161+
}
162+
163+
IdentityProvider.prototype.createCardAcl = function (options, callback) {
164+
var graph = $rdf.graph()
165+
var url = options.card
166+
var acl = url + options.suffixAcl
167+
var agent = options.agent
168+
var owner = acl + '#owner'
169+
var readAll = acl + '#readall'
170+
171+
graph.add(
172+
sym(owner),
173+
sym('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
174+
sym('http://www.w3.org/ns/auth/acl#Authorization'))
175+
graph.add(
176+
sym(owner),
177+
sym('http://www.w3.org/ns/auth/acl#accessTo'),
178+
sym(url))
179+
180+
// This is soon to be deprecated
181+
graph.add(
182+
sym(owner),
183+
sym('http://www.w3.org/ns/auth/acl#accessTo'),
184+
sym(acl))
185+
186+
graph.add(
187+
sym(owner),
188+
sym('http://www.w3.org/ns/auth/acl#agent'),
189+
sym(agent))
190+
graph.add(
191+
sym(owner),
192+
sym('http://www.w3.org/ns/auth/acl#mode'),
193+
sym('http://www.w3.org/ns/auth/acl#Read'))
194+
graph.add(
195+
sym(owner),
196+
sym('http://www.w3.org/ns/auth/acl#mode'),
197+
sym('http://www.w3.org/ns/auth/acl#Write'))
198+
graph.add(
199+
sym(owner),
200+
sym('http://www.w3.org/ns/auth/acl#mode'),
201+
sym('http://www.w3.org/ns/auth/acl#Control'))
202+
203+
graph.add(
204+
sym(readAll),
205+
sym('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
206+
sym('http://www.w3.org/ns/auth/acl#Authorization'))
207+
graph.add(
208+
sym(readAll),
209+
sym('http://www.w3.org/ns/auth/acl#accessTo'),
210+
sym(url))
211+
graph.add(
212+
sym(readAll),
213+
sym('http://www.w3.org/ns/auth/acl#agentClass'),
214+
sym('http://xmlns.com/foaf/0.1/Agent'))
215+
graph.add(
216+
sym(readAll),
217+
sym('http://www.w3.org/ns/auth/acl#mode'),
218+
sym('http://www.w3.org/ns/auth/acl#Read'))
219+
220+
var content = graph.serialize(acl, 'text/turtle')
221+
return this.store.put(acl, content, callback)
222+
}
223+
224+
// TODO create workspaces
225+
// TODO create master acl
226+
227+
IdentityProvider.prototype.post = function (req, res, next) {
228+
var self = this
229+
self.create(req.body, function (err, agent) {
230+
if (err) {
231+
return next(err)
232+
}
233+
234+
res.session.agent = agent
235+
res.set('User', agent)
236+
237+
if (req.body['spkac'] && req.body['spkac'].length > 0) {
238+
self.setupWebidTLS({
239+
spkac: req.body['spkac'],
240+
agent: agent
241+
}, next)
242+
} else {
243+
next()
244+
}
245+
})
246+
}
247+
248+
IdentityProvider.prototype.setupWebidTLS = function (options, callback) {
249+
var self = this
250+
webid('tls').generate(options, function (err, cert) {
251+
if (err) {
252+
err.statusCode = 500
253+
return callback(err)
254+
}
255+
var id = uuid.v4()
256+
var card = options.agent.split('#')[0]
257+
var agent = options.agent
258+
var key = card + '#key' + id
259+
260+
var graph = $rdf.graph()
261+
graph.add(
262+
sym(agent),
263+
sym('http://www.w3.org/ns/auth/cert#key'),
264+
sym(key))
265+
graph.add(
266+
sym(key),
267+
sym('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
268+
sym('http://www.w3.org/ns/auth/cert#RSAPublicKey'))
269+
graph.add(
270+
sym(key),
271+
sym('http://www.w3.org/2000/01/rdf-schema#label'),
272+
lit('Created on ' + (new Date()).toString()))
273+
graph.add(
274+
sym(key),
275+
sym('http://www.w3.org/ns/auth/cert#modulus'),
276+
lit(cert.mod)) // add Datatype "http://www.w3.org/2001/XMLSchema#hexBinary"
277+
graph.add(
278+
sym(key),
279+
sym('http://www.w3.org/ns/auth/cert#exponent'),
280+
lit(cert.exponent)) // TODO add Datatype "http://www.w3.org/2001/XMLSchema#int"
281+
282+
// TODO merge to append
283+
self.store.merge(card, graph, callback)
284+
})
285+
}
286+
287+
IdentityProvider.prototype.middleware = function (req, res, next) {
288+
var self = this
289+
290+
if (req.method === 'POST') {
291+
self.post(req, res, next)
292+
} else {
293+
var err = new Error('Can only do GET or POST')
294+
err.statusCode = 406
295+
next(err)
296+
}
297+
}

‎lib/ldp.js‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,10 @@ LDP.prototype.listContainer = function (filename, uri, containerData, callback)
316316
});
317317
};
318318

319-
LDP.prototype.put = function (filePath, contents, callback) {
319+
LDP.prototype.put = function (resourcePath, contents, callback) {
320+
var ldp = this;
321+
var filePath = utils.uriToFilename(resourcePath, ldp.root);
322+
320323
// PUT requests not supported on containers. Use POST instead
321324
if (filePath[filePath.length - 1] === '/') {
322325
return callback(new HttpError({
@@ -333,7 +336,7 @@ LDP.prototype.put = function (filePath, contents, callback) {
333336
message: err.message
334337
}));
335338
}
336-
return fs.writeFile(filePath, contents, function() {
339+
return fs.writeFile(filePath, contents, function (err) {
337340
if (err) {
338341
debug.handlers("PUT -- Error writing file: " + err);
339342
return callback(new HttpError({
@@ -347,14 +350,15 @@ LDP.prototype.put = function (filePath, contents, callback) {
347350
});
348351
};
349352

350-
LDP.prototype.get = function(filename, uri, includeBody, callback) {
353+
LDP.prototype.get = function (resourcePath, uri, includeBody, callback) {
351354
var ldp = this;
352-
ldp.stat(filename, function(err, stats) {
355+
var filename = utils.uriToFilename(resourcePath, ldp.root);
356+
ldp.stat(filename, function (err, stats) {
353357
// File does not exist
354358
if (err) {
355359
return callback(new HttpError({
356360
status: err.status,
357-
message:"Can't read file: " + err.message
361+
message: "Can't read file: " + err.message
358362
}));
359363
}
360364

@@ -393,12 +397,13 @@ LDP.prototype.get = function(filename, uri, includeBody, callback) {
393397
});
394398
};
395399

396-
LDP.prototype.delete = function(filename, callback) {
400+
LDP.prototype.delete = function(resourcePath, callback) {
397401
var ldp = this;
402+
var filename = utils.uriToFilename(resourcePath, ldp.root);
398403
ldp.stat(filename, function(err, stats) {
399404
if (err) {
400405
return callback(new HttpError({
401-
status:404,
406+
status: 404,
402407
message: "Can't find file: " + err
403408
}));
404409
}

0 commit comments

Comments
 (0)