mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Combine the results of multiple CredentialsExtractors
This commit is contained in:
parent
62f026f2bc
commit
ba1886ab85
@ -7,8 +7,8 @@
|
||||
"Supports authentication headers such as `Authentication: WebID http://test.com/card#me`"
|
||||
],
|
||||
"@id": "urn:solid-server:default:CredentialsExtractor",
|
||||
"@type": "WaterfallHandler",
|
||||
"handlers": [
|
||||
"@type": "UnionCredentialsExtractor",
|
||||
"extractors": [
|
||||
{ "@type": "UnsecureWebIdExtractor" },
|
||||
{ "@type": "EmptyCredentialsExtractor" }
|
||||
]
|
||||
|
@ -7,8 +7,14 @@
|
||||
"This extractor always sets the credentials to the fixed value."
|
||||
],
|
||||
"@id": "urn:solid-server:default:CredentialsExtractor",
|
||||
"@type": "UnsecureConstantCredentialsExtractor",
|
||||
"agent": "http://test.com/card#me"
|
||||
"@type": "UnionCredentialsExtractor",
|
||||
"extractors": [
|
||||
{
|
||||
"@type": "UnsecureConstantCredentialsExtractor",
|
||||
"agent": "http://test.com/card#me"
|
||||
},
|
||||
{ "@type": "EmptyCredentialsExtractor" }
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
@ -4,15 +4,20 @@
|
||||
{
|
||||
"comment": "Supports DPoP and Bearer access tokens, or no credentials.",
|
||||
"@id": "urn:solid-server:default:CredentialsExtractor",
|
||||
"@type": "WaterfallHandler",
|
||||
"handlers": [
|
||||
"@type": "UnionCredentialsExtractor",
|
||||
"extractors": [
|
||||
{
|
||||
"@type": "DPoPWebIdExtractor",
|
||||
"originalUrlExtractor": {
|
||||
"@type": "OriginalUrlExtractor"
|
||||
}
|
||||
"@type": "WaterfallHandler",
|
||||
"handlers": [
|
||||
{
|
||||
"@type": "DPoPWebIdExtractor",
|
||||
"originalUrlExtractor": {
|
||||
"@type": "OriginalUrlExtractor"
|
||||
}
|
||||
},
|
||||
{ "@type": "BearerWebIdExtractor" }
|
||||
]
|
||||
},
|
||||
{ "@type": "BearerWebIdExtractor" },
|
||||
{ "@type": "EmptyCredentialsExtractor" }
|
||||
]
|
||||
}
|
||||
|
27
src/authentication/UnionCredentialsExtractor.ts
Normal file
27
src/authentication/UnionCredentialsExtractor.ts
Normal file
@ -0,0 +1,27 @@
|
||||
import { UnionHandler } from '../util/handlers/UnionHandler';
|
||||
import type { CredentialGroup, Credential, CredentialSet } from './Credentials';
|
||||
|
||||
import type { CredentialsExtractor } from './CredentialsExtractor';
|
||||
|
||||
/**
|
||||
* Combines the results of several CredentialsExtractors into one.
|
||||
* If multiple of these extractors return a value for the same key,
|
||||
* the last result will be used.
|
||||
*/
|
||||
export class UnionCredentialsExtractor extends UnionHandler<CredentialsExtractor> {
|
||||
public constructor(extractors: CredentialsExtractor[]) {
|
||||
super(extractors);
|
||||
}
|
||||
|
||||
public async combine(results: CredentialSet[]): Promise<CredentialSet> {
|
||||
// Combine all the results into a single object
|
||||
return results.reduce((result, credential): CredentialSet => {
|
||||
for (const [ key, value ] of Object.entries(credential) as [ CredentialGroup, Credential ][]) {
|
||||
if (value) {
|
||||
result[key] = value;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}, {});
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@ export * from './authentication/Credentials';
|
||||
export * from './authentication/CredentialsExtractor';
|
||||
export * from './authentication/DPoPWebIdExtractor';
|
||||
export * from './authentication/EmptyCredentialsExtractor';
|
||||
export * from './authentication/UnionCredentialsExtractor';
|
||||
export * from './authentication/UnsecureConstantCredentialsExtractor';
|
||||
export * from './authentication/UnsecureWebIdExtractor';
|
||||
|
||||
|
@ -74,7 +74,7 @@ Promise<AsyncHandler<TIn, TOut>[]> {
|
||||
await handler.canHandle(input);
|
||||
return handler;
|
||||
}));
|
||||
const matches = results.filter((result): boolean => result.status === 'fulfilled')
|
||||
const matches = results.filter(({ status }): boolean => status === 'fulfilled')
|
||||
.map((result): AsyncHandler<TIn, TOut> =>
|
||||
(result as PromiseFulfilledResult<AsyncHandler<TIn, TOut>>).value);
|
||||
|
||||
|
46
test/unit/authentication/UnionCredentialsExtractor.test.ts
Normal file
46
test/unit/authentication/UnionCredentialsExtractor.test.ts
Normal file
@ -0,0 +1,46 @@
|
||||
import { CredentialGroup } from '../../../src/authentication/Credentials';
|
||||
import type { Credentials } from '../../../src/authentication/Credentials';
|
||||
import type { CredentialsExtractor } from '../../../src/authentication/CredentialsExtractor';
|
||||
import { UnionCredentialsExtractor } from '../../../src/authentication/UnionCredentialsExtractor';
|
||||
import type { HttpRequest } from '../../../src/server/HttpRequest';
|
||||
|
||||
describe('A UnionCredentialsExtractor', (): void => {
|
||||
const agent: Credentials = { [CredentialGroup.agent]: { webId: 'http://test.com/#me' }};
|
||||
const everyone: Credentials = { [CredentialGroup.public]: {}};
|
||||
const request: HttpRequest = {} as any;
|
||||
let extractors: jest.Mocked<CredentialsExtractor>[];
|
||||
let extractor: UnionCredentialsExtractor;
|
||||
|
||||
beforeEach(async(): Promise<void> => {
|
||||
extractors = [
|
||||
{
|
||||
canHandle: jest.fn(),
|
||||
handle: jest.fn().mockResolvedValue(agent),
|
||||
} as any,
|
||||
{
|
||||
canHandle: jest.fn(),
|
||||
handle: jest.fn().mockResolvedValue(everyone),
|
||||
} as any,
|
||||
];
|
||||
|
||||
extractor = new UnionCredentialsExtractor(extractors);
|
||||
});
|
||||
|
||||
it('combines the results of the extractors.', async(): Promise<void> => {
|
||||
await expect(extractor.handle(request)).resolves.toEqual({
|
||||
[CredentialGroup.agent]: agent.agent,
|
||||
[CredentialGroup.public]: {},
|
||||
});
|
||||
});
|
||||
|
||||
it('ignores undefined values.', async(): Promise<void> => {
|
||||
extractors[1].handle.mockResolvedValueOnce({
|
||||
[CredentialGroup.public]: {},
|
||||
[CredentialGroup.agent]: undefined,
|
||||
});
|
||||
await expect(extractor.handle(request)).resolves.toEqual({
|
||||
[CredentialGroup.agent]: agent.agent,
|
||||
[CredentialGroup.public]: {},
|
||||
});
|
||||
});
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user