feat: Full rework of account management

Complete rewrite of the account management and related systems.
Makes the architecture more modular,
allowing for easier extensions and configurations.
This commit is contained in:
Joachim Van Herwegen
2022-03-16 10:12:13 +01:00
parent ade977bb4f
commit a47f5236ef
366 changed files with 12345 additions and 5111 deletions

View File

@@ -1,31 +1,27 @@
import type { Operation } from '../../../src/http/Operation';
import { BasicRepresentation } from '../../../src/http/representation/BasicRepresentation';
import type { Representation } from '../../../src/http/representation/Representation';
import { RepresentationMetadata } from '../../../src/http/representation/RepresentationMetadata';
import type { ProviderFactory } from '../../../src/identity/configuration/ProviderFactory';
import type { IdentityProviderHttpHandlerArgs } from '../../../src/identity/IdentityProviderHttpHandler';
import { IdentityProviderHttpHandler } from '../../../src/identity/IdentityProviderHttpHandler';
import type { CookieStore } from '../../../src/identity/interaction/account/util/CookieStore';
import type { Interaction, InteractionHandler } from '../../../src/identity/interaction/InteractionHandler';
import type { HttpRequest } from '../../../src/server/HttpRequest';
import type { HttpResponse } from '../../../src/server/HttpResponse';
import { getBestPreference } from '../../../src/storage/conversion/ConversionUtil';
import type {
RepresentationConverter,
RepresentationConverterArgs,
} from '../../../src/storage/conversion/RepresentationConverter';
import { APPLICATION_JSON, APPLICATION_X_WWW_FORM_URLENCODED } from '../../../src/util/ContentTypes';
import { CONTENT_TYPE } from '../../../src/util/Vocabularies';
import { SOLID_HTTP } from '../../../src/util/Vocabularies';
import type Provider from '../../../templates/types/oidc-provider';
describe('An IdentityProviderHttpHandler', (): void => {
const cookie = 'cookie';
const accountId = 'accountId';
const request: HttpRequest = {} as any;
const response: HttpResponse = {} as any;
const oidcInteraction: Interaction = {} as any;
let operation: Operation;
let representation: Representation;
let providerFactory: jest.Mocked<ProviderFactory>;
let converter: jest.Mocked<RepresentationConverter>;
let provider: jest.Mocked<Provider>;
let cookieStore: jest.Mocked<CookieStore>;
let handler: jest.Mocked<InteractionHandler>;
let idpHandler: IdentityProviderHttpHandler;
@@ -36,6 +32,7 @@ describe('An IdentityProviderHttpHandler', (): void => {
preferences: { type: { 'text/html': 1 }},
body: new BasicRepresentation(),
};
operation.body.metadata.set(SOLID_HTTP.terms.accountCookie, cookie);
provider = {
interactionDetails: jest.fn().mockReturnValue(oidcInteraction),
@@ -45,14 +42,12 @@ describe('An IdentityProviderHttpHandler', (): void => {
getProvider: jest.fn().mockResolvedValue(provider),
};
converter = {
handleSafe: jest.fn((input: RepresentationConverterArgs): Representation => {
// Just find the best match;
const type = getBestPreference(input.preferences.type!, { '*/*': 1 })!;
const metadata = new RepresentationMetadata(input.representation.metadata, { [CONTENT_TYPE]: type.value });
return new BasicRepresentation(input.representation.data, metadata);
}),
} as any;
cookieStore = {
generate: jest.fn(),
get: jest.fn().mockResolvedValue(accountId),
delete: jest.fn(),
refresh: jest.fn(),
};
representation = new BasicRepresentation();
handler = {
@@ -61,7 +56,7 @@ describe('An IdentityProviderHttpHandler', (): void => {
const args: IdentityProviderHttpHandlerArgs = {
providerFactory,
converter,
cookieStore,
handler,
};
idpHandler = new IdentityProviderHttpHandler(args);
@@ -72,8 +67,12 @@ describe('An IdentityProviderHttpHandler', (): void => {
expect(result.statusCode).toBe(200);
expect(result.data).toBe(representation.data);
expect(result.metadata).toBe(representation.metadata);
expect(provider.interactionDetails).toHaveBeenCalledTimes(1);
expect(provider.interactionDetails).toHaveBeenLastCalledWith(request, response);
expect(cookieStore.get).toHaveBeenCalledTimes(1);
expect(cookieStore.get).toHaveBeenLastCalledWith(cookie);
expect(handler.handleSafe).toHaveBeenCalledTimes(1);
expect(handler.handleSafe).toHaveBeenLastCalledWith({ operation, oidcInteraction });
expect(handler.handleSafe).toHaveBeenLastCalledWith({ operation, oidcInteraction, accountId });
});
it('passes no interaction if the provider call failed.', async(): Promise<void> => {
@@ -82,22 +81,24 @@ describe('An IdentityProviderHttpHandler', (): void => {
expect(result.statusCode).toBe(200);
expect(result.data).toBe(representation.data);
expect(result.metadata).toBe(representation.metadata);
expect(provider.interactionDetails).toHaveBeenCalledTimes(1);
expect(provider.interactionDetails).toHaveBeenLastCalledWith(request, response);
expect(cookieStore.get).toHaveBeenCalledTimes(1);
expect(cookieStore.get).toHaveBeenLastCalledWith(cookie);
expect(handler.handleSafe).toHaveBeenCalledTimes(1);
expect(handler.handleSafe).toHaveBeenLastCalledWith({ operation });
expect(handler.handleSafe).toHaveBeenLastCalledWith({ operation, accountId });
});
it('converts input bodies to JSON.', async(): Promise<void> => {
operation.body.metadata.contentType = APPLICATION_X_WWW_FORM_URLENCODED;
it('passes no accountID if there is no cookie.', async(): Promise<void> => {
operation.body.metadata.removeAll(SOLID_HTTP.terms.accountCookie);
const result = await idpHandler.handle({ operation, request, response });
expect(result.statusCode).toBe(200);
expect(result.data).toBe(representation.data);
expect(result.metadata).toBe(representation.metadata);
expect(provider.interactionDetails).toHaveBeenCalledTimes(1);
expect(provider.interactionDetails).toHaveBeenLastCalledWith(request, response);
expect(cookieStore.get).toHaveBeenCalledTimes(0);
expect(handler.handleSafe).toHaveBeenCalledTimes(1);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { body, ...partialOperation } = operation;
expect(handler.handleSafe).toHaveBeenLastCalledWith(
{ operation: expect.objectContaining(partialOperation), oidcInteraction },
);
expect(handler.handleSafe.mock.calls[0][0].operation.body.metadata.contentType).toBe(APPLICATION_JSON);
expect(handler.handleSafe).toHaveBeenLastCalledWith({ operation, oidcInteraction });
});
});