Skip to content

Commit 1bc05b1

Browse files
megothkjetilk
authored andcommitted
Fixed so that capability discovery points to valid APIs
Login and logout should be accessible on subdomains Restricting access to API endpoints to top domain
1 parent 63d2b81 commit 1bc05b1

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
lines changed

lib/capability-discovery.js

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,7 @@
33
* @module capability-discovery
44
*/
55
const express = require('express')
6-
const restrictToTopDomain = require('./handlers/restrict-to-top-domain')
7-
8-
const serviceConfigDefaults = {
9-
'api': {
10-
'accounts': {
11-
// 'changePassword': '/api/account/changePassword',
12-
// 'delete': '/api/accounts/delete',
13-
14-
// Create new user (see IdentityProvider.post() in identity-provider.js)
15-
'new': '/api/accounts/new',
16-
'recover': '/api/accounts/recover',
17-
'signin': '/login',
18-
'signout': '/logout',
19-
'validateToken': '/api/accounts/validateToken'
20-
}
21-
}
22-
}
6+
const { URL } = require('url')
237

248
module.exports = capabilityDiscovery
259

@@ -32,7 +16,7 @@ function capabilityDiscovery () {
3216
const router = express.Router('/')
3317

3418
// Advertise the server capability discover endpoint
35-
router.get('/.well-known/solid', restrictToTopDomain, serviceCapabilityDocument(serviceConfigDefaults))
19+
router.get('/.well-known/solid', serviceCapabilityDocument())
3620
return router
3721
}
3822

@@ -44,12 +28,27 @@ function capabilityDiscovery () {
4428
* @param res
4529
* @param next
4630
*/
47-
function serviceCapabilityDocument (serviceConfig) {
31+
function serviceCapabilityDocument () {
4832
return (req, res) => {
49-
// Add the server root url
50-
serviceConfig.root = req.app.locals.ldp.resourceMapper.resolveUrl(req.hostname, req.path)
51-
// Add the 'apps' urls section
52-
serviceConfig.apps = req.app.locals.appUrls
53-
res.json(serviceConfig)
33+
const ldp = req.app.locals.ldp
34+
res.json({
35+
// Add the server root url
36+
root: req.app.locals.ldp.resourceMapper.resolveUrl(req.hostname, req.path),
37+
// Add the 'apps' urls section
38+
apps: req.app.locals.appUrls,
39+
api: {
40+
accounts: {
41+
// 'changePassword': '/api/account/changePassword',
42+
// 'delete': '/api/accounts/delete',
43+
44+
// Create new user (see IdentityProvider.post() in identity-provider.js)
45+
new: new URL('/api/accounts/new', ldp.serverUri),
46+
recover: new URL('/api/accounts/recover', ldp.serverUri),
47+
signin: req.app.locals.ldp.resourceMapper.resolveUrl(req.hostname, '/login'),
48+
signout: req.app.locals.ldp.resourceMapper.resolveUrl(req.hostname, '/logout'),
49+
validateToken: new URL('/api/accounts/validateToken', ldp.serverUri)
50+
}
51+
}
52+
})
5453
}
5554
}

lib/handlers/restrict-to-top-domain.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ module.exports = function (req, res, next) {
88
if (hostname === serverUri) {
99
return next()
1010
}
11-
return next(new HTTPError(403, 'Not allowed to access top-level APIs on accounts'))
11+
const isLoggedIn = !!(req.session && req.session.userId)
12+
return next(new HTTPError(isLoggedIn ? 403 : 401, 'Not allowed to access top-level APIs on accounts'))
1213
}

0 commit comments

Comments
 (0)