Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
147 additions
and
3 deletions.
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
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,39 @@ | ||
import type { AuxiliaryIdentifierStrategy } from '../../http/auxiliary/AuxiliaryIdentifierStrategy'; | ||
import type { TargetExtractor } from '../../http/input/identifier/TargetExtractor'; | ||
import { addHeader } from '../../util/HeaderUtil'; | ||
import { ACP } from '../../util/Vocabularies'; | ||
import type { HttpHandlerInput } from '../HttpHandler'; | ||
import { HttpHandler } from '../HttpHandler'; | ||
|
||
/** | ||
* Handles all the required ACP headers as defined at | ||
* https://solid.github.io/authorization-panel/acp-specification/#conforming-acp-server | ||
*/ | ||
export class AcpHeaderHandler extends HttpHandler { | ||
private readonly targetExtractor: TargetExtractor; | ||
private readonly strategy: AuxiliaryIdentifierStrategy; | ||
private readonly modes: string[]; | ||
private readonly attributes: string[]; | ||
|
||
public constructor(targetExtractor: TargetExtractor, strategy: AuxiliaryIdentifierStrategy, | ||
modes: string[], attributes: string[]) { | ||
super(); | ||
this.targetExtractor = targetExtractor; | ||
this.strategy = strategy; | ||
this.modes = modes; | ||
this.attributes = attributes; | ||
} | ||
|
||
public async handle({ request, response }: HttpHandlerInput): Promise<void> { | ||
const identifier = await this.targetExtractor.handleSafe({ request }); | ||
if (!this.strategy.isAuxiliaryIdentifier(identifier)) { | ||
return; | ||
} | ||
const linkValues = [ | ||
`<${ACP.AccessControlResource}>; rel="type"`, | ||
...this.modes.map((mode): string => `<${mode}>; rel="${ACP.grant}"`), | ||
...this.attributes.map((attribute): string => `<${attribute}>; rel="${ACP.attribute}"`), | ||
]; | ||
addHeader(response, 'Link', linkValues); | ||
} | ||
} |
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
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,47 @@ | ||
import { ACP } from '@solid/access-control-policy/dist/constant/acp'; | ||
import { createResponse } from 'node-mocks-http'; | ||
import type { AuxiliaryIdentifierStrategy } from '../../../../src/http/auxiliary/AuxiliaryIdentifierStrategy'; | ||
import type { TargetExtractor } from '../../../../src/http/input/identifier/TargetExtractor'; | ||
import type { HttpRequest } from '../../../../src/server/HttpRequest'; | ||
import type { HttpResponse } from '../../../../src/server/HttpResponse'; | ||
import { AcpHeaderHandler } from '../../../../src/server/middleware/AcpHeaderHandler'; | ||
import { ACL } from '../../../../src/util/Vocabularies'; | ||
import { SimpleSuffixStrategy } from '../../../util/SimpleSuffixStrategy'; | ||
|
||
describe('an AcpHeaderHandler', (): void => { | ||
const request: HttpRequest = {} as any; | ||
let response: HttpResponse; | ||
const modes = [ ACL.Read, ACL.Write ]; | ||
const attributes = [ ACP.agent, ACP.client ]; | ||
let targetExtractor: jest.Mocked<TargetExtractor>; | ||
let strategy: AuxiliaryIdentifierStrategy; | ||
let handler: AcpHeaderHandler; | ||
|
||
beforeEach(async(): Promise<void> => { | ||
response = createResponse() as HttpResponse; | ||
targetExtractor = { | ||
handleSafe: jest.fn().mockResolvedValue({ path: 'http://example.org/foo/bar' }), | ||
} as any; | ||
|
||
strategy = new SimpleSuffixStrategy('.acr'); | ||
|
||
handler = new AcpHeaderHandler(targetExtractor, strategy, modes, attributes); | ||
}); | ||
|
||
it('adds no headers if the target is not an ACR.', async(): Promise<void> => { | ||
await expect(handler.handle({ request, response })).resolves.toBeUndefined(); | ||
expect(response.getHeaders()).toEqual({}); | ||
}); | ||
|
||
it('adds all the required headers.', async(): Promise<void> => { | ||
targetExtractor.handleSafe.mockResolvedValueOnce({ path: 'http://example.org/foo/bar.acr' }); | ||
await expect(handler.handle({ request, response })).resolves.toBeUndefined(); | ||
expect(response.getHeaders()).toEqual({ link: [ | ||
'<http://www.w3.org/ns/solid/acp#AccessControlResource>; rel="type"', | ||
'<http://www.w3.org/ns/auth/acl#Read>; rel="http://www.w3.org/ns/solid/acp#grant"', | ||
'<http://www.w3.org/ns/auth/acl#Write>; rel="http://www.w3.org/ns/solid/acp#grant"', | ||
'<http://www.w3.org/ns/solid/acp#agent>; rel="http://www.w3.org/ns/solid/acp#attribute"', | ||
'<http://www.w3.org/ns/solid/acp#client>; rel="http://www.w3.org/ns/solid/acp#attribute"', | ||
]}); | ||
}); | ||
}); |