feat: Add acl link headers based on headers instead of hardcoding

This commit is contained in:
Joachim Van Herwegen 2021-02-02 17:30:26 +01:00
parent 0c047234e3
commit 8ccc68d29c
4 changed files with 4 additions and 60 deletions

View File

@ -37,14 +37,12 @@
{ {
"LinkRelMetadataWriter:_linkRelMap_key": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type", "LinkRelMetadataWriter:_linkRelMap_key": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
"LinkRelMetadataWriter:_linkRelMap_value": "type" "LinkRelMetadataWriter:_linkRelMap_value": "type"
},
{
"LinkRelMetadataWriter:_linkRelMap_key": "http://www.w3.org/ns/auth/acl#accessControl",
"LinkRelMetadataWriter:_linkRelMap_value": "acl"
} }
] ]
},
{
"@type": "AclLinkMetadataWriter",
"AclLinkMetadataWriter:_aclStrategy": {
"@id": "urn:solid-server:default:AclIdentifierStrategy"
}
} }
] ]
}, },

View File

@ -34,7 +34,6 @@ export * from './ldp/auxiliary/SuffixAuxiliaryIdentifierStrategy';
export * from './ldp/auxiliary/Validator'; export * from './ldp/auxiliary/Validator';
// LDP/HTTP/Metadata // LDP/HTTP/Metadata
export * from './ldp/http/metadata/AclLinkMetadataWriter';
export * from './ldp/http/metadata/BasicMetadataExtractor'; export * from './ldp/http/metadata/BasicMetadataExtractor';
export * from './ldp/http/metadata/ConstantMetadataWriter'; export * from './ldp/http/metadata/ConstantMetadataWriter';
export * from './ldp/http/metadata/ContentTypeParser'; export * from './ldp/http/metadata/ContentTypeParser';

View File

@ -1,25 +0,0 @@
import type { HttpResponse } from '../../../server/HttpResponse';
import { addHeader } from '../../../util/HeaderUtil';
import type { AuxiliaryIdentifierStrategy } from '../../auxiliary/AuxiliaryIdentifierStrategy';
import type { RepresentationMetadata } from '../../representation/RepresentationMetadata';
import { MetadataWriter } from './MetadataWriter';
/**
* A MetadataWriter that always adds a rel="acl" link header to a response.
* The `rel` parameter can be used if a different `rel` value is needed (such as http://www.w3.org/ns/solid/terms#acl).
*/
export class AclLinkMetadataWriter extends MetadataWriter {
private readonly aclStrategy: AuxiliaryIdentifierStrategy;
private readonly rel: string;
public constructor(aclStrategy: AuxiliaryIdentifierStrategy, rel = 'acl') {
super();
this.aclStrategy = aclStrategy;
this.rel = rel;
}
public async handle(input: { response: HttpResponse; metadata: RepresentationMetadata }): Promise<void> {
const identifier = this.aclStrategy.getAuxiliaryIdentifier({ path: input.metadata.identifier.value });
addHeader(input.response, 'Link', `<${identifier.path}>; rel="${this.rel}"`);
}
}

View File

@ -1,28 +0,0 @@
import { createResponse } from 'node-mocks-http';
import type { AuxiliaryIdentifierStrategy } from '../../../../../src/ldp/auxiliary/AuxiliaryIdentifierStrategy';
import { AclLinkMetadataWriter } from '../../../../../src/ldp/http/metadata/AclLinkMetadataWriter';
import { RepresentationMetadata } from '../../../../../src/ldp/representation/RepresentationMetadata';
import type { ResourceIdentifier } from '../../../../../src/ldp/representation/ResourceIdentifier';
describe('An AclLinkMetadataWriter', (): void => {
const strategy = {
getAuxiliaryIdentifier: (id: ResourceIdentifier): ResourceIdentifier => ({ path: `${id.path}.acl` }),
} as AuxiliaryIdentifierStrategy;
const identifier = { path: 'http://test.com/foo' };
it('adds the acl link header.', async(): Promise<void> => {
const writer = new AclLinkMetadataWriter(strategy);
const response = createResponse();
const metadata = new RepresentationMetadata(identifier);
await expect(writer.handle({ response, metadata })).resolves.toBeUndefined();
expect(response.getHeaders()).toEqual({ link: `<${identifier.path}.acl>; rel="acl"` });
});
it('can use a custom rel attribute.', async(): Promise<void> => {
const writer = new AclLinkMetadataWriter(strategy, 'http://www.w3.org/ns/solid/terms#acl');
const response = createResponse();
const metadata = new RepresentationMetadata(identifier);
await expect(writer.handle({ response, metadata })).resolves.toBeUndefined();
expect(response.getHeaders()).toEqual({ link: `<${identifier.path}.acl>; rel="http://www.w3.org/ns/solid/terms#acl"` });
});
});