Skip to content

Commit f26cfc0

Browse files
author
nicola
committed
add GET and subdomain support
1 parent ad1856d commit f26cfc0

3 files changed

Lines changed: 38 additions & 16 deletions

File tree

‎lib/create-app.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var LDP = require('./ldp');
99
var LdpMiddleware = require('./ldp-middleware');
1010
var proxy = require('./handlers/proxy')
1111
var IdentityProvider = require('./identity-provider');
12+
var vhost = require('vhost');
1213
var corsSettings = cors({
1314
methods: [
1415
'OPTIONS', 'HEAD', 'GET',
@@ -52,10 +53,11 @@ function createApp (argv) {
5253
if (ldp.idp) {
5354
var idp = IdentityProvider({
5455
store: ldp,
55-
suffixAcl: ldp.suffixAcl,
56-
host: ldp.host // TODO host must be hard-coded
56+
suffixAcl: ldp.suffixAcl
5757
})
5858
app.use('/accounts', idp.middleware(corsSettings));
59+
app.use('/', corsSettings, idp.get.bind(idp))
60+
app.use(vhost('*', LdpMiddleware(corsSettings)));
5961
}
6062

6163
// Setting up routes

‎lib/handlers/get.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function get (req, res, next) {
4949
res.header('Updates-Via', utils.uriBase(req));
5050
}
5151

52-
debug(req.method + ' requesting -- ' + req.originalUrl);
52+
debug(req.method + ' requesting -- ' + req.originalUrl + ' ' + req.hostname);
5353

5454
ldp.get(req.hostname, req.path, baseUri, includeBody, possibleRDFType,
5555
function (err, stream, contentType, container) {

‎lib/identity-provider.js‎

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var express = require('express')
1313
var bodyParser = require('body-parser')
1414
var errorHandler = require('./handlers/error');
1515
var url = require('url')
16+
var uriBase = require('./utils').uriBase
1617

1718
function 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) {
5858
IdentityProvider.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
278295
IdentityProvider.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
329348
IdentityProvider.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

Comments
 (0)