-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Combine the results of multiple CredentialsExtractors
- Loading branch information
Showing
7 changed files
with
97 additions
and
12 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,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; | ||
}, {}); | ||
} | ||
} |
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
46 changes: 46 additions & 0 deletions
46
test/unit/authentication/UnionCredentialsExtractor.test.ts
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,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]: {}, | ||
}); | ||
}); | ||
}); |