favalib 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/Command/BaseCommand.d.mts +65 -0
- package/build/Command/BaseCommand.mjs +54 -0
- package/build/Command/CommandQueue.d.mts +28 -0
- package/build/Command/CommandQueue.mjs +43 -0
- package/build/Command/commandConstructors.d.mts +11 -0
- package/build/Command/commandConstructors.mjs +11 -0
- package/build/Command/commands/AddEntryCommand.d.mts +31 -0
- package/build/Command/commands/AddEntryCommand.mjs +43 -0
- package/build/Command/commands/AddSyncDeviceCommand.d.mts +36 -0
- package/build/Command/commands/AddSyncDeviceCommand.mjs +42 -0
- package/build/Command/commands/DeleteEntryCommand.d.mts +35 -0
- package/build/Command/commands/DeleteEntryCommand.mjs +50 -0
- package/build/Command/commands/UpdateEntryCommand.d.mts +38 -0
- package/build/Command/commands/UpdateEntryCommand.mjs +51 -0
- package/build/CryptoProviders/browser/index.d.mts +73 -0
- package/build/CryptoProviders/browser/index.mjs +209 -0
- package/build/CryptoProviders/node/index.d.mts +62 -0
- package/build/CryptoProviders/node/index.mjs +189 -0
- package/build/TwoFALibError.d.mts +77 -0
- package/build/TwoFALibError.mjs +91 -0
- package/build/TwoFaLib.d.mts +95 -0
- package/build/TwoFaLib.mjs +180 -0
- package/build/TwoFaLibEvent.d.mts +8 -0
- package/build/TwoFaLibEvent.mjs +9 -0
- package/build/TwoFaLibMediator.d.mts +37 -0
- package/build/TwoFaLibMediator.mjs +58 -0
- package/build/interfaces/CommandTypes.d.mts +20 -0
- package/build/interfaces/CommandTypes.mjs +1 -0
- package/build/interfaces/CryptoLib.d.mts +113 -0
- package/build/interfaces/CryptoLib.mjs +1 -0
- package/build/interfaces/Entry.d.mts +33 -0
- package/build/interfaces/Entry.mjs +1 -0
- package/build/interfaces/Events.d.mts +22 -0
- package/build/interfaces/Events.mjs +1 -0
- package/build/interfaces/PassphraseExtraDict.d.ts +2 -0
- package/build/interfaces/PassphraseExtraDict.js +1 -0
- package/build/interfaces/SyncTypes.d.mts +45 -0
- package/build/interfaces/SyncTypes.mjs +1 -0
- package/build/interfaces/Vault.d.mts +30 -0
- package/build/interfaces/Vault.mjs +1 -0
- package/build/main.d.mts +12 -0
- package/build/main.mjs +5 -0
- package/build/subclasses/CommandManager.d.mts +46 -0
- package/build/subclasses/CommandManager.mjs +117 -0
- package/build/subclasses/ExportImportManager.d.mts +58 -0
- package/build/subclasses/ExportImportManager.mjs +105 -0
- package/build/subclasses/LibraryLoader.d.mts +56 -0
- package/build/subclasses/LibraryLoader.mjs +108 -0
- package/build/subclasses/PersistentStorageManager.d.mts +71 -0
- package/build/subclasses/PersistentStorageManager.mjs +127 -0
- package/build/subclasses/SyncManager.d.mts +161 -0
- package/build/subclasses/SyncManager.mjs +567 -0
- package/build/subclasses/VaultDataManager.d.mts +68 -0
- package/build/subclasses/VaultDataManager.mjs +114 -0
- package/build/subclasses/VaultOperationsManager.d.mts +91 -0
- package/build/subclasses/VaultOperationsManager.mjs +163 -0
- package/build/utils/constants.d.mts +2 -0
- package/build/utils/constants.mjs +1 -0
- package/build/utils/creationUtils.d.mts +43 -0
- package/build/utils/creationUtils.mjs +125 -0
- package/build/utils/exportImportUtils.d.mts +53 -0
- package/build/utils/exportImportUtils.mjs +185 -0
- package/build/utils/qrUtils.d.mts +25 -0
- package/build/utils/qrUtils.mjs +84 -0
- package/build/utils/syncUtils.d.mts +26 -0
- package/build/utils/syncUtils.mjs +78 -0
- package/package.json +56 -0
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type TwoFaLibMediator from '../TwoFaLibMediator.mjs';
|
|
2
|
+
import { CommandData } from '../interfaces/CommandTypes.mjs';
|
|
3
|
+
/**
|
|
4
|
+
* Abstract base class for commands that interact with the vault.
|
|
5
|
+
* @template T - The type of command data, extending CommandData.
|
|
6
|
+
*/
|
|
7
|
+
declare abstract class BaseCommand<T extends CommandData = CommandData> {
|
|
8
|
+
readonly id: string;
|
|
9
|
+
readonly type: string;
|
|
10
|
+
readonly timestamp: number;
|
|
11
|
+
readonly version: string;
|
|
12
|
+
readonly data: T;
|
|
13
|
+
readonly fromRemote: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Creates a new BaseCommand instance.
|
|
16
|
+
* @param type - The type of the command.
|
|
17
|
+
* @param data - The data associated with the command.
|
|
18
|
+
* @param id - The unique identifier for the command. If not provided, a new UUID will be generated.
|
|
19
|
+
* @param timestamp - The timestamp of when the command was created. If not provided, the current timestamp will be used.
|
|
20
|
+
* @param version - The version of the command. Defaults to '1.0'.
|
|
21
|
+
* @param fromRemote - Indicates if the command originated from a remote source. Defaults to false.
|
|
22
|
+
*/
|
|
23
|
+
constructor(type: string, data: T, id?: string, timestamp?: number, version?: string, fromRemote?: boolean);
|
|
24
|
+
/**
|
|
25
|
+
* Executes the command using the provided mediator, which can be used to access the other classes.
|
|
26
|
+
* @param VaultDataManager - The TwoFaLibMediator instance to use for execution.
|
|
27
|
+
* @returns A Promise that resolves when the execution is complete.
|
|
28
|
+
*/
|
|
29
|
+
abstract execute(twoFaLibMediator: TwoFaLibMediator): Promise<void>;
|
|
30
|
+
/**
|
|
31
|
+
* Creates an undo command that, when executed, reverses the effects of this command.
|
|
32
|
+
* @param VaultDataManager - The TwoFaLibMediator instance to use for creating the undo command.
|
|
33
|
+
* @returns A BaseCommand instance that undoes this command.
|
|
34
|
+
*/
|
|
35
|
+
abstract createUndoCommand(TwoFaLibMediator: TwoFaLibMediator): BaseCommand;
|
|
36
|
+
/**
|
|
37
|
+
* Creates a new instance of the command with the provided data.
|
|
38
|
+
* @param data - The data to use for creating the new command instance.
|
|
39
|
+
* @returns A new instance of the command.
|
|
40
|
+
*/
|
|
41
|
+
static create<T extends CommandData, C extends BaseCommand<T>>(this: new (data: T, id?: string, timestamp?: number, version?: string, fromRemote?: boolean) => C, data: T): C;
|
|
42
|
+
/**
|
|
43
|
+
* Creates a new instance of the command from JSON data.
|
|
44
|
+
* @param input - The JSON input containing the command data.
|
|
45
|
+
* @returns A new instance of the command created from the JSON data.
|
|
46
|
+
*/
|
|
47
|
+
static fromJSON<T extends CommandData, C extends BaseCommand<T>>(this: new (data: T, id: string, timestamp: number, version: string, fromRemote: boolean) => C, input: {
|
|
48
|
+
data: T;
|
|
49
|
+
id: string;
|
|
50
|
+
timestamp: number;
|
|
51
|
+
version: string;
|
|
52
|
+
}): C;
|
|
53
|
+
/**
|
|
54
|
+
* Converts the command instance to a JSON-serializable object.
|
|
55
|
+
* @returns An object representation of the command.
|
|
56
|
+
*/
|
|
57
|
+
toJSON(): {
|
|
58
|
+
id: string;
|
|
59
|
+
type: string;
|
|
60
|
+
data: T;
|
|
61
|
+
timestamp: number;
|
|
62
|
+
version: string;
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
export default BaseCommand;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
2
|
+
/**
|
|
3
|
+
* Abstract base class for commands that interact with the vault.
|
|
4
|
+
* @template T - The type of command data, extending CommandData.
|
|
5
|
+
*/
|
|
6
|
+
class BaseCommand {
|
|
7
|
+
/**
|
|
8
|
+
* Creates a new BaseCommand instance.
|
|
9
|
+
* @param type - The type of the command.
|
|
10
|
+
* @param data - The data associated with the command.
|
|
11
|
+
* @param id - The unique identifier for the command. If not provided, a new UUID will be generated.
|
|
12
|
+
* @param timestamp - The timestamp of when the command was created. If not provided, the current timestamp will be used.
|
|
13
|
+
* @param version - The version of the command. Defaults to '1.0'.
|
|
14
|
+
* @param fromRemote - Indicates if the command originated from a remote source. Defaults to false.
|
|
15
|
+
*/
|
|
16
|
+
constructor(type, data, id = uuidv4(), timestamp = Date.now(), version = '1.0', fromRemote = false) {
|
|
17
|
+
this.id = id;
|
|
18
|
+
this.type = type;
|
|
19
|
+
this.timestamp = timestamp;
|
|
20
|
+
this.version = version;
|
|
21
|
+
this.data = data;
|
|
22
|
+
this.fromRemote = fromRemote;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Creates a new instance of the command with the provided data.
|
|
26
|
+
* @param data - The data to use for creating the new command instance.
|
|
27
|
+
* @returns A new instance of the command.
|
|
28
|
+
*/
|
|
29
|
+
static create(data) {
|
|
30
|
+
return new this(data);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Creates a new instance of the command from JSON data.
|
|
34
|
+
* @param input - The JSON input containing the command data.
|
|
35
|
+
* @returns A new instance of the command created from the JSON data.
|
|
36
|
+
*/
|
|
37
|
+
static fromJSON(input) {
|
|
38
|
+
return new this(input.data, input.id, input.timestamp, input.version, true);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Converts the command instance to a JSON-serializable object.
|
|
42
|
+
* @returns An object representation of the command.
|
|
43
|
+
*/
|
|
44
|
+
toJSON() {
|
|
45
|
+
return {
|
|
46
|
+
id: this.id,
|
|
47
|
+
type: this.type,
|
|
48
|
+
data: this.data,
|
|
49
|
+
timestamp: this.timestamp,
|
|
50
|
+
version: this.version,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
export default BaseCommand;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import Command from './BaseCommand.mjs';
|
|
2
|
+
/**
|
|
3
|
+
* Represents a queue of commands with timestamp-based sorting.
|
|
4
|
+
*/
|
|
5
|
+
declare class CommandQueue {
|
|
6
|
+
private queue;
|
|
7
|
+
/**
|
|
8
|
+
* @returns The number of commands in the queue.
|
|
9
|
+
*/
|
|
10
|
+
get size(): number;
|
|
11
|
+
/**
|
|
12
|
+
* Adds a command to the queue and sorts it based on timestamp.
|
|
13
|
+
* @param command - The command to be added to the queue.
|
|
14
|
+
*/
|
|
15
|
+
enqueue(command: Command): void;
|
|
16
|
+
/**
|
|
17
|
+
* Removes and returns the first command from the queue.
|
|
18
|
+
* @returns The first command in the queue, or undefined if the queue is empty.
|
|
19
|
+
* @throws {TwoFALibError} If the queue is empty.
|
|
20
|
+
*/
|
|
21
|
+
dequeue(): Command | undefined;
|
|
22
|
+
/**
|
|
23
|
+
* Checks if the queue is empty.
|
|
24
|
+
* @returns True if the queue is empty, false otherwise.
|
|
25
|
+
*/
|
|
26
|
+
isEmpty(): boolean;
|
|
27
|
+
}
|
|
28
|
+
export default CommandQueue;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { TwoFALibError } from '../TwoFALibError.mjs';
|
|
2
|
+
/**
|
|
3
|
+
* Represents a queue of commands with timestamp-based sorting.
|
|
4
|
+
*/
|
|
5
|
+
class CommandQueue {
|
|
6
|
+
constructor() {
|
|
7
|
+
this.queue = [];
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* @returns The number of commands in the queue.
|
|
11
|
+
*/
|
|
12
|
+
get size() {
|
|
13
|
+
return this.queue.length;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Adds a command to the queue and sorts it based on timestamp.
|
|
17
|
+
* @param command - The command to be added to the queue.
|
|
18
|
+
*/
|
|
19
|
+
enqueue(command) {
|
|
20
|
+
// command queue shouldn't be very large, so this simple implementation should be fine
|
|
21
|
+
this.queue.push(command);
|
|
22
|
+
this.queue.sort((a, b) => a.timestamp - b.timestamp);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Removes and returns the first command from the queue.
|
|
26
|
+
* @returns The first command in the queue, or undefined if the queue is empty.
|
|
27
|
+
* @throws {TwoFALibError} If the queue is empty.
|
|
28
|
+
*/
|
|
29
|
+
dequeue() {
|
|
30
|
+
if (this.isEmpty()) {
|
|
31
|
+
throw new TwoFALibError('Command queue is empty');
|
|
32
|
+
}
|
|
33
|
+
return this.queue.shift();
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Checks if the queue is empty.
|
|
37
|
+
* @returns True if the queue is empty, false otherwise.
|
|
38
|
+
*/
|
|
39
|
+
isEmpty() {
|
|
40
|
+
return this.queue.length === 0;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
export default CommandQueue;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import AddEntryCommand from './commands/AddEntryCommand.mjs';
|
|
2
|
+
import AddSyncDeviceCommand from './commands/AddSyncDeviceCommand.mjs';
|
|
3
|
+
import DeleteEntryCommand from './commands/DeleteEntryCommand.mjs';
|
|
4
|
+
import UpdateEntryCommand from './commands/UpdateEntryCommand.mjs';
|
|
5
|
+
declare const commandConstructors: {
|
|
6
|
+
AddEntry: typeof AddEntryCommand;
|
|
7
|
+
DeleteEntry: typeof DeleteEntryCommand;
|
|
8
|
+
UpdateEntry: typeof UpdateEntryCommand;
|
|
9
|
+
AddSyncDevice: typeof AddSyncDeviceCommand;
|
|
10
|
+
};
|
|
11
|
+
export default commandConstructors;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import AddEntryCommand from './commands/AddEntryCommand.mjs';
|
|
2
|
+
import AddSyncDeviceCommand from './commands/AddSyncDeviceCommand.mjs';
|
|
3
|
+
import DeleteEntryCommand from './commands/DeleteEntryCommand.mjs';
|
|
4
|
+
import UpdateEntryCommand from './commands/UpdateEntryCommand.mjs';
|
|
5
|
+
const commandConstructors = {
|
|
6
|
+
AddEntry: AddEntryCommand,
|
|
7
|
+
DeleteEntry: DeleteEntryCommand,
|
|
8
|
+
UpdateEntry: UpdateEntryCommand,
|
|
9
|
+
AddSyncDevice: AddSyncDeviceCommand,
|
|
10
|
+
};
|
|
11
|
+
export default commandConstructors;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type TwoFaLibMediator from '../../TwoFaLibMediator.mjs';
|
|
2
|
+
import Command from '../BaseCommand.mjs';
|
|
3
|
+
import type Entry from '../../interfaces/Entry.mjs';
|
|
4
|
+
export type AddEntryData = Entry;
|
|
5
|
+
/**
|
|
6
|
+
* Represents a command that when executed add an entry to the vault.
|
|
7
|
+
*/
|
|
8
|
+
declare class AddEntryCommand extends Command<AddEntryData> {
|
|
9
|
+
/**
|
|
10
|
+
* Creates a new AddEntryCommand instance.
|
|
11
|
+
* @inheritdoc
|
|
12
|
+
* @param data - The data of the entry to be added.
|
|
13
|
+
*/
|
|
14
|
+
constructor(data: AddEntryData, id?: string, timestamp?: number, version?: string, fromRemote?: boolean);
|
|
15
|
+
/**
|
|
16
|
+
* Executes the command to add the entry to the vault.
|
|
17
|
+
* @inheritdoc
|
|
18
|
+
* @throws {InvalidCommandError} If the command data is invalid.
|
|
19
|
+
*/
|
|
20
|
+
execute(mediator: TwoFaLibMediator): Promise<void>;
|
|
21
|
+
/**
|
|
22
|
+
* @inheritdoc
|
|
23
|
+
*/
|
|
24
|
+
createUndoCommand(): Command;
|
|
25
|
+
/**
|
|
26
|
+
* Validates the command data.
|
|
27
|
+
* @returns True if the command data is valid, false otherwise.
|
|
28
|
+
*/
|
|
29
|
+
validate(): boolean;
|
|
30
|
+
}
|
|
31
|
+
export default AddEntryCommand;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { InvalidCommandError } from '../../TwoFALibError.mjs';
|
|
2
|
+
import Command from '../BaseCommand.mjs';
|
|
3
|
+
import DeleteEntryCommand from './DeleteEntryCommand.mjs';
|
|
4
|
+
/**
|
|
5
|
+
* Represents a command that when executed add an entry to the vault.
|
|
6
|
+
*/
|
|
7
|
+
class AddEntryCommand extends Command {
|
|
8
|
+
/**
|
|
9
|
+
* Creates a new AddEntryCommand instance.
|
|
10
|
+
* @inheritdoc
|
|
11
|
+
* @param data - The data of the entry to be added.
|
|
12
|
+
*/
|
|
13
|
+
constructor(data, id, timestamp, version, fromRemote = false) {
|
|
14
|
+
super('AddEntry', data, id, timestamp, version, fromRemote);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Executes the command to add the entry to the vault.
|
|
18
|
+
* @inheritdoc
|
|
19
|
+
* @throws {InvalidCommandError} If the command data is invalid.
|
|
20
|
+
*/
|
|
21
|
+
async execute(mediator) {
|
|
22
|
+
const vault = mediator.getComponent('vaultDataManager');
|
|
23
|
+
if (!this.validate()) {
|
|
24
|
+
throw new InvalidCommandError('Invalid AddEntry command');
|
|
25
|
+
}
|
|
26
|
+
await vault.addEntry(this.data);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* @inheritdoc
|
|
30
|
+
*/
|
|
31
|
+
createUndoCommand() {
|
|
32
|
+
return DeleteEntryCommand.create({ entryId: this.data.id });
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Validates the command data.
|
|
36
|
+
* @returns True if the command data is valid, false otherwise.
|
|
37
|
+
*/
|
|
38
|
+
validate() {
|
|
39
|
+
// TODO: actually validate
|
|
40
|
+
return this.data.id !== undefined;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
export default AddEntryCommand;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type TwoFaLibMediator from '../../TwoFaLibMediator.mjs';
|
|
2
|
+
import Command from '../BaseCommand.mjs';
|
|
3
|
+
import type { DeviceId, DeviceType } from '../../interfaces/SyncTypes.mjs';
|
|
4
|
+
import type { PublicKey } from '../../interfaces/CryptoLib.mjs';
|
|
5
|
+
export interface AddSyncDeviceData {
|
|
6
|
+
deviceId: DeviceId;
|
|
7
|
+
deviceType: DeviceType;
|
|
8
|
+
publicKey: PublicKey;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Represents a command that when executed add an entry to the vault.
|
|
12
|
+
*/
|
|
13
|
+
declare class AddSyncDeviceCommand extends Command<AddSyncDeviceData> {
|
|
14
|
+
/**
|
|
15
|
+
* Creates a new AddEntryCommand instance.
|
|
16
|
+
* @inheritdoc
|
|
17
|
+
* @param data - The data of the entry to be added.
|
|
18
|
+
*/
|
|
19
|
+
constructor(data: AddSyncDeviceData, id?: string, timestamp?: number, version?: string, fromRemote?: boolean);
|
|
20
|
+
/**
|
|
21
|
+
* Executes the command to add the entry to the vault.
|
|
22
|
+
* @inheritdoc
|
|
23
|
+
* @throws {InvalidCommandError} If the command data is invalid.
|
|
24
|
+
*/
|
|
25
|
+
execute(mediator: TwoFaLibMediator): Promise<void>;
|
|
26
|
+
/**
|
|
27
|
+
* @inheritdoc
|
|
28
|
+
*/
|
|
29
|
+
createUndoCommand(): Command;
|
|
30
|
+
/**
|
|
31
|
+
* Validates the command data.
|
|
32
|
+
* @returns True if the command data is valid, false otherwise.
|
|
33
|
+
*/
|
|
34
|
+
validate(): boolean;
|
|
35
|
+
}
|
|
36
|
+
export default AddSyncDeviceCommand;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { InvalidCommandError, TwoFALibError } from '../../TwoFALibError.mjs';
|
|
2
|
+
import Command from '../BaseCommand.mjs';
|
|
3
|
+
/**
|
|
4
|
+
* Represents a command that when executed add an entry to the vault.
|
|
5
|
+
*/
|
|
6
|
+
class AddSyncDeviceCommand extends Command {
|
|
7
|
+
/**
|
|
8
|
+
* Creates a new AddEntryCommand instance.
|
|
9
|
+
* @inheritdoc
|
|
10
|
+
* @param data - The data of the entry to be added.
|
|
11
|
+
*/
|
|
12
|
+
constructor(data, id, timestamp, version, fromRemote = false) {
|
|
13
|
+
super('AddSyncDevice', data, id, timestamp, version, fromRemote);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Executes the command to add the entry to the vault.
|
|
17
|
+
* @inheritdoc
|
|
18
|
+
* @throws {InvalidCommandError} If the command data is invalid.
|
|
19
|
+
*/
|
|
20
|
+
async execute(mediator) {
|
|
21
|
+
const syncManager = mediator.getComponent('syncManager');
|
|
22
|
+
if (!this.validate()) {
|
|
23
|
+
throw new InvalidCommandError('Invalid AddEntry command');
|
|
24
|
+
}
|
|
25
|
+
await syncManager.addSyncDevice(this.data);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* @inheritdoc
|
|
29
|
+
*/
|
|
30
|
+
createUndoCommand() {
|
|
31
|
+
throw new TwoFALibError('Not implemented yet');
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Validates the command data.
|
|
35
|
+
* @returns True if the command data is valid, false otherwise.
|
|
36
|
+
*/
|
|
37
|
+
validate() {
|
|
38
|
+
// TODO: actually validate
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export default AddSyncDeviceCommand;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import Command from '../BaseCommand.mjs';
|
|
2
|
+
import type TwoFaLibMediator from '../../TwoFaLibMediator.mjs';
|
|
3
|
+
import type { EntryId } from '../../interfaces/Entry.mjs';
|
|
4
|
+
export interface DeleteEntryData {
|
|
5
|
+
entryId: EntryId;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Represents a command that when executed deletes an entry from the vault.
|
|
9
|
+
*/
|
|
10
|
+
declare class DeleteEntryCommand extends Command<DeleteEntryData> {
|
|
11
|
+
private deletedEntry?;
|
|
12
|
+
/**
|
|
13
|
+
* Creates a new DeleteEntryCommand instance.
|
|
14
|
+
* @inheritdoc
|
|
15
|
+
* @param data - The data of the entry to be deleted.
|
|
16
|
+
*/
|
|
17
|
+
constructor(data: DeleteEntryData, id?: string, timestamp?: number, version?: string, fromRemote?: boolean);
|
|
18
|
+
/**
|
|
19
|
+
* Executes the command to delete the entry from the vault.
|
|
20
|
+
* @inheritdoc
|
|
21
|
+
* @throws {InvalidCommandError} If the command data is invalid or the entry doesn't exist.
|
|
22
|
+
*/
|
|
23
|
+
execute(mediator: TwoFaLibMediator): Promise<void>;
|
|
24
|
+
/**
|
|
25
|
+
* @inheritdoc
|
|
26
|
+
* @throws {InvalidCommandError} If the deleted entry content is not available.
|
|
27
|
+
*/
|
|
28
|
+
createUndoCommand(): Command;
|
|
29
|
+
/**
|
|
30
|
+
* Validates the command data.
|
|
31
|
+
* @returns True if the command data is valid, false otherwise.
|
|
32
|
+
*/
|
|
33
|
+
validate(): boolean;
|
|
34
|
+
}
|
|
35
|
+
export default DeleteEntryCommand;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { InvalidCommandError } from '../../TwoFALibError.mjs';
|
|
2
|
+
import Command from '../BaseCommand.mjs';
|
|
3
|
+
import AddEntryCommand from './AddEntryCommand.mjs';
|
|
4
|
+
/**
|
|
5
|
+
* Represents a command that when executed deletes an entry from the vault.
|
|
6
|
+
*/
|
|
7
|
+
class DeleteEntryCommand extends Command {
|
|
8
|
+
/**
|
|
9
|
+
* Creates a new DeleteEntryCommand instance.
|
|
10
|
+
* @inheritdoc
|
|
11
|
+
* @param data - The data of the entry to be deleted.
|
|
12
|
+
*/
|
|
13
|
+
constructor(data, id, timestamp, version, fromRemote = false) {
|
|
14
|
+
super('DeleteEntry', data, id, timestamp, version, fromRemote);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Executes the command to delete the entry from the vault.
|
|
18
|
+
* @inheritdoc
|
|
19
|
+
* @throws {InvalidCommandError} If the command data is invalid or the entry doesn't exist.
|
|
20
|
+
*/
|
|
21
|
+
async execute(mediator) {
|
|
22
|
+
if (!this.validate()) {
|
|
23
|
+
throw new InvalidCommandError('Invalid DeleteEntry command');
|
|
24
|
+
}
|
|
25
|
+
const vault = mediator.getComponent('vaultDataManager');
|
|
26
|
+
this.deletedEntry = vault.getFullEntry(this.data.entryId);
|
|
27
|
+
if (this.deletedEntry === undefined) {
|
|
28
|
+
throw new InvalidCommandError(`Entry with id ${this.data.entryId} does not exist`);
|
|
29
|
+
}
|
|
30
|
+
await vault.deleteEntry(this.data.entryId);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* @inheritdoc
|
|
34
|
+
* @throws {InvalidCommandError} If the deleted entry content is not available.
|
|
35
|
+
*/
|
|
36
|
+
createUndoCommand() {
|
|
37
|
+
if (this.deletedEntry === undefined) {
|
|
38
|
+
throw new InvalidCommandError('Cannot create undo command, content not available');
|
|
39
|
+
}
|
|
40
|
+
return AddEntryCommand.create(this.deletedEntry);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Validates the command data.
|
|
44
|
+
* @returns True if the command data is valid, false otherwise.
|
|
45
|
+
*/
|
|
46
|
+
validate() {
|
|
47
|
+
return this.data.entryId !== undefined;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
export default DeleteEntryCommand;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type TwoFaLibMediator from '../../TwoFaLibMediator.mjs';
|
|
2
|
+
import Command from '../BaseCommand.mjs';
|
|
3
|
+
import type Entry from '../../interfaces/Entry.mjs';
|
|
4
|
+
import { EntryId } from '../../interfaces/Entry.mjs';
|
|
5
|
+
export interface UpdateEntryData {
|
|
6
|
+
entryId: EntryId;
|
|
7
|
+
oldEntry: Entry;
|
|
8
|
+
updatedEntry: Entry;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Represents a command that when executed updates an entry in the vault.
|
|
12
|
+
*/
|
|
13
|
+
declare class UpdateEntryCommand extends Command<UpdateEntryData> {
|
|
14
|
+
private originalEntry?;
|
|
15
|
+
/**
|
|
16
|
+
* Creates a new UpdateEntryCommand instance.
|
|
17
|
+
* @inheritdoc
|
|
18
|
+
* @param data - The data containing the entry to be updated.
|
|
19
|
+
*/
|
|
20
|
+
constructor(data: UpdateEntryData, id?: string, timestamp?: number, version?: string, fromRemote?: boolean);
|
|
21
|
+
/**
|
|
22
|
+
* Executes the command to update the entry in the vault.
|
|
23
|
+
* @inheritdoc
|
|
24
|
+
* @throws {InvalidCommandError} If the command data is invalid.
|
|
25
|
+
*/
|
|
26
|
+
execute(mediator: TwoFaLibMediator): Promise<void>;
|
|
27
|
+
/**
|
|
28
|
+
* @inheritdoc
|
|
29
|
+
* @throws {InvalidCommandError} If the original entry is not available.
|
|
30
|
+
*/
|
|
31
|
+
createUndoCommand(): Command;
|
|
32
|
+
/**
|
|
33
|
+
* Validates the command data.
|
|
34
|
+
* @returns True if the command data is valid, false otherwise.
|
|
35
|
+
*/
|
|
36
|
+
validate(): boolean;
|
|
37
|
+
}
|
|
38
|
+
export default UpdateEntryCommand;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { InvalidCommandError } from '../../TwoFALibError.mjs';
|
|
2
|
+
import Command from '../BaseCommand.mjs';
|
|
3
|
+
/**
|
|
4
|
+
* Represents a command that when executed updates an entry in the vault.
|
|
5
|
+
*/
|
|
6
|
+
class UpdateEntryCommand extends Command {
|
|
7
|
+
/**
|
|
8
|
+
* Creates a new UpdateEntryCommand instance.
|
|
9
|
+
* @inheritdoc
|
|
10
|
+
* @param data - The data containing the entry to be updated.
|
|
11
|
+
*/
|
|
12
|
+
constructor(data, id, timestamp, version, fromRemote = false) {
|
|
13
|
+
super('UpdateEntry', data, id, timestamp, version, fromRemote);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Executes the command to update the entry in the vault.
|
|
17
|
+
* @inheritdoc
|
|
18
|
+
* @throws {InvalidCommandError} If the command data is invalid.
|
|
19
|
+
*/
|
|
20
|
+
async execute(mediator) {
|
|
21
|
+
if (!this.validate()) {
|
|
22
|
+
throw new InvalidCommandError('Invalid UpdateEntry command');
|
|
23
|
+
}
|
|
24
|
+
const vault = mediator.getComponent('vaultDataManager');
|
|
25
|
+
this.originalEntry = vault.getFullEntry(this.data.entryId);
|
|
26
|
+
await vault.updateEntry(this.data.updatedEntry);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* @inheritdoc
|
|
30
|
+
* @throws {InvalidCommandError} If the original entry is not available.
|
|
31
|
+
*/
|
|
32
|
+
createUndoCommand() {
|
|
33
|
+
if (!this.originalEntry) {
|
|
34
|
+
throw new InvalidCommandError('Cannot create undo command, original entry not available');
|
|
35
|
+
}
|
|
36
|
+
return new UpdateEntryCommand({
|
|
37
|
+
entryId: this.data.entryId,
|
|
38
|
+
oldEntry: this.data.updatedEntry,
|
|
39
|
+
updatedEntry: this.data.oldEntry,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Validates the command data.
|
|
44
|
+
* @returns True if the command data is valid, false otherwise.
|
|
45
|
+
*/
|
|
46
|
+
validate() {
|
|
47
|
+
// TODO: write a complete validate function
|
|
48
|
+
return Boolean(this.data.entryId);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
export default UpdateEntryCommand;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type CryptoLib from '../../interfaces/CryptoLib.mjs';
|
|
2
|
+
import type { Encrypted, EncryptedPrivateKey, EncryptedSymmetricKey, Passphrase, PassphraseHash, PrivateKey, PublicKey, Salt, SymmetricKey, SyncKey } from '../../interfaces/CryptoLib.mjs';
|
|
3
|
+
/**
|
|
4
|
+
* Create a password hash
|
|
5
|
+
* @param salt - The salt to use
|
|
6
|
+
* @param passphrase - The passphrase to hash
|
|
7
|
+
* @returns The calculated password hash
|
|
8
|
+
*/
|
|
9
|
+
export declare const generatePassphraseHash: (salt: Salt, passphrase: string) => Promise<PassphraseHash>;
|
|
10
|
+
/**
|
|
11
|
+
* @inheritdoc
|
|
12
|
+
*/
|
|
13
|
+
declare class BrowserCryptoLib implements CryptoLib {
|
|
14
|
+
/**
|
|
15
|
+
* @inheritdoc
|
|
16
|
+
*/
|
|
17
|
+
getRandomBytes(count: number): Promise<Uint8Array>;
|
|
18
|
+
/**
|
|
19
|
+
* @inheritdoc
|
|
20
|
+
*/
|
|
21
|
+
createKeys(passphrase: Passphrase): Promise<{
|
|
22
|
+
privateKey: PrivateKey;
|
|
23
|
+
symmetricKey: SymmetricKey;
|
|
24
|
+
encryptedPrivateKey: EncryptedPrivateKey;
|
|
25
|
+
encryptedSymmetricKey: EncryptedSymmetricKey;
|
|
26
|
+
salt: Salt;
|
|
27
|
+
publicKey: PublicKey;
|
|
28
|
+
}>;
|
|
29
|
+
/**
|
|
30
|
+
* @inheritdoc
|
|
31
|
+
*/
|
|
32
|
+
encryptKeys(privateKey: PrivateKey, symmetricKey: SymmetricKey, salt: Salt, passphrase: Passphrase): Promise<{
|
|
33
|
+
encryptedPrivateKey: EncryptedPrivateKey;
|
|
34
|
+
encryptedSymmetricKey: EncryptedSymmetricKey;
|
|
35
|
+
}>;
|
|
36
|
+
/**
|
|
37
|
+
* @inheritdoc
|
|
38
|
+
*/
|
|
39
|
+
decryptKeys(encryptedPrivateKey: EncryptedPrivateKey, encryptedSymmetricKey: EncryptedSymmetricKey, salt: Salt, passphrase: Passphrase): Promise<{
|
|
40
|
+
privateKey: PrivateKey;
|
|
41
|
+
publicKey: PublicKey;
|
|
42
|
+
symmetricKey: SymmetricKey;
|
|
43
|
+
}>;
|
|
44
|
+
/**
|
|
45
|
+
* @inheritdoc
|
|
46
|
+
*/
|
|
47
|
+
encrypt<T extends string>(publicKey: PublicKey, plainText: T): Promise<Encrypted<T>>;
|
|
48
|
+
/**
|
|
49
|
+
* @inheritdoc
|
|
50
|
+
*/
|
|
51
|
+
decrypt<T extends string>(privateKey: PrivateKey, encryptedText: Encrypted<T>): Promise<T>;
|
|
52
|
+
/**
|
|
53
|
+
* @inheritdoc
|
|
54
|
+
*/
|
|
55
|
+
encryptSymmetric<T extends string>(symmetricKey: SymmetricKey, plainText: T): Promise<Encrypted<T>>;
|
|
56
|
+
/**
|
|
57
|
+
* @inheritdoc
|
|
58
|
+
*/
|
|
59
|
+
decryptSymmetric<T extends string>(symmetricKey: SymmetricKey, encryptedText: Encrypted<T>): Promise<T>;
|
|
60
|
+
/**
|
|
61
|
+
* @inheritdoc
|
|
62
|
+
*/
|
|
63
|
+
createSymmetricKey(): Promise<SymmetricKey>;
|
|
64
|
+
/**
|
|
65
|
+
* @inheritdoc
|
|
66
|
+
*/
|
|
67
|
+
createSyncKey(sharedKey: Uint8Array, salt: string): Promise<SyncKey>;
|
|
68
|
+
private encryptPrivateKey;
|
|
69
|
+
private decryptPrivateKey;
|
|
70
|
+
private createKeyPair;
|
|
71
|
+
private getPublicKeyFromPrivateKey;
|
|
72
|
+
}
|
|
73
|
+
export default BrowserCryptoLib;
|