Skip to content

Commit

Permalink
fix: Update acl authorizer to make write rights imply append rights
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimvh committed Jan 11, 2021
1 parent 998296a commit 61aa2e1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/authorization/WebAclAuthorizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ export class WebAclAuthorizer extends Authorizer {
*/
private checkPermission(agent: Credentials, store: Store, mode: string): void {
const modeString = ACL[this.capitalize(mode) as 'Write' | 'Read' | 'Append' | 'Control'];
const auths = store.getQuads(null, ACL.mode, modeString, null).map((quad: Quad): Term => quad.subject);
const auths = this.getModePermissions(store, modeString);

// Having write permissions implies having append permissions
if (modeString === ACL.Append) {
auths.push(...this.getModePermissions(store, ACL.Write));
}

if (!auths.some((term): boolean => this.hasAccess(agent, term, store))) {
const isLoggedIn = typeof agent.webId === 'string';
if (isLoggedIn) {
Expand All @@ -87,6 +93,15 @@ export class WebAclAuthorizer extends Authorizer {
return `${mode[0].toUpperCase()}${mode.slice(1).toLowerCase()}`;
}

/**
* Returns the identifiers of all authorizations that grant the given mode access for a resource.
* @param store - The store containing the quads of the acl resource.
* @param aclMode - A valid acl mode (ACL.Write/Read/...)
*/
private getModePermissions(store: Store, aclMode: string): Term[] {
return store.getQuads(null, ACL.mode, aclMode, null).map((quad: Quad): Term => quad.subject);
}

/**
* Checks if the given agent has access to the modes specified by the given authorization.
* @param agent - Credentials of agent that needs access.
Expand Down
16 changes: 16 additions & 0 deletions test/unit/authorization/WebAclAuthorizer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,20 @@ describe('A WebAclAuthorizer', (): void => {
await expect(promise).rejects.toThrow('No ACL document found for root container');
await expect(promise).rejects.toThrow(InternalServerError);
});

it('allows an agent to append if they have write access.', async(): Promise<void> => {
credentials.webId = 'http://test.com/user';
identifier.path = 'http://test.com/foo';
permissions = {
read: false,
write: false,
append: true,
};
store.getRepresentation = async(): Promise<Representation> => ({ data: streamifyArray([
quad(nn('auth'), nn(`${acl}agent`), nn(credentials.webId!)),
quad(nn('auth'), nn(`${acl}accessTo`), nn(identifier.path)),
quad(nn('auth'), nn(`${acl}mode`), nn(`${acl}Write`)),
]) } as Representation);
await expect(authorizer.handle({ identifier, permissions, credentials })).resolves.toBeUndefined();
});
});

0 comments on commit 61aa2e1

Please sign in to comment.