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

76
test/util/AccountUtil.ts Normal file
View File

@@ -0,0 +1,76 @@
import urljoin from 'url-join';
import type { Account } from '../../src/identity/interaction/account/util/Account';
import type { AccountStore } from '../../src/identity/interaction/account/util/AccountStore';
export function createAccount(id = 'id'): Account {
return { id, logins: {}, webIds: {}, pods: {}, clientCredentials: {}, settings: {}};
}
export function mockAccountStore(account?: Account): jest.Mocked<AccountStore> {
return {
create: jest.fn(async(): Promise<Account> => createAccount()),
get: jest.fn().mockResolvedValue(account),
update: jest.fn(),
};
}
export type User = {
email: string;
password: string;
webId?: string;
podName: string;
settings?: Record<string, unknown>;
};
/**
* Registers an account for the given user details and creates one or more pods.
* @param baseUrl - Base URL of the server.
* @param user - User details to register.
*/
export async function register(baseUrl: string, user: User):
Promise<{ pod: string; webId: string; authorization: string; controls: any }> {
// Get controls
let res = await fetch(urljoin(baseUrl, '.account/'));
let { controls } = await res.json();
// Create account
res = await fetch(controls.account.create, { method: 'POST' });
expect(res.status).toBe(200);
const { cookie } = await res.json();
const authorization = `CSS-Account-Cookie ${cookie}`;
// Get account controls
res = await fetch(controls.account.create, {
headers: { authorization },
});
if (res.status !== 200) {
throw new Error(`Error creating account: ${await res.text()}`);
}
const json = await res.json();
({ controls } = json);
// Add login method
res = await fetch(controls.password.create, {
method: 'POST',
headers: { authorization, 'content-type': 'application/json' },
body: JSON.stringify({
email: user.email,
password: user.password,
}),
});
if (res.status !== 200) {
throw new Error(`Error adding login method: ${await res.text()}`);
}
// Create pod(s)
res = await fetch(controls.account.pod, {
method: 'POST',
headers: { authorization, 'content-type': 'application/json' },
body: JSON.stringify({ name: user.podName, settings: { webId: user.webId, ...user.settings }}),
});
if (res.status !== 200) {
throw new Error(`Error creating pod: ${await res.text()}`);
}
return { ...await res.json(), controls, authorization };
}

View File

@@ -5,6 +5,7 @@ import Describe = jest.Describe;
const portNames = [
// Integration
'Accounts',
'AcpServer',
'Conditions',
'ContentNegotiation',
@@ -48,7 +49,9 @@ export function getPort(name: typeof portNames[number]): number {
if (idx < 0) {
throw new Error(`Unknown port name ${name}`);
}
return 6000 + idx;
// 6000 is a bad port, causing node v18+ to block fetch requests targeting such a URL
// https://fetch.spec.whatwg.org/#port-blocking
return 6000 + idx + 1;
}
export function getSocket(name: typeof socketNames[number]): string {