Skip to content

Commit ff44551

Browse files
megothrubensworks
authored andcommitted
Work in progress - want to rebase
1 parent 0b40e61 commit ff44551

File tree

5 files changed

+88
-43
lines changed

5 files changed

+88
-43
lines changed

lib/acl-checker.js

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ const PermissionSet = require('solid-permissions').PermissionSet
44
const rdf = require('rdflib')
55
const debug = require('./debug').ACL
66
const HTTPError = require('./http-error')
7+
const aclCheck = require('acl-check')
78

89
const DEFAULT_ACL_SUFFIX = '.acl'
10+
const ACL = rdf.Namespace('http://www.w3.org/ns/auth/acl#')
911

1012
// An ACLChecker exposes the permissions on a specific resource
1113
class ACLChecker {
@@ -22,34 +24,77 @@ class ACLChecker {
2224

2325
// Returns a fulfilled promise when the user can access the resource
2426
// in the given mode, or rejects with an HTTP error otherwise
25-
can (user, mode) {
27+
async can (user, mode) {
2628
// If this is an ACL, Control mode must be present for any operations
2729
if (this.isAcl(this.resource)) {
2830
mode = 'Control'
2931
}
3032

3133
// Obtain the permission set for the resource
32-
if (!this._permissionSet) {
33-
this._permissionSet = this.getNearestACL()
34-
.then(acl => this.getPermissionSet(acl))
35-
}
34+
// this.acl.graph
35+
// this.resource
36+
// this.acl.isContainer ? this.resource : null
37+
// this.acl.acl
38+
// user
39+
// ACL(mode)
40+
// this.origin
41+
// this.trustedOrigins
42+
43+
// console.log('ACL', this.origin, this.trustedOrigins)
44+
// console.log(aclCheck.accessDenied)
45+
// if (!this._permissionSet) {
46+
// this._permissionSet = this.getNearestACL()
47+
// .then(acl => this.getPermissionSet(acl))
48+
// }
49+
50+
// aclCheck.checkAccess(acl.graph, this.resource)
3651

3752
// Check the resource's permissions
38-
return this._permissionSet
39-
.then(acls => this.checkAccess(acls, user, mode))
40-
.catch(() => {
41-
if (!user) {
42-
throw new HTTPError(401, `Access to ${this.resource} requires authorization`)
43-
} else {
44-
throw new HTTPError(403, `Access to ${this.resource} denied for ${user}`)
45-
}
46-
})
53+
this.acl = this.acl || await this.getNearestACL()
54+
const resource = rdf.sym(this.resource)
55+
// const directory = this.acl.isContainer ? this.resource : null
56+
const directory = this.acl.isContainer ? rdf.sym(ACLChecker.getDirectory(this.acl.acl)) : null
57+
// console.log(ACLChecker.getDirectory(this.acl.acl))
58+
const aclFile = rdf.sym(this.acl.acl)
59+
// const agent = rdf.sym(user)
60+
const agent = user ? rdf.sym(user) : null
61+
// console.log('ACL agent', agent)
62+
// console.log('ACL FILE', this.resource, this.acl.acl)
63+
const modes = [ACL(mode)]
64+
const origin = this.origin ? rdf.sym(this.origin) : null
65+
const trustedOrigins = this.trustedOrigins ? this.trustedOrigins.map(trustedOrigin => rdf.sym(trustedOrigin)) : null
66+
const accessDenied = aclCheck.accessDenied(this.acl.graph, resource, directory, aclFile, agent, modes, origin, trustedOrigins)
67+
console.log('ACCESS DENIED', accessDenied, '\n\n')
68+
if (accessDenied && user) {
69+
throw new HTTPError(403, `Access to ${this.resource} denied for ${user}`)
70+
} else if (accessDenied) {
71+
throw new HTTPError(401, `Access to ${this.resource} requires authorization`)
72+
}
73+
return Promise.resolve(true)
74+
}
75+
76+
// return Promise.resolve(true)
77+
// return this._permissionSet
78+
// .then(acls => this.checkAccess(acls, user, mode))
79+
// .catch(() => {
80+
// if (!user) {
81+
// throw new HTTPError(401, `Access to ${this.resource} requires authorization`)
82+
// } else {
83+
// throw new HTTPError(403, `Access to ${this.resource} denied for ${user}`)
84+
// }
85+
// })
86+
87+
static getDirectory (aclFile) {
88+
const parts = aclFile.split('/')
89+
parts.pop()
90+
return `${parts.join('/')}/`
4791
}
4892

49-
// Gets the ACL that applies to the resource
50-
getNearestACL () {
93+
// Gets the ACL that applies to the resource
94+
async getNearestACL () {
5195
const { resource } = this
5296
let isContainer = false
97+
// let directory = null
5398
// Create a cascade of reject handlers (one for each possible ACL)
5499
const nearestACL = this.getPossibleACLs().reduce((prevACL, acl) => {
55100
return prevACL.catch(() => new Promise((resolve, reject) => {
@@ -68,11 +113,11 @@ class ACLChecker {
68113
return nearestACL.catch(e => { throw new Error('No ACL resource found') })
69114
}
70115

71-
// Gets all possible ACL paths that apply to the resource
116+
// Gets all possible ACL paths that apply to the resource
72117
getPossibleACLs () {
73118
// Obtain the resource URI and the length of its base
74119
let { resource: uri, suffix } = this
75-
const [ { length: base } ] = uri.match(/^[^:]+:\/*[^/]+/)
120+
const [{ length: base }] = uri.match(/^[^:]+:\/*[^/]+/)
76121

77122
// If the URI points to a file, append the file's ACL
78123
const possibleAcls = []
@@ -87,7 +132,7 @@ class ACLChecker {
87132
return possibleAcls
88133
}
89134

90-
// Tests whether the permissions allow a given operation
135+
// Tests whether the permissions allow a given operation
91136
checkAccess (permissionSet, user, mode) {
92137
const options = { fetchGraph: this.fetchGraph }
93138
return permissionSet.checkAccess(this.resource, user, mode, options)
@@ -100,7 +145,7 @@ class ACLChecker {
100145
})
101146
}
102147

103-
// Gets the permission set for the given ACL
148+
// Gets the permission set for the given ACL
104149
getPermissionSet ({ acl, graph, isContainer }) {
105150
if (!graph || graph.length === 0) {
106151
debug('ACL ' + acl + ' is empty')

lib/api/authn/webid-oidc.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,7 @@ function middleware (oidc) {
8787
// Static assets related to authentication
8888
const authAssets = [
8989
['/.well-known/solid/login/', '../static/popup-redirect.html', false],
90-
['/common/', 'solid-auth-client/dist-popup/popup.html'],
91-
['/common/js/', 'solid-auth-client/dist-lib/solid-auth-client.bundle.js'],
92-
['/common/js/', 'solid-auth-client/dist-lib/solid-auth-client.bundle.js.map']
90+
['/common/', 'solid-auth-client/dist-popup/popup.html']
9391
]
9492
authAssets.map(args => routeResolvedFile(router, ...args))
9593

lib/create-app.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ function createApp (argv = {}) {
6464
app.use('/common', express.static(path.join(__dirname, '../common')))
6565
routeResolvedFile(app, '/common/js/', 'mashlib/dist/mashlib.min.js')
6666
routeResolvedFile(app, '/common/js/', 'mashlib/dist/mashlib.min.js.map')
67+
routeResolvedFile(app, '/common/js/', 'solid-auth-client/dist-lib/solid-auth-client.bundle.js.map')
68+
routeResolvedFile(app, '/common/js/', 'solid-auth-client/dist-lib/solid-auth-client.bundle.js.map')
6769
app.use('/.well-known', express.static(path.join(__dirname, '../common/well-known')))
6870

6971
// Serve bootstrap from it's node_module directory

lib/handlers/allow.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function allow (mode) {
3636
// Obtain and store the ACL of the requested resource
3737
req.acl = new ACL(rootUrl + reqPath, {
3838
origin: req.get('origin'),
39-
host: req.protocol + '://' + req.get('host'),
39+
host: req.get('host'),
4040
fetch: fetchFromLdp(ldp.resourceMapper, ldp),
4141
fetchGraph: (uri, options) => {
4242
// first try loading from local fs

test/unit/acl-checker-test.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,27 @@ chai.use(require('chai-as-promised'))
77
const options = { fetch: (url, callback) => {} }
88

99
describe('ACLChecker unit test', () => {
10-
let acl
10+
// let acl
1111

12-
beforeEach(() => {
13-
acl = new ACLChecker('http://ex.com/.acl', options)
14-
})
12+
// beforeEach(() => {
13+
// acl = new ACLChecker('http://ex.com/.acl', options)
14+
// })
1515

16-
describe('checkAccess', () => {
17-
it('should callback with null on grant success', () => {
18-
let acls = { checkAccess: () => Promise.resolve(true) }
19-
return expect(acl.checkAccess(acls)).to.eventually.be.true
20-
})
21-
it('should callback with error on grant failure', () => {
22-
let acls = { checkAccess: () => Promise.resolve(false) }
23-
return expect(acl.checkAccess(acls))
24-
.to.be.rejectedWith('ACL file found but no matching policy found')
25-
})
26-
it('should callback with error on grant error', () => {
27-
let acls = { checkAccess: () => Promise.reject(new Error('my error')) }
28-
return expect(acl.checkAccess(acls)).to.be.rejectedWith('my error')
29-
})
30-
})
16+
// describe('checkAccess', () => {
17+
// it('should callback with null on grant success', () => {
18+
// let acls = { checkAccess: () => Promise.resolve(true) }
19+
// return expect(acl.checkAccess(acls)).to.eventually.be.true
20+
// })
21+
// it('should callback with error on grant failure', () => {
22+
// let acls = { checkAccess: () => Promise.resolve(false) }
23+
// return expect(acl.checkAccess(acls))
24+
// .to.be.rejectedWith('ACL file found but no matching policy found')
25+
// })
26+
// it('should callback with error on grant error', () => {
27+
// let acls = { checkAccess: () => Promise.reject(new Error('my error')) }
28+
// return expect(acl.checkAccess(acls)).to.be.rejectedWith('my error')
29+
// })
30+
// })
3131

3232
describe('getPossibleACLs', () => {
3333
it('returns all possible ACLs of the root', () => {

0 commit comments

Comments
 (0)