@@ -9,6 +9,8 @@ var async = require('async')
99var parallel = async . parallel
1010var waterfall = async . waterfall
1111var debug = require ( './debug' ) . idp
12+ var express = require ( 'express' )
13+ var bodyParser = require ( 'body-parser' )
1214
1315function 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
3335IdentityProvider . 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) {
4244IdentityProvider . 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) {
5153IdentityProvider . 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
8481function 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}
0 commit comments