Skip to content

Commit

Permalink
feat: Combine the results of multiple CredentialsExtractors
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimvh committed Sep 28, 2021
1 parent 62f026f commit ba1886a
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 12 deletions.
4 changes: 2 additions & 2 deletions config/ldp/authentication/debug-auth-header.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
]
Expand Down
10 changes: 8 additions & 2 deletions config/ldp/authentication/debug-test-agent.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
]
}
]
}
19 changes: 12 additions & 7 deletions config/ldp/authentication/dpop-bearer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
]
}
Expand Down
27 changes: 27 additions & 0 deletions src/authentication/UnionCredentialsExtractor.ts
Original file line number Diff line number Diff line change
@@ -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;
}, {});
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
2 changes: 1 addition & 1 deletion src/util/handlers/HandlerUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
46 changes: 46 additions & 0 deletions test/unit/authentication/UnionCredentialsExtractor.test.ts
Original file line number Diff line number Diff line change
@@ -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]: {},
});
});
});

0 comments on commit ba1886a

Please sign in to comment.