Skip to content

Commit a976baf

Browse files
committed
refactor: Use AccountStore interfaces from CSS
1 parent c1efcab commit a976baf

12 files changed

Lines changed: 83 additions & 390 deletions

File tree

packages/css/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"demo:start": "yarn run community-solid-server -m . -c ./config/demo.json -f ./tmp -a http://localhost:4000/ -l debug"
6868
},
6969
"dependencies": {
70-
"@solid/community-server": "^7.1.6",
70+
"@solid/community-server": "^7.1.7",
7171
"@solidlab/derived-resources-component": "^1.0.2",
7272
"@solidlab/uma": "workspace:^",
7373
"@types/n3": "^1.16.4",
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { ACCOUNT_SETTINGS_REMEMBER_LOGIN } from '@solid/community-server';
2+
3+
/**
4+
* Settings parameter containing the URL of the user's Authorization Server.
5+
*/
6+
export const ACCOUNT_SETTINGS_AUTHZ_SERVER = 'authzServer';
7+
8+
/**
9+
* Settings parameter containing the Personal Access Token (PAT) of the user at their AS.
10+
*/
11+
export const ACCOUNT_SETTINGS_AS_TOKEN = 'asToken';
12+
13+
/**
14+
* Settings parameter containing the private key of the user.
15+
*/
16+
export const ACCOUNT_SETTINGS_KEYS = 'keys';
17+
18+
export const UMA_ACCOUNT_STORAGE_DESCRIPTION = {
19+
[ACCOUNT_SETTINGS_REMEMBER_LOGIN]: 'boolean?',
20+
[ACCOUNT_SETTINGS_AUTHZ_SERVER]: 'string?',
21+
[ACCOUNT_SETTINGS_AS_TOKEN]: 'string?',
22+
[ACCOUNT_SETTINGS_KEYS]: 'string[]?',
23+
} as const;
24+
25+
// Duplication but needed to get around Components.js limitations
26+
export type UMA_ACCOUNT_STORAGE_TYPE = {
27+
[ACCOUNT_SETTINGS_REMEMBER_LOGIN]: 'boolean?',
28+
[ACCOUNT_SETTINGS_AUTHZ_SERVER]: 'string?',
29+
[ACCOUNT_SETTINGS_AS_TOKEN]: 'string?',
30+
[ACCOUNT_SETTINGS_KEYS]: 'string[]?',
31+
}

packages/css/src/identity/interaction/account/util/AccountStore.ts

Lines changed: 0 additions & 55 deletions
This file was deleted.

packages/css/src/identity/interaction/account/util/LoginStorage.ts

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 5 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,12 @@
1-
2-
import { Initializer, getLoggerFor, createErrorMessage, InternalServerError } from '@solid/community-server';
3-
import { AccountLoginStorage, ACCOUNT_TYPE } from './LoginStorage';
4-
import { ACCOUNT_SETTINGS_AS_TOKEN, ACCOUNT_SETTINGS_AUTHZ_SERVER, ACCOUNT_SETTINGS_KEYS,
5-
ACCOUNT_SETTINGS_REMEMBER_LOGIN, AccountSettings, AccountStore } from './AccountStore';
6-
import { ValueType } from '../../../../storage/keyvalue/IndexedStorage';
7-
8-
export const ACCOUNT_STORAGE_DESCRIPTION = {
9-
[ACCOUNT_SETTINGS_REMEMBER_LOGIN]: 'boolean?',
10-
[ACCOUNT_SETTINGS_AUTHZ_SERVER]: 'string?',
11-
[ACCOUNT_SETTINGS_AS_TOKEN]: 'string?',
12-
[ACCOUNT_SETTINGS_KEYS]: 'string[]?',
13-
} as const;
1+
import { AccountLoginStorage, AccountStore, GenericAccountStore } from '@solid/community-server';
2+
import { UMA_ACCOUNT_STORAGE_DESCRIPTION, UMA_ACCOUNT_STORAGE_TYPE } from './AccountSettings';
143

154
/**
165
* A {@link AccountStore} that uses an {@link AccountLoginStorage} to keep track of the accounts.
176
* Needs to be initialized before it can be used.
187
*/
19-
export class UmaAccountStore extends Initializer implements AccountStore {
20-
private readonly logger = getLoggerFor(this);
21-
22-
private readonly storage: AccountLoginStorage<{ [ACCOUNT_TYPE]: typeof ACCOUNT_STORAGE_DESCRIPTION }>;
23-
private initialized = false;
24-
25-
public constructor(storage: AccountLoginStorage<any>) {
26-
super();
27-
this.storage = storage as typeof this.storage;
28-
}
29-
30-
// Initialize the type definitions
31-
public async handle(): Promise<void> {
32-
if (this.initialized) {
33-
return;
34-
}
35-
try {
36-
await this.storage.defineType(ACCOUNT_TYPE, ACCOUNT_STORAGE_DESCRIPTION, false);
37-
this.initialized = true;
38-
} catch (cause: unknown) {
39-
throw new InternalServerError(`Error defining account in storage: ${createErrorMessage(cause)}`, { cause });
40-
}
41-
}
42-
43-
public async create(): Promise<string> {
44-
const { id } = await this.storage.create(ACCOUNT_TYPE, {});
45-
this.logger.debug(`Created new account ${id}`);
46-
47-
return id;
48-
}
49-
50-
public async getSetting<T extends keyof AccountSettings>(id: string, setting: T): Promise<AccountSettings[T]> {
51-
const account = await this.storage.get(ACCOUNT_TYPE, id);
52-
if (!account) {
53-
return;
54-
}
55-
const { id: unused, ...settings } = account;
56-
return settings[setting];
57-
}
58-
59-
public async updateSetting<T extends keyof AccountSettings>(id: string, setting: T, value: AccountSettings[T]):
60-
Promise<void> {
61-
await this.storage.setField(ACCOUNT_TYPE, id, setting, value as ValueType<typeof ACCOUNT_STORAGE_DESCRIPTION[T]>);
8+
export class UmaAccountStore extends GenericAccountStore<UMA_ACCOUNT_STORAGE_TYPE> {
9+
public constructor(storage: AccountLoginStorage<Record<string, never>>) {
10+
super(storage, UMA_ACCOUNT_STORAGE_DESCRIPTION);
6211
}
6312
}

packages/css/src/index.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ export * from './authorization/UmaPermissionReader';
55

66
export * from './http/output/metadata/UmaTicketMetadataWriter';
77

8-
// export * from './identity/configuration/JwksKeyHolder';
9-
// export * from './identity/configuration/InMemoryJwksKeyHolder';
10-
11-
export * from './identity/interaction/account/util/AccountStore';
12-
export * from './identity/interaction/account/util/LoginStorage';
8+
export * from './identity/interaction/account/util/AccountSettings';
139
export * from './identity/interaction/account/util/UmaAccountStore';
1410

1511
export * from './init/UmaSeededAccountInitializer';
@@ -18,8 +14,6 @@ export * from './server/description/AccountSettingsStorageDescriber';
1814

1915
export * from './server/middleware/JwksHandler';
2016

21-
export * from './storage/keyvalue/IndexedStorage';
22-
2317
export * from './uma/ResourceRegistrar';
2418
export * from './uma/UmaClient';
2519

packages/css/src/init/UmaSeededAccountInitializer.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1+
import {
2+
AccountStore,
3+
createErrorMessage,
4+
getLoggerFor,
5+
Initializer,
6+
PasswordStore,
7+
PodCreator,
8+
URL_SCHEMA
9+
} from '@solid/community-server';
110
import { readJson } from 'fs-extra';
211
import { array, object, string } from 'yup';
3-
import { PasswordStore, PodCreator, URL_SCHEMA, getLoggerFor,
4-
createErrorMessage, Initializer} from '@solid/community-server';
5-
import { ACCOUNT_SETTINGS_AUTHZ_SERVER, ACCOUNT_SETTINGS_KEYS,
6-
type AccountStore } from '../identity/interaction/account/util/AccountStore'
12+
import {
13+
ACCOUNT_SETTINGS_AUTHZ_SERVER,
14+
ACCOUNT_SETTINGS_KEYS,
15+
UMA_ACCOUNT_STORAGE_TYPE
16+
} from '../identity/interaction/account/util/AccountSettings';
717

818
const inSchema = array().of(object({
919
email: string().trim().email().lowercase().required(),
@@ -24,7 +34,7 @@ export interface SeededAccountInitializerArgs {
2434
/**
2535
* Creates the accounts.
2636
*/
27-
accountStore: AccountStore;
37+
accountStore: AccountStore<UMA_ACCOUNT_STORAGE_TYPE>;
2838
/**
2939
* Adds the login methods.
3040
*/
@@ -47,7 +57,7 @@ export interface SeededAccountInitializerArgs {
4757
export class UmaSeededAccountInitializer extends Initializer {
4858
protected readonly logger = getLoggerFor(this);
4959

50-
private readonly accountStore: AccountStore;
60+
private readonly accountStore: AccountStore<UMA_ACCOUNT_STORAGE_TYPE>;
5161
private readonly passwordStore: PasswordStore;
5262
private readonly podCreator: PodCreator;
5363
private readonly configFilePath?: string;

packages/css/src/server/description/AccountSettingsStorageDescriber.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import type { NamedNode, Quad, Quad_Object, Term } from '@rdfjs/types';
2+
import type { AccountSettings, AccountStore, PodStore, ResourceIdentifier } from '@solid/community-server';
3+
import { StorageDescriber } from '@solid/community-server';
24
import { DataFactory } from 'n3';
35
import { stringToTerm } from 'rdf-string';
4-
import type { PodStore, ResourceIdentifier } from '@solid/community-server';
5-
import { StorageDescriber } from '@solid/community-server';
6-
import quad = DataFactory.quad;
76
import namedNode = DataFactory.namedNode;
8-
import type { AccountStore, AccountSettings } from '../../identity/interaction/account/util/AccountStore';
7+
import quad = DataFactory.quad;
98

109
/**
1110
* Adds triples to the storage description resource, based on the settings of
12-
* the account that created the storage.
13-
*
11+
* the account that created the storage.
12+
*
1413
* The resource identifier of the storage is used as subject.
1514
*/
1615
export class AccountSettingsStorageDescriber extends StorageDescriber {
@@ -22,15 +21,15 @@ export class AccountSettingsStorageDescriber extends StorageDescriber {
2221
terms: Record<string, keyof AccountSettings>,
2322
) {
2423
super();
25-
24+
2625
const termMap = new Map<NamedNode, keyof AccountSettings>();
2726
for (const [ predicate, settingsKey ] of Object.entries(terms)) {
28-
27+
2928
const predTerm = stringToTerm(predicate);
3029
if (predTerm.termType !== 'NamedNode') {
3130
throw new Error('Predicate needs to be a named node.');
3231
}
33-
32+
3433
termMap.set(predTerm, settingsKey);
3534
}
3635

@@ -51,7 +50,7 @@ export class AccountSettingsStorageDescriber extends StorageDescriber {
5150
}
5251

5352
private async* generateTriples(subject: NamedNode, account: string): AsyncGenerator<Quad> {
54-
53+
5554
for (const [ predicate, settingsKey ] of this.terms.entries()) {
5655

5756
const settingsValue = await this.accountStore.getSetting(account, settingsKey);

0 commit comments

Comments
 (0)