Skip to content

Commit 9cfe646

Browse files
author
nicola
committed
bumping to the newer identity provider
1 parent 1d1bf8f commit 9cfe646

1 file changed

Lines changed: 152 additions & 167 deletions

File tree

‎lib/identity-provider.js‎

Lines changed: 152 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -5,162 +5,124 @@ var $rdf = require('rdflib')
55
var uuid = require('uuid')
66
var sym = $rdf.sym
77
var lit = $rdf.lit
8+
var async = require('async')
9+
var parallel = async.parallel
10+
var waterfall = async.waterfall
11+
var debug = require('./debug').idp
812

913
function defaultBuildURI (account, host, port) {
10-
var hostAndPort = host
11-
if (port) {
12-
hostAndPort = hostAndPort + port
13-
}
14-
if (!hostAndPort) hostAndPort = 'localhost'
14+
var hostAndPort = (host || 'localhost') + (port || '')
1515
return 'https://' + account.toLowerCase() + '.' + hostAndPort + '/'
1616
}
1717

18+
// IdentityProvider singleton
1819
function IdentityProvider (options) {
1920
if (!(this instanceof IdentityProvider)) {
2021
return new IdentityProvider(options)
2122
}
23+
options = options || {}
24+
this.store = options.store
25+
this.pathCard = options.pathCard || 'profile/card'
26+
this.suffixURI = options.suffixURI || '#me'
27+
this.host = options.host
28+
this.buildURI = options.buildURI || defaultBuildURI
29+
debug('Setting up identity provider')
30+
}
2231

23-
var self = this
24-
32+
// Generate the future webid from the options and the IdentityProvider Settings
33+
IdentityProvider.prototype.agent = function (options) {
2534
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
35+
var url = options.url || this.buildURI(options.account, this.host)
36+
var card = url + this.pathCard
37+
var agent = card + this.suffixURI
38+
return agent
39+
}
40+
41+
// Store a graph straight into the LDPStore
42+
IdentityProvider.prototype.putGraph = function (uri, graph) {
43+
var self = this
44+
return function (callback) {
45+
var content = graph.serialize(uri, 'text/turtle')
46+
return self.store.put(uri, content, callback)
47+
}
3048
}
3149

32-
IdentityProvider.prototype.create = function (options, callback) {
50+
// Create an identity give the options and the WebID+TLS certificates
51+
IdentityProvider.prototype.create = function (options, cert, callback) {
52+
var self = this
53+
3354
if (!options || !options.account) {
3455
var err = new Error('You must enter an account name!')
3556
err.statusCode = 406 // TODO
3657
return callback(err)
3758
}
3859

39-
var self = this
4060
options.url = options.url || self.buildURI(options.account, self.host)
4161
options.card = options.url + self.pathCard
4262
options.agent = options.card + self.suffixURI
4363

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-
}
64+
var card = createCard(options, cert)
65+
var cardAcl = createCardAcl(options)
66+
var rootAcl = createRootAcl(options)
5967

60-
IdentityProvider.prototype.setupSpace = function (options, callback) {
61-
var self = this
68+
// TODO create workspaces
6269

63-
self.createRootAcl(options, function (err) {
70+
parallel([
71+
self.putGraph(options.card, card),
72+
self.putGraph(options.card + self.suffixAcl, cardAcl),
73+
self.putGraph(options.url + self.suffixAcl, rootAcl)
74+
], function (err) {
6475
if (err) {
6576
err.statusCode = 500
66-
return callback(err)
6777
}
6878

69-
callback()
79+
callback(err)
7080
})
7181
}
7282

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))
83+
// Generates a WebID card and add the key if the cert is passed
84+
function createCard (options, cert) {
85+
var graph = webid.generate({
86+
// TODO list all the attributes
87+
// if any
88+
})
9789

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))
90+
if (cert) {
91+
addKey(graph, options.agent, cert)
10392
}
10493

94+
return graph
95+
}
96+
97+
// Add the WebID+TLS Public Key to the WebID graph
98+
function addKey (graph, agent, cert) {
99+
var card = agent.split('#')[0]
100+
var key = card + '#key' + uuid.v4()
101+
105102
graph.add(
106-
sym(owner),
107-
sym('http://www.w3.org/ns/auth/acl#defaultForNew'),
108-
sym(url))
103+
sym(agent),
104+
sym('http://www.w3.org/ns/auth/cert#key'),
105+
sym(key))
109106
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'))
107+
sym(key),
108+
sym('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
109+
sym('http://www.w3.org/ns/auth/cert#RSAPublicKey'))
113110
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'))
111+
sym(key),
112+
sym('http://www.w3.org/2000/01/rdf-schema#label'),
113+
lit('Created on ' + (new Date()).toString()))
117114
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)
115+
sym(key),
116+
sym('http://www.w3.org/ns/auth/cert#modulus'),
117+
lit(cert.mod, 'http://www.w3.org/2001/XMLSchema#hexBinary')) // TODO add Datatype "http://www.w3.org/2001/XMLSchema#hexBinary"
118+
graph.add(
119+
sym(key),
120+
sym('http://www.w3.org/ns/auth/cert#exponent'),
121+
lit(cert.exponent, 'http://www.w3.org/2001/XMLSchema#int')) // TODO add Datatype "http://www.w3.org/2001/XMLSchema#int"
161122
}
162123

163-
IdentityProvider.prototype.createCardAcl = function (options, callback) {
124+
// Create an ACL for the WebID card
125+
function createCardAcl (options) {
164126
var graph = $rdf.graph()
165127
var url = options.card
166128
var acl = url + options.suffixAcl
@@ -217,80 +179,103 @@ IdentityProvider.prototype.createCardAcl = function (options, callback) {
217179
sym('http://www.w3.org/ns/auth/acl#mode'),
218180
sym('http://www.w3.org/ns/auth/acl#Read'))
219181

220-
var content = graph.serialize(acl, 'text/turtle')
221-
return this.store.put(acl, content, callback)
182+
return graph
183+
}
184+
185+
// Create ACL for the space reserved for the new user
186+
function createRootAcl (options) {
187+
var owner = options.url + options.suffixAcl + '#owner'
188+
var agent = options.agent
189+
190+
var graph = $rdf.graph()
191+
graph.add(
192+
sym(owner),
193+
sym('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
194+
sym('http://www.w3.org/ns/auth/acl#Authorization'))
195+
graph.add(
196+
sym(owner),
197+
sym('http://www.w3.org/ns/auth/acl#accessTo'),
198+
sym(options.url))
199+
graph.add(
200+
sym(owner),
201+
sym('http://www.w3.org/ns/auth/acl#accessTo'),
202+
sym(options.url))
203+
graph.add(
204+
sym(owner),
205+
sym('http://www.w3.org/ns/auth/acl#agent'),
206+
sym(agent))
207+
208+
if (options.email && options.email.length > 0) {
209+
graph.add(
210+
sym(owner),
211+
sym('http://www.w3.org/ns/auth/acl#agent'),
212+
sym('mailto:' + options.email))
213+
}
214+
215+
graph.add(
216+
sym(owner),
217+
sym('http://www.w3.org/ns/auth/acl#defaultForNew'),
218+
sym(options.url))
219+
graph.add(
220+
sym(owner),
221+
sym('http://www.w3.org/ns/auth/acl#mode'),
222+
sym('http://www.w3.org/ns/auth/acl#Read'))
223+
graph.add(
224+
sym(owner),
225+
sym('http://www.w3.org/ns/auth/acl#mode'),
226+
sym('http://www.w3.org/ns/auth/acl#Write'))
227+
graph.add(
228+
sym(owner),
229+
sym('http://www.w3.org/ns/auth/acl#mode'),
230+
sym('http://www.w3.org/ns/auth/acl#Control'))
231+
232+
return graph
222233
}
223234

224235
// TODO create workspaces
225-
// TODO create master acl
226236

237+
// Handle POST requests on account creation
227238
IdentityProvider.prototype.post = function (req, res, next) {
228239
var self = this
229-
self.create(req.body, function (err, agent) {
240+
241+
debug('Create account with settings ', req.body)
242+
var agent = self.agent(req.body)
243+
244+
waterfall([
245+
function (callback) {
246+
if (req.body['spkac'] && req.body['spkac'].length > 0) {
247+
webid('tls').generate({
248+
spkac: req.body['spkac'],
249+
agent: agent // TODO generate agent
250+
}, callback)
251+
} else {
252+
return callback()
253+
}
254+
},
255+
function (cert, callback) {
256+
self.create(req.body, cert, callback)
257+
}
258+
], function (err) {
230259
if (err) {
231260
return next(err)
232261
}
233262

263+
// Set the cookies on success
234264
res.session.agent = agent
235265
res.set('User', agent)
236266

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)
267+
next()
284268
})
285269
}
286270

271+
// Middleware (or Router) to serve the IdentityProvider
287272
IdentityProvider.prototype.middleware = function (req, res, next) {
288273
var self = this
289274

290275
if (req.method === 'POST') {
291276
self.post(req, res, next)
292277
} else {
293-
var err = new Error('Can only do GET or POST')
278+
var err = new Error('Can only do POST')
294279
err.statusCode = 406
295280
next(err)
296281
}

0 commit comments

Comments
 (0)