feat: Combine the results of multiple CredentialsExtractors

This commit is contained in:
Joachim Van Herwegen 2021-09-17 11:51:21 +02:00
parent 62f026f2bc
commit ba1886ab85
7 changed files with 97 additions and 12 deletions

View File

@ -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" }
]

View File

@ -7,8 +7,14 @@
"This extractor always sets the credentials to the fixed value."
],
"@id": "urn:solid-server:default:CredentialsExtractor",
"@type": "UnionCredentialsExtractor",
"extractors": [
{
"@type": "UnsecureConstantCredentialsExtractor",
"agent": "http://test.com/card#me"
},
{ "@type": "EmptyCredentialsExtractor" }
]
}
]
}

View File

@ -4,6 +4,9 @@
{
"comment": "Supports DPoP and Bearer access tokens, or no credentials.",
"@id": "urn:solid-server:default:CredentialsExtractor",
"@type": "UnionCredentialsExtractor",
"extractors": [
{
"@type": "WaterfallHandler",
"handlers": [
{
@ -12,7 +15,9 @@
"@type": "OriginalUrlExtractor"
}
},
{ "@type": "BearerWebIdExtractor" },
{ "@type": "BearerWebIdExtractor" }
]
},
{ "@type": "EmptyCredentialsExtractor" }
]
}

View 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;
}, {});
}
}

View File

@ -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';

View File

@ -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);

View 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]: {},
});
});
});