Skip to content

Commit e846e6b

Browse files
author
Andrei
committed
Merge pull request nodeSolidServer#328 from solid/drop-create-admin
Drop create admin
2 parents 3418d6a + ef0214a commit e846e6b

7 files changed

Lines changed: 95 additions & 40 deletions

File tree

.gitignore

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,3 @@ node_modules/
33
*.swp
44
.tern-port
55
npm-debug.log
6-
.acl
7-
profile/
8-
accounts/
9-
settings/
10-
temp/

README.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,6 @@ $ ldnode --port 8443 --ssl-key path/to/ssl-key.pem --ssl-cert path/to/ssl-cert.p
3737
# Solid server (ldnode v0.2.24) running on https://localhost:8443/
3838
```
3939

40-
First time user? If you have never run `ldnode` before, let's get you a WebID to access your server.
41-
```bash
42-
$ ldnode --port 8443 --ssl-key path/to/ssl-key.pem --ssl-cert path/to/ssl-cert.pem --create-admin
43-
# Action required: Create your admin account on https://localhost:8080/
44-
# When done, stop your server (<ctrl>+c) and restart without "--create-admin"
45-
```
46-
4740
If you want to run `ldnode` on a particular folder (different from the one you are in, e.g. `path/to/folder`):
4841
```bash
4942
$ ldnode --root path/to/folder --port 8443 --ssl-key path/to/ssl-key.pem --ssl-cert path/to/ssl-cert.pem
@@ -104,7 +97,6 @@ Options:
10497
--ssl-cert Path to the SSL certificate key in PEM format
10598
--allow-signup Allow users to register their WebID on subdomains
10699
107-
--create-admin Allow a user to set up their initial identity in single-user mode
108100
--no-live Disable live support through WebSockets
109101
--default-app URI to use as a default app for resources (default: https://linkeddata.github.io/warp/#/list/)
110102
--proxy Use a proxy on example.tld/proxyPath

bin/ldnode.js

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ var argv = require('nomnom')
2929
full: 'webid',
3030
flag: true
3131
})
32+
.option('owner', {
33+
help: 'Set the owner of the storage'
34+
})
3235
.option('key', {
3336
help: 'Path to the SSL private key in PEM format',
3437
full: 'ssl-key'
@@ -42,11 +45,6 @@ var argv = require('nomnom')
4245
full: 'allow-signup',
4346
flag: true
4447
})
45-
.option('createAdmin', {
46-
full: 'create-admin',
47-
flag: true,
48-
help: 'Allow a user to set up their initial identity in single-user mode'
49-
})
5048
.option('noLive', {
5149
full: 'no-live',
5250
help: 'Disable live support through WebSockets',
@@ -134,6 +132,35 @@ function bin (argv) {
134132
})
135133
}
136134

135+
if (argv.owner) {
136+
var rootPath = argv.root
137+
if (!rootPath) {
138+
rootPath = process.cwd()
139+
}
140+
if (!(rootPath.endsWith('/'))) {
141+
rootPath += '/'
142+
}
143+
rootPath += (argv.suffixAcl || '.acl')
144+
145+
var defaultAcl = `@prefix n0: <http://www.w3.org/ns/auth/acl#>.
146+
@prefix n2: <http://xmlns.com/foaf/0.1/>.
147+
148+
<#owner>
149+
a n0:Authorization;
150+
n0:accessTo <./>;
151+
n0:agent <${argv.owner}>;
152+
n0:defaultForNew <./>;
153+
n0:mode n0:Control, n0:Read, n0:Write.
154+
<#everyone>
155+
a n0:Authorization;
156+
n0: n2:Agent;
157+
n0:accessTo <./>;
158+
n0:defaultForNew <./>;
159+
n0:mode n0:Read.' > .acl`
160+
161+
fs.writeFileSync(rootPath, defaultAcl)
162+
}
163+
137164
// Finally starting ldnode
138165
var ldnode = require('../')
139166
var app
@@ -154,13 +181,8 @@ function bin (argv) {
154181
}
155182
app.listen(argv.port, function () {
156183
fs.readFile(path.resolve(__dirname, '../package.json'), 'utf-8', function (_, file) {
157-
if (argv.createAdmin) {
158-
console.log('Action required: Create your admin account on \u001b[4mhttps://localhost:' + argv.port + '/\u001b[0m')
159-
console.log('When done, stop your server (<ctrl>+c) and restart without "--create-admin"')
160-
} else {
161-
console.log('Solid server (ldnode v' + JSON.parse(file).version + ') running on \u001b[4mhttps://localhost:' + argv.port + '/\u001b[0m')
162-
console.log('Press <ctrl>+c to stop')
163-
}
184+
console.log('Solid server (ldnode v' + JSON.parse(file).version + ') running on \u001b[4mhttps://localhost:' + argv.port + '/\u001b[0m')
185+
console.log('Press <ctrl>+c to stop')
164186
})
165187
})
166188
}

lib/create-app.js

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,21 @@ function createApp (argv) {
2424
var ldp = new LDP(argv)
2525
var app = express()
2626

27+
// check if we have master ACL or not
28+
var masterAcl
29+
var checkMasterAcl = function (req, callback) {
30+
if (masterAcl) {
31+
return callback(true)
32+
}
33+
34+
ldp.exists(req.hostname, '/' + ldp.suffixAcl, function (err) {
35+
if (!err) {
36+
masterAcl = true
37+
}
38+
callback(!err)
39+
})
40+
}
41+
2742
// Setting options as local variable
2843
app.locals.ldp = ldp
2944

@@ -63,29 +78,49 @@ function createApp (argv) {
6378
}
6479

6580
// Adding Multi-user support
66-
if (ldp.idp || ldp.createAdmin) {
81+
if (ldp.webid) {
6782
var idp = IdentityProvider({
6883
store: ldp,
6984
suffixAcl: ldp.suffixAcl,
70-
overwrite: ldp.createAdmin,
7185
settings: 'settings',
7286
inbox: 'inbox'
7387
})
74-
app.use('/accounts', idp.middleware(corsSettings))
88+
var needsOverwrite = function (req, res, next) {
89+
checkMasterAcl(req, function (found) {
90+
if (!found) {
91+
// this allows IdentityProvider to overwrite root acls
92+
idp.middleware(corsSettings, true)(req, res, next)
93+
} else if (found && ldp.idp) {
94+
idp.middleware(corsSettings)(req, res, next)
95+
} else {
96+
next()
97+
}
98+
})
99+
}
100+
app.use('/accounts', needsOverwrite)
75101
app.use('/', corsSettings, idp.get.bind(idp))
76102
}
77103

78104
if (ldp.idp) {
79105
app.use(vhost('*', LdpMiddleware(corsSettings)))
80106
}
81107

82-
if (ldp.createAdmin) {
83-
app.get('/', function (req, res) {
84-
res.set('Content-Type', 'text/html')
85-
var signup = path.join(__dirname, '../static/signup.html')
86-
res.sendFile(signup)
108+
app.get('/', function (req, res, next) {
109+
// Do not bother showing html page can't be read
110+
if (!req.accepts('text/html') || !ldp.webid) {
111+
return next()
112+
}
113+
114+
checkMasterAcl(req, function (found) {
115+
if (!found) {
116+
res.set('Content-Type', 'text/html')
117+
var signup = path.join(__dirname, '../static/signup.html')
118+
res.sendFile(signup)
119+
} else {
120+
next()
121+
}
87122
})
88-
}
123+
})
89124
app.use('/', LdpMiddleware(corsSettings))
90125

91126
return app

lib/identity-provider.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ function IdentityProvider (options) {
3939
this.buildURI = options.buildURI || defaultBuildURI
4040
this.suffixAcl = options.suffixAcl
4141
this.defaultContainers = options.defaultContainers || defaultContainers
42-
this.overwrite = options.overwrite
4342
this.inbox = options.inbox
4443
this.settings = options.settings
4544
}
@@ -113,7 +112,7 @@ IdentityProvider.prototype.create = function (options, cert, callback) {
113112
var subdomain = options.host.split(':')[0]
114113
self.store.exists(subdomain, '/', function (err) {
115114
// if page exists, cannot create account
116-
if (!self.overwrite && (!err || err.status !== 404)) {
115+
if (!options.firstUser && (!err || err.status !== 404)) {
117116
debug('Cannot create ' + subdomain + ', it already exists')
118117
var error = new Error('Account already exists')
119118
error.status = 406
@@ -511,6 +510,7 @@ IdentityProvider.prototype.post = function (req, res, next) {
511510
var self = this
512511
var options = req.body
513512
options.host = req.get('host')
513+
options.firstUser = res.locals.firstUser
514514
var agent = self.agent(options)
515515
var spkac = null
516516
var cert = null
@@ -558,14 +558,15 @@ IdentityProvider.prototype.post = function (req, res, next) {
558558
}
559559

560560
// Middleware (or Router) to serve the IdentityProvider
561-
IdentityProvider.prototype.middleware = function (corsSettings) {
561+
IdentityProvider.prototype.middleware = function (corsSettings, firstUser) {
562562
var router = express.Router('/')
563563
var parser = bodyParser.urlencoded({ extended: false })
564564

565565
if (corsSettings) {
566566
router.use(corsSettings)
567567
}
568-
router.post('/new', parser, this.post.bind(this))
568+
569+
router.post('/new', parser, setFirstUser(firstUser), this.post.bind(this))
569570
router.post('/cert', parser, this.newCert.bind(this))
570571
router.all('/*', function (req, res) {
571572
var host = uriAbs(req)
@@ -576,3 +577,10 @@ IdentityProvider.prototype.middleware = function (corsSettings) {
576577

577578
return router
578579
}
580+
581+
function setFirstUser (isFirstUser) {
582+
return function (req, res, next) {
583+
res.locals.firstUser = isFirstUser
584+
next()
585+
}
586+
}

static/signup.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ <h2>Finish by issuing credentials in the form of a certificate</h2>
9797
document.getElementById('cert').style.display = 'none'
9898
var done = document.createElement('div')
9999
done.innerHTML = '<h2>You\'re all set!</h2>'
100-
done.innerHTML += '<p>Please restart your server without the <strong>--create-admin</strong> parameter.</p>'
101-
done.innerHTML += '<p>If an error occured and a certificate was not installed, please reload this page and start again.</p>'
100+
done.innerHTML += '<p>as soon as you will reset the page, you will be logged in!</p>'
102101
document.querySelector('body').appendChild(done)
103102
}
104103

test/resources/acl/owner-only/.acl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<#0>
2+
<http://www.w3.org/ns/auth/acl#defaultForNew> <./> ;
3+
<http://www.w3.org/ns/auth/acl#agent> <https://user1.databox.me/profile/card#me> ;
4+
<http://www.w3.org/ns/auth/acl#mode> <http://www.w3.org/ns/auth/acl#Write>, <http://www.w3.org/ns/auth/acl#Control>.

0 commit comments

Comments
 (0)