|
| 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 | +} |
0 commit comments