Skip to content

Commit b1af124

Browse files
author
nicola
committed
adding first test passing the creating of a webid/acls
1 parent 9cfe646 commit b1af124

6 files changed

Lines changed: 147 additions & 27 deletions

File tree

‎lib/create-app.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var debug = require('./debug');
88
var LDP = require('./ldp');
99
var LdpMiddleware = require('./ldp-middleware');
1010
var proxy = require('./handlers/proxy')
11+
var IdentityProvider = require('./identity-provider');
1112
var corsSettings = cors({
1213
methods: [
1314
'OPTIONS', 'HEAD', 'GET',
@@ -38,6 +39,20 @@ function createApp (argv) {
3839
proxy(app, ldp.proxy);
3940
}
4041

42+
app.use('/test', function(req, res) {
43+
res.send(200, 'HELLO')
44+
})
45+
46+
// Adding Multi-user support
47+
if (ldp.idp) {
48+
var idp = IdentityProvider({
49+
store: ldp,
50+
suffixAcl: ldp.suffixAcl,
51+
host: ldp.host // TODO host must be hard-coded
52+
})
53+
app.use('/accounts', idp.middleware(corsSettings));
54+
}
55+
4156
// Setting up routes
4257
app.use('/', LdpMiddleware(corsSettings));
4358

‎lib/create-server.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ function createServer(argv) {
5959
requestCert: true
6060
};
6161

62-
debug.settings("Certificate: " + credentials.cert);
6362
server = https.createServer(credentials, app);
6463
}
6564

‎lib/identity-provider.js‎

Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ var async = require('async')
99
var parallel = async.parallel
1010
var waterfall = async.waterfall
1111
var debug = require('./debug').idp
12+
var express = require('express')
13+
var bodyParser = require('body-parser')
1214

1315
function defaultBuildURI (account, host, port) {
1416
var hostAndPort = (host || 'localhost') + (port || '')
@@ -26,13 +28,13 @@ function IdentityProvider (options) {
2628
this.suffixURI = options.suffixURI || '#me'
2729
this.host = options.host
2830
this.buildURI = options.buildURI || defaultBuildURI
29-
debug('Setting up identity provider')
31+
this.suffixAcl = options.suffixAcl
3032
}
3133

3234
// Generate the future webid from the options and the IdentityProvider Settings
3335
IdentityProvider.prototype.agent = function (options) {
3436
options = options || {}
35-
var url = options.url || this.buildURI(options.account, this.host)
37+
var url = options.url || this.buildURI(options.username, this.host)
3638
var card = url + this.pathCard
3739
var agent = card + this.suffixURI
3840
return agent
@@ -42,7 +44,7 @@ IdentityProvider.prototype.agent = function (options) {
4244
IdentityProvider.prototype.putGraph = function (uri, graph) {
4345
var self = this
4446
return function (callback) {
45-
var content = graph.serialize(uri, 'text/turtle')
47+
var content = $rdf.serialize(content, graph, uri, 'text/turtle')
4648
return self.store.put(uri, content, callback)
4749
}
4850
}
@@ -51,15 +53,10 @@ IdentityProvider.prototype.putGraph = function (uri, graph) {
5153
IdentityProvider.prototype.create = function (options, cert, callback) {
5254
var self = this
5355

54-
if (!options || !options.account) {
55-
var err = new Error('You must enter an account name!')
56-
err.statusCode = 406 // TODO
57-
return callback(err)
58-
}
59-
60-
options.url = options.url || self.buildURI(options.account, self.host)
56+
options.url = options.url || self.buildURI(options.username, self.host)
6157
options.card = options.url + self.pathCard
6258
options.agent = options.card + self.suffixURI
59+
options.suffixAcl = self.suffixAcl
6360

6461
var card = createCard(options, cert)
6562
var cardAcl = createCardAcl(options)
@@ -82,12 +79,40 @@ IdentityProvider.prototype.create = function (options, cert, callback) {
8279

8380
// Generates a WebID card and add the key if the cert is passed
8481
function createCard (options, cert) {
85-
var graph = webid.generate({
86-
// TODO list all the attributes
87-
// if any
88-
})
82+
var graph = $rdf.graph()
83+
graph.add(
84+
sym(options.card),
85+
sym('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
86+
sym('http://xmlns.com/foaf/0.1/PersonalProfileDocument'))
87+
graph.add(
88+
sym(options.card),
89+
sym('http://xmlns.com/foaf/0.1/maker'),
90+
sym(options.agent))
91+
graph.add(
92+
sym(options.card),
93+
sym('http://xmlns.com/foaf/0.1/primaryTopic'),
94+
sym(options.agent))
95+
graph.add(
96+
sym(options.agent),
97+
sym('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
98+
sym('http://xmlns.com/foaf/0.1/Person'))
99+
100+
if (options.name && options.name.length > 0) {
101+
graph.add(
102+
sym(options.agent),
103+
sym('http://xmlns.com/foaf/0.1/name'),
104+
lit(options.name))
105+
}
106+
graph.add(
107+
sym(options.agent),
108+
sym('http://www.w3.org/ns/pim/space#storage'),
109+
sym(options.url))
110+
111+
// TODO add workspace with inbox, timeline, preferencesFile..
112+
// See: https://github.com/linkeddata/gold/blob/master/webid.go
89113

90114
if (cert) {
115+
console.log("CERT", cert)
91116
addKey(graph, options.agent, cert)
92117
}
93118

@@ -239,6 +264,13 @@ IdentityProvider.prototype.post = function (req, res, next) {
239264
var self = this
240265

241266
debug('Create account with settings ', req.body)
267+
268+
if (!req.body || !req.body.username) {
269+
var err = new Error('You must enter an account name!')
270+
err.statusCode = 406
271+
return next(err)
272+
}
273+
242274
var agent = self.agent(req.body)
243275

244276
waterfall([
@@ -249,7 +281,7 @@ IdentityProvider.prototype.post = function (req, res, next) {
249281
agent: agent // TODO generate agent
250282
}, callback)
251283
} else {
252-
return callback()
284+
return callback(null, false)
253285
}
254286
},
255287
function (cert, callback) {
@@ -261,22 +293,21 @@ IdentityProvider.prototype.post = function (req, res, next) {
261293
}
262294

263295
// Set the cookies on success
264-
res.session.agent = agent
296+
req.session.agent = agent
265297
res.set('User', agent)
266298

267-
next()
299+
res.send(201)
268300
})
269301
}
270302

271303
// Middleware (or Router) to serve the IdentityProvider
272-
IdentityProvider.prototype.middleware = function (req, res, next) {
273-
var self = this
304+
IdentityProvider.prototype.middleware = function (corsSettings) {
305+
var router = express.Router('/')
274306

275-
if (req.method === 'POST') {
276-
self.post(req, res, next)
277-
} else {
278-
var err = new Error('Can only do POST')
279-
err.statusCode = 406
280-
next(err)
281-
}
307+
if (corsSettings) router.use(corsSettings)
308+
router.post('/', bodyParser.urlencoded({ extended: false }), this.post.bind(this))
309+
router.all('/*', function (req, res) {
310+
res.redirect('https://solid.github.io/solid-signup/?domain=https://' + this.host + '/accounts')
311+
})
312+
return router
282313
}

‎lib/ldp.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ function LDP(argv) {
2121
// From input
2222
ldp.cache = argv.cache;
2323
ldp.live = argv.live;
24+
ldp.host = argv.host; // TODO maybe deprecated
2425
ldp.root = argv.root || process.cwd();
2526
// Add trailing /
2627
if (!(S(ldp.root).endsWith('/'))) {
@@ -58,6 +59,7 @@ function LDP(argv) {
5859
debug.settings("Verbose: " + !!ldp.verbose);
5960
debug.settings("WebID: " + !!ldp.webid);
6061
debug.settings("Live: " + !!ldp.live);
62+
debug.settings("Identity Provider: " + ldp.idp);
6163

6264
return ldp;
6365
}

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"dependencies": {
2727
"acl": "^0.4.7",
2828
"async": "^1.3.0",
29+
"body-parser": "^1.14.2",
2930
"cors": "^2.7.1",
3031
"debug": "^2.2.0",
3132
"express": "^4.13.3",

‎test/identity-provider.js‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
var assert = require('chai').assert
2+
var fs = require('fs')
3+
var $rdf = require('rdflib')
4+
var request = require('request')
5+
var S = require('string')
6+
var supertest = require('supertest')
7+
var async = require('async')
8+
9+
// Helper functions for the FS
10+
var rm = require('./test-utils').rm
11+
var write = require('./test-utils').write
12+
var cp = require('./test-utils').cp
13+
var read = require('./test-utils').read
14+
15+
var ldnode = require('../index')
16+
17+
describe('Identity Provider', function () {
18+
this.timeout(10000)
19+
20+
var address = 'https://localhost:3457'
21+
22+
var ldpHttpsServer
23+
var ldp = ldnode.createServer({
24+
root: __dirname + '/resources',
25+
key: __dirname + '/keys/key.pem',
26+
cert: __dirname + '/keys/cert.pem',
27+
webid: true,
28+
idp: true,
29+
host: 'localhost:3457'
30+
})
31+
32+
var ldpHttpsServer
33+
before(function (done) {
34+
ldpHttpsServer = ldp.listen(3457, done)
35+
})
36+
37+
after(function () {
38+
if (ldpHttpsServer) ldpHttpsServer.close()
39+
})
40+
41+
var server = supertest(address)
42+
var user1 = 'https://user1.databox.me/profile/card#me'
43+
var user2 = 'https://user2.databox.me/profile/card#me'
44+
45+
var userCredentials = {
46+
user1: {
47+
cert: fs.readFileSync(__dirname + '/keys/user1-cert.pem'),
48+
key: fs.readFileSync(__dirname + '/keys/user1-key.pem')
49+
},
50+
user2: {
51+
cert: fs.readFileSync(__dirname + '/keys/user2-cert.pem'),
52+
key: fs.readFileSync(__dirname + '/keys/user2-key.pem')
53+
}
54+
}
55+
56+
it('should redirect to signup on GET /accounts', function (done) {
57+
server.get('/accounts')
58+
.expect(302, done)
59+
})
60+
61+
describe('Creating an account with POST', function () {
62+
it('should return 406 if username is not given', function (done) {
63+
server.post('/accounts')
64+
.expect(406, done)
65+
})
66+
it('should return create WebID if only username is given', function (done) {
67+
server.post('/accounts')
68+
.send('username=nicola')
69+
.expect(201, done)
70+
})
71+
})
72+
})

0 commit comments

Comments
 (0)