@@ -13,6 +13,7 @@ var express = require('express')
1313var bodyParser = require ( 'body-parser' )
1414var errorHandler = require ( './handlers/error' ) ;
1515var url = require ( 'url' )
16+ var uriBase = require ( './utils' ) . uriBase
1617
1718function defaultBuildURI ( account , host , port ) {
1819 var hostAndPort = ( host || 'localhost' ) + ( port || '' )
@@ -28,7 +29,6 @@ function IdentityProvider (options) {
2829 this . store = options . store
2930 this . pathCard = options . pathCard || 'profile/card'
3031 this . suffixURI = options . suffixURI || '#me'
31- this . host = options . host
3232 this . buildURI = options . buildURI || defaultBuildURI
3333 this . suffixAcl = options . suffixAcl
3434}
@@ -58,7 +58,7 @@ IdentityProvider.prototype.putGraph = function (uri, graph) {
5858IdentityProvider . prototype . create = function ( options , cert , callback ) {
5959 var self = this
6060
61- options . url = options . url || self . buildURI ( options . username , self . host )
61+ options . url = options . url || self . buildURI ( options . username , options . host )
6262 options . card = options . url + self . pathCard
6363 options . agent = options . card + self . suffixURI
6464 options . suffixAcl = self . suffixAcl
@@ -69,8 +69,9 @@ IdentityProvider.prototype.create = function (options, cert, callback) {
6969
7070 // TODO create workspaces
7171 // remove port from host
72- var host = self . host . split ( ':' ) [ 0 ]
73- var subdomain = options . username + '.' + host
72+
73+ var hostname = url . parse ( options . host ) . hostname
74+ var subdomain = options . username + '.' + hostname
7475 self . store . exists ( subdomain , '/' , function ( err ) {
7576 // if page exists
7677 if ( ! err || err . status !== 404 ) {
@@ -273,27 +274,44 @@ function createRootAcl (options) {
273274}
274275
275276// TODO create workspaces
277+ IdentityProvider . prototype . get = function ( req , res , next ) {
278+ if ( req . path !== '/' ) {
279+ return next ( )
280+ }
281+ this . store . exists ( req . hostname , '/' , function ( err ) {
282+ if ( err && err . status === 404 ) {
283+ // TODO maybe a personalized page
284+ // or a redirect
285+ debug ( 'Account on ' + req . hostname + ' is available from ' + req . originalUrl )
286+ return res . sendStatus ( 404 )
287+ } else {
288+ debug ( 'Account on ' + req . hostname + ' is not available ' + req . originalUrl )
289+ return next ( )
290+ }
291+ } )
292+ }
276293
277294// Handle POST requests on account creation
278295IdentityProvider . prototype . post = function ( req , res , next ) {
279296 var self = this
297+ var options = req . body
298+ options . host = uriBase ( req )
299+ debug ( 'Create account with settings ' , options )
280300
281- debug ( 'Create account with settings ' , req . body )
282-
283- if ( ! req . body || ! req . body . username ) {
301+ if ( ! options || ! options . username ) {
284302 var err = new Error ( 'You must enter an account name!' )
285303 err . status = 406
286304 return next ( err )
287305 }
288306
289- var agent = self . agent ( req . body )
307+ var agent = self . agent ( options )
290308
291309 var cert = null
292310 waterfall ( [
293311 function ( callback ) {
294- if ( req . body [ 'spkac' ] && req . body [ 'spkac' ] . length > 0 ) {
312+ if ( options [ 'spkac' ] && options [ 'spkac' ] . length > 0 ) {
295313 webid ( 'tls' ) . generate ( {
296- spkac : req . body [ 'spkac' ] ,
314+ spkac : options [ 'spkac' ] ,
297315 agent : agent // TODO generate agent
298316 } , callback )
299317 } else {
@@ -302,7 +320,8 @@ IdentityProvider.prototype.post = function (req, res, next) {
302320 } ,
303321 function ( newSpkac , callback ) {
304322 cert = newSpkac
305- self . create ( req . body , cert , callback )
323+
324+ self . create ( options , cert , callback )
306325 }
307326 ] , function ( err ) {
308327 if ( err ) {
@@ -327,12 +346,13 @@ IdentityProvider.prototype.post = function (req, res, next) {
327346
328347// Middleware (or Router) to serve the IdentityProvider
329348IdentityProvider . prototype . middleware = function ( corsSettings ) {
349+ var self = this
330350 var router = express . Router ( '/' )
331-
332351 if ( corsSettings ) router . use ( corsSettings )
333352 router . post ( '/' , bodyParser . urlencoded ( { extended : false } ) , this . post . bind ( this ) )
334353 router . all ( '/*' , function ( req , res ) {
335- res . redirect ( 'https://solid.github.io/solid-signup/?domain=https://' + this . host + '/accounts' )
354+ var host = uriBase ( req )
355+ res . redirect ( 'https://solid.github.io/solid-signup/?domain=' + host + '/accounts' )
336356 } )
337357 router . use ( errorHandler . handler )
338358 return router
0 commit comments