-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add WAC-Allow header when required
- Loading branch information
Showing
9 changed files
with
115 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import type { Term } from 'rdf-js'; | ||
import type { HttpResponse } from '../../../server/HttpResponse'; | ||
import { addHeader } from '../../../util/HeaderUtil'; | ||
import { ACL, AUTH } from '../../../util/Vocabularies'; | ||
import type { RepresentationMetadata } from '../../representation/RepresentationMetadata'; | ||
import { MetadataWriter } from './MetadataWriter'; | ||
|
||
/** | ||
* Add the necessary WAC-Allow header values. | ||
* Solid, §10.1: "Servers exposing client’s access privileges on a resource URL MUST advertise | ||
* by including the WAC-Allow HTTP header in the response of HTTP HEAD and GET requests." | ||
* https://solid.github.io/specification/protocol#web-access-control | ||
*/ | ||
export class WacAllowMetadataWriter extends MetadataWriter { | ||
public async handle(input: { response: HttpResponse; metadata: RepresentationMetadata }): Promise<void> { | ||
const userModes = input.metadata.getAll(AUTH.terms.userMode).map(this.aclToPermission); | ||
const publicModes = input.metadata.getAll(AUTH.terms.publicMode).map(this.aclToPermission); | ||
|
||
const headerStrings: string[] = []; | ||
if (userModes.length > 0) { | ||
headerStrings.push(this.createAccessParam('user', userModes)); | ||
} | ||
if (publicModes.length > 0) { | ||
headerStrings.push(this.createAccessParam('public', publicModes)); | ||
} | ||
|
||
// Only add the header if there are permissions to show | ||
if (headerStrings.length > 0) { | ||
addHeader(input.response, 'WAC-Allow', headerStrings.join(',')); | ||
} | ||
} | ||
|
||
private aclToPermission(aclTerm: Term): string { | ||
return aclTerm.value.slice(ACL.namespace.length).toLowerCase(); | ||
} | ||
|
||
private createAccessParam(name: string, modes: string[]): string { | ||
return `${name}="${modes.join(' ')}"`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
test/unit/ldp/http/metadata/WacAllowMetadataWriter.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { createResponse } from 'node-mocks-http'; | ||
import { WacAllowMetadataWriter } from '../../../../../src/ldp/http/metadata/WacAllowMetadataWriter'; | ||
import { RepresentationMetadata } from '../../../../../src/ldp/representation/RepresentationMetadata'; | ||
import type { HttpResponse } from '../../../../../src/server/HttpResponse'; | ||
import { ACL, AUTH } from '../../../../../src/util/Vocabularies'; | ||
|
||
describe('WacAllowMetadataWriter', (): void => { | ||
const writer = new WacAllowMetadataWriter(); | ||
let response: HttpResponse; | ||
|
||
beforeEach(async(): Promise<void> => { | ||
response = createResponse(); | ||
}); | ||
|
||
it('adds no header if there is no relevant metadata.', async(): Promise<void> => { | ||
const metadata = new RepresentationMetadata(); | ||
await expect(writer.handle({ response, metadata })).resolves.toBeUndefined(); | ||
expect(response.getHeaders()).toEqual({ }); | ||
}); | ||
|
||
it('adds a WAC-Allow header if there is relevant metadata.', async(): Promise<void> => { | ||
const metadata = new RepresentationMetadata({ | ||
[AUTH.userMode]: [ ACL.terms.Read, ACL.terms.Write ], | ||
[AUTH.publicMode]: [ ACL.terms.Read ], | ||
}); | ||
await expect(writer.handle({ response, metadata })).resolves.toBeUndefined(); | ||
|
||
expect(response.getHeaders()).toEqual({ | ||
'wac-allow': 'user="read write",public="read"', | ||
}); | ||
}); | ||
|
||
it('only adds a header value for entries with at least 1 permission.', async(): Promise<void> => { | ||
const metadata = new RepresentationMetadata({ | ||
[AUTH.userMode]: [ ACL.terms.Read, ACL.terms.Write ], | ||
}); | ||
await expect(writer.handle({ response, metadata })).resolves.toBeUndefined(); | ||
|
||
expect(response.getHeaders()).toEqual({ | ||
'wac-allow': 'user="read write"', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters