favalib 0.0.6 → 0.0.8
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/TwoFaLib.d.mts +15 -19
- package/build/TwoFaLib.mjs +20 -26
- package/build/interfaces/Events.d.mts +1 -5
- package/build/interfaces/SaveFunction.d.mts +3 -0
- package/build/interfaces/SaveFunction.mjs +1 -0
- package/build/main.d.mts +2 -1
- package/build/subclasses/PersistentStorageManager.d.mts +16 -4
- package/build/subclasses/PersistentStorageManager.mjs +34 -8
- package/build/subclasses/StorageOperationsManager.d.mts +36 -0
- package/build/subclasses/StorageOperationsManager.mjs +42 -0
- package/build/subclasses/VaultDataManager.d.mts +1 -0
- package/build/subclasses/VaultDataManager.mjs +8 -0
- package/build/subclasses/VaultOperationsManager.d.mts +2 -2
- package/build/subclasses/VaultOperationsManager.mjs +2 -2
- package/build/utils/creationUtils.d.mts +3 -1
- package/build/utils/creationUtils.mjs +10 -7
- package/package.json +8 -8
package/build/TwoFaLib.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { TypedEventTarget } from 'typescript-event-target';
|
|
2
2
|
import type CryptoLib from './interfaces/CryptoLib.mjs';
|
|
3
|
-
import type { EncryptedPrivateKey, EncryptedSymmetricKey,
|
|
3
|
+
import type { EncryptedPrivateKey, EncryptedSymmetricKey, PrivateKey, PublicKey, Salt, SymmetricKey } from './interfaces/CryptoLib.mjs';
|
|
4
4
|
import type { DeviceFriendlyName, DeviceId, DeviceType } from './interfaces/SyncTypes.mjs';
|
|
5
5
|
import type { TwoFaLibEventMapEvents } from './interfaces/Events.mjs';
|
|
6
6
|
import type { PassphraseExtraDict } from './interfaces/PassphraseExtraDict.js';
|
|
@@ -8,6 +8,8 @@ import type { Vault, VaultSyncState } from './interfaces/Vault.mjs';
|
|
|
8
8
|
import SyncManager from './subclasses/SyncManager.mjs';
|
|
9
9
|
import ExportImportManager from './subclasses/ExportImportManager.mjs';
|
|
10
10
|
import VaultOperationsManager from './subclasses/VaultOperationsManager.mjs';
|
|
11
|
+
import SaveFunction from './interfaces/SaveFunction.mjs';
|
|
12
|
+
import StorageOperationsManager from './subclasses/StorageOperationsManager.mjs';
|
|
11
13
|
/**
|
|
12
14
|
* The Two-Factor Library, this is the main entry point.
|
|
13
15
|
*/
|
|
@@ -33,17 +35,27 @@ declare class TwoFaLib extends TypedEventTarget<TwoFaLibEventMapEvents> {
|
|
|
33
35
|
* @param publicKey - The public key of the device.
|
|
34
36
|
* @param deviceId - A unique identifier for this device.
|
|
35
37
|
* @param vault - The vault data (entries)
|
|
38
|
+
* @param saveFunction - The function to save the data.
|
|
36
39
|
* @param syncState - The state of the sync, includes the serverUrl
|
|
37
40
|
* @returns A promise that resolves when initialization is complete.
|
|
38
41
|
* @throws {InitializationError} If some parameter has an invalid value
|
|
39
42
|
* @throws {AuthenticationError} If the provided passphrase is incorrect.
|
|
40
43
|
*/
|
|
41
|
-
constructor(deviceType: DeviceType, cryptoLib: CryptoLib, passphraseExtraDict: PassphraseExtraDict, privateKey: PrivateKey, symmetricKey: SymmetricKey, encryptedPrivateKey: EncryptedPrivateKey, encryptedSymmetricKey: EncryptedSymmetricKey, salt: Salt, publicKey: PublicKey, deviceId: DeviceId, vault?: Vault, syncState?: VaultSyncState);
|
|
44
|
+
constructor(deviceType: DeviceType, cryptoLib: CryptoLib, passphraseExtraDict: PassphraseExtraDict, privateKey: PrivateKey, symmetricKey: SymmetricKey, encryptedPrivateKey: EncryptedPrivateKey, encryptedSymmetricKey: EncryptedSymmetricKey, salt: Salt, publicKey: PublicKey, deviceId: DeviceId, vault?: Vault, saveFunction?: SaveFunction, syncState?: VaultSyncState);
|
|
45
|
+
/**
|
|
46
|
+
* @returns The persistent storage manager instance which can be used to store data.
|
|
47
|
+
*/
|
|
48
|
+
private get persistentStorageManager();
|
|
42
49
|
/**
|
|
43
50
|
* Gives access to vault operations.
|
|
44
|
-
* @returns The vault manager instance which can be used to perform operations on the vault.
|
|
51
|
+
* @returns The vault operations manager instance which can be used to perform operations on the vault.
|
|
45
52
|
*/
|
|
46
53
|
get vault(): VaultOperationsManager;
|
|
54
|
+
/**
|
|
55
|
+
* Gives access to storage operations.
|
|
56
|
+
* @returns The storage operations manager instance which can be used to perform operations on the vault.
|
|
57
|
+
*/
|
|
58
|
+
get storage(): StorageOperationsManager;
|
|
47
59
|
/**
|
|
48
60
|
* Gives access to export/import operations.
|
|
49
61
|
* @returns The export/import manager instance which can be used to export and import vaults.
|
|
@@ -54,22 +66,6 @@ declare class TwoFaLib extends TypedEventTarget<TwoFaLibEventMapEvents> {
|
|
|
54
66
|
* @returns The sync manager instance which can be used to sync the vault with a server or null if none was initialized.
|
|
55
67
|
*/
|
|
56
68
|
get sync(): SyncManager | null;
|
|
57
|
-
/**
|
|
58
|
-
* @returns The persistent storage manager instance which can be used to store data.
|
|
59
|
-
*/
|
|
60
|
-
private get persistentStorage();
|
|
61
|
-
/**
|
|
62
|
-
* Forces a save of the persistent storage.
|
|
63
|
-
*/
|
|
64
|
-
forceSave(): Promise<void>;
|
|
65
|
-
/**
|
|
66
|
-
* Changes the library's passphrase.
|
|
67
|
-
* @param oldPassphrase - The current passphrase.
|
|
68
|
-
* @param newPassphrase - The new passphrase to set.
|
|
69
|
-
* @returns A promise that resolves when the passphrase change is complete.
|
|
70
|
-
* @throws {AuthenticationError} If the provided old passphrase is incorrect.
|
|
71
|
-
*/
|
|
72
|
-
changePassphrase(oldPassphrase: Passphrase, newPassphrase: Passphrase): Promise<void>;
|
|
73
69
|
/**
|
|
74
70
|
* Sets a server url, this will allow syncing with the server.
|
|
75
71
|
* @param serverUrl - The server url.
|
package/build/TwoFaLib.mjs
CHANGED
|
@@ -9,6 +9,7 @@ import PersistentStorageManager from './subclasses/PersistentStorageManager.mjs'
|
|
|
9
9
|
import VaultDataManager from './subclasses/VaultDataManager.mjs';
|
|
10
10
|
import VaultOperationsManager from './subclasses/VaultOperationsManager.mjs';
|
|
11
11
|
import CommandManager from './subclasses/CommandManager.mjs';
|
|
12
|
+
import StorageOperationsManager from './subclasses/StorageOperationsManager.mjs';
|
|
12
13
|
/**
|
|
13
14
|
* The Two-Factor Library, this is the main entry point.
|
|
14
15
|
*/
|
|
@@ -28,12 +29,13 @@ class TwoFaLib extends TypedEventTarget {
|
|
|
28
29
|
* @param publicKey - The public key of the device.
|
|
29
30
|
* @param deviceId - A unique identifier for this device.
|
|
30
31
|
* @param vault - The vault data (entries)
|
|
32
|
+
* @param saveFunction - The function to save the data.
|
|
31
33
|
* @param syncState - The state of the sync, includes the serverUrl
|
|
32
34
|
* @returns A promise that resolves when initialization is complete.
|
|
33
35
|
* @throws {InitializationError} If some parameter has an invalid value
|
|
34
36
|
* @throws {AuthenticationError} If the provided passphrase is incorrect.
|
|
35
37
|
*/
|
|
36
|
-
constructor(deviceType, cryptoLib, passphraseExtraDict, privateKey, symmetricKey, encryptedPrivateKey, encryptedSymmetricKey, salt, publicKey, deviceId, vault, syncState) {
|
|
38
|
+
constructor(deviceType, cryptoLib, passphraseExtraDict, privateKey, symmetricKey, encryptedPrivateKey, encryptedSymmetricKey, salt, publicKey, deviceId, vault, saveFunction, syncState) {
|
|
37
39
|
super();
|
|
38
40
|
this.deviceFriendlyName = '';
|
|
39
41
|
if (!deviceType) {
|
|
@@ -57,11 +59,12 @@ class TwoFaLib extends TypedEventTarget {
|
|
|
57
59
|
['libraryLoader', new LibraryLoader(cryptoLib)],
|
|
58
60
|
[
|
|
59
61
|
'persistentStorageManager',
|
|
60
|
-
new PersistentStorageManager(this.mediator, passphraseExtraDict, deviceId, privateKey, symmetricKey, encryptedPrivateKey, encryptedSymmetricKey, salt),
|
|
62
|
+
new PersistentStorageManager(this.mediator, passphraseExtraDict, deviceId, privateKey, symmetricKey, encryptedPrivateKey, encryptedSymmetricKey, salt, saveFunction),
|
|
61
63
|
],
|
|
62
64
|
['vaultDataManager', new VaultDataManager(this.mediator)],
|
|
63
65
|
['commandManager', new CommandManager(this.mediator)],
|
|
64
66
|
['vaultOperationsManager', new VaultOperationsManager(this.mediator)],
|
|
67
|
+
['storageOperationsManager', new StorageOperationsManager(this.mediator)],
|
|
65
68
|
[
|
|
66
69
|
'exportImportManager',
|
|
67
70
|
new ExportImportManager(this.mediator, passphraseExtraDict),
|
|
@@ -92,13 +95,26 @@ class TwoFaLib extends TypedEventTarget {
|
|
|
92
95
|
};
|
|
93
96
|
this.addEventListener(TwoFaLibEvent.Ready, handleReadyEvent);
|
|
94
97
|
}
|
|
98
|
+
/**
|
|
99
|
+
* @returns The persistent storage manager instance which can be used to store data.
|
|
100
|
+
*/
|
|
101
|
+
get persistentStorageManager() {
|
|
102
|
+
return this.mediator.getComponent('persistentStorageManager');
|
|
103
|
+
}
|
|
95
104
|
/**
|
|
96
105
|
* Gives access to vault operations.
|
|
97
|
-
* @returns The vault manager instance which can be used to perform operations on the vault.
|
|
106
|
+
* @returns The vault operations manager instance which can be used to perform operations on the vault.
|
|
98
107
|
*/
|
|
99
108
|
get vault() {
|
|
100
109
|
return this.mediator.getComponent('vaultOperationsManager');
|
|
101
110
|
}
|
|
111
|
+
/**
|
|
112
|
+
* Gives access to storage operations.
|
|
113
|
+
* @returns The storage operations manager instance which can be used to perform operations on the vault.
|
|
114
|
+
*/
|
|
115
|
+
get storage() {
|
|
116
|
+
return this.mediator.getComponent('storageOperationsManager');
|
|
117
|
+
}
|
|
102
118
|
/**
|
|
103
119
|
* Gives access to export/import operations.
|
|
104
120
|
* @returns The export/import manager instance which can be used to export and import vaults.
|
|
@@ -116,28 +132,6 @@ class TwoFaLib extends TypedEventTarget {
|
|
|
116
132
|
}
|
|
117
133
|
return this.mediator.getComponent('syncManager');
|
|
118
134
|
}
|
|
119
|
-
/**
|
|
120
|
-
* @returns The persistent storage manager instance which can be used to store data.
|
|
121
|
-
*/
|
|
122
|
-
get persistentStorage() {
|
|
123
|
-
return this.mediator.getComponent('persistentStorageManager');
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Forces a save of the persistent storage.
|
|
127
|
-
*/
|
|
128
|
-
async forceSave() {
|
|
129
|
-
await this.persistentStorage.save();
|
|
130
|
-
}
|
|
131
|
-
/**
|
|
132
|
-
* Changes the library's passphrase.
|
|
133
|
-
* @param oldPassphrase - The current passphrase.
|
|
134
|
-
* @param newPassphrase - The new passphrase to set.
|
|
135
|
-
* @returns A promise that resolves when the passphrase change is complete.
|
|
136
|
-
* @throws {AuthenticationError} If the provided old passphrase is incorrect.
|
|
137
|
-
*/
|
|
138
|
-
async changePassphrase(oldPassphrase, newPassphrase) {
|
|
139
|
-
return this.persistentStorage.changePassphrase(oldPassphrase, newPassphrase);
|
|
140
|
-
}
|
|
141
135
|
/**
|
|
142
136
|
* Sets a server url, this will allow syncing with the server.
|
|
143
137
|
* @param serverUrl - The server url.
|
|
@@ -182,7 +176,7 @@ class TwoFaLib extends TypedEventTarget {
|
|
|
182
176
|
this.mediator.unRegisterComponent('syncManager');
|
|
183
177
|
this.mediator.registerComponent('syncManager', newSyncManager);
|
|
184
178
|
// save
|
|
185
|
-
await this.
|
|
179
|
+
await this.persistentStorageManager.save();
|
|
186
180
|
}
|
|
187
181
|
/**
|
|
188
182
|
* Dispatches a library event.
|
|
@@ -1,12 +1,8 @@
|
|
|
1
1
|
import type { EmptyObject } from 'type-fest';
|
|
2
2
|
import type { TwoFaLibEvent } from '../TwoFaLibEvent.mjs';
|
|
3
|
-
import type { LockedRepresentationString } from './Vault.mjs';
|
|
4
3
|
import type { ConnectionStatus } from '../subclasses/SyncManager.mjs';
|
|
5
|
-
export interface ChangedEvent {
|
|
6
|
-
newLockedRepresentationString: LockedRepresentationString;
|
|
7
|
-
}
|
|
8
4
|
export interface TwoFaLibEventMap {
|
|
9
|
-
[TwoFaLibEvent.Changed]:
|
|
5
|
+
[TwoFaLibEvent.Changed]: EmptyObject;
|
|
10
6
|
[TwoFaLibEvent.LoadedFromLockedRepresentation]: EmptyObject;
|
|
11
7
|
[TwoFaLibEvent.ConnectToExistingVaultFinished]: EmptyObject;
|
|
12
8
|
[TwoFaLibEvent.ConnectionToSyncServerStatusChanged]: {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/build/main.d.mts
CHANGED
|
@@ -5,8 +5,9 @@ import type CryptoLib from './interfaces/CryptoLib.mjs';
|
|
|
5
5
|
import type { Encrypted, EncryptedPrivateKey, EncryptedSymmetricKey, EncryptedPublicKey, PrivateKey, SymmetricKey, PublicKey, Passphrase, Salt } from './interfaces/CryptoLib.mjs';
|
|
6
6
|
import type { PublicSyncDevice, DeviceId, DeviceType, DeviceFriendlyName } from './interfaces/SyncTypes.mjs';
|
|
7
7
|
import type { EncryptedVaultStateString, LockedRepresentationString } from './interfaces/Vault.mjs';
|
|
8
|
+
import type SaveFunction from './interfaces/SaveFunction.mjs';
|
|
8
9
|
import { TwoFALibError, InitializationError, AuthenticationError, EntryNotFoundError, TokenGenerationError } from './TwoFALibError.mjs';
|
|
9
10
|
import { TwoFaLibEvent } from './TwoFaLibEvent.mjs';
|
|
10
11
|
import { getTwoFaLibVaultCreationUtils } from './utils/creationUtils.mjs';
|
|
11
12
|
export { TwoFaLib, TwoFALibError, getTwoFaLibVaultCreationUtils, InitializationError, AuthenticationError, EntryNotFoundError, TokenGenerationError, TwoFaLibEvent, };
|
|
12
|
-
export type { Entry, EntryId, NewEntry, EntryMeta, EntryMetaWithToken, EntryType, TotpPayload, Token, EncryptedVaultStateString, LockedRepresentationString, CryptoLib, Encrypted, EncryptedPrivateKey, EncryptedPublicKey, EncryptedSymmetricKey, PrivateKey, SymmetricKey, PublicKey, Passphrase, Salt, DeviceId, DeviceType, DeviceFriendlyName, PublicSyncDevice, };
|
|
13
|
+
export type { Entry, EntryId, NewEntry, EntryMeta, EntryMetaWithToken, EntryType, TotpPayload, Token, EncryptedVaultStateString, LockedRepresentationString, CryptoLib, Encrypted, EncryptedPrivateKey, EncryptedPublicKey, EncryptedSymmetricKey, PrivateKey, SymmetricKey, PublicKey, Passphrase, Salt, DeviceId, DeviceType, DeviceFriendlyName, PublicSyncDevice, SaveFunction, };
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import type { EncryptedPrivateKey, EncryptedSymmetricKey, Passphrase, PrivateKey, Salt, SymmetricKey } from '../interfaces/CryptoLib.mjs';
|
|
2
|
-
import { EncryptedVaultStateString
|
|
2
|
+
import { EncryptedVaultStateString } from '../interfaces/Vault.mjs';
|
|
3
3
|
import type TwoFaLibMediator from '../TwoFaLibMediator.mjs';
|
|
4
4
|
import type { DeviceId } from '../interfaces/SyncTypes.mjs';
|
|
5
5
|
import type { PassphraseExtraDict } from '../interfaces/PassphraseExtraDict.js';
|
|
6
|
+
import SaveFunction from '../interfaces/SaveFunction.mjs';
|
|
6
7
|
/**
|
|
7
8
|
* Manages all storage of data that should be persistent.
|
|
8
9
|
*/
|
|
@@ -15,7 +16,9 @@ declare class PersistentStorageManager {
|
|
|
15
16
|
private encryptedPrivateKey;
|
|
16
17
|
private encryptedSymmetricKey;
|
|
17
18
|
private salt;
|
|
19
|
+
private saveFunction?;
|
|
18
20
|
static readonly storageVersion = 1;
|
|
21
|
+
private savePromise;
|
|
19
22
|
/**
|
|
20
23
|
* Constructs a new instance of PersistentStorageManager.
|
|
21
24
|
* @param mediator - The mediator for accessing other components.
|
|
@@ -26,12 +29,12 @@ declare class PersistentStorageManager {
|
|
|
26
29
|
* @param encryptedPrivateKey - The encrypted private key
|
|
27
30
|
* @param encryptedSymmetricKey - The encrypted symmetric key
|
|
28
31
|
* @param salt - The salt used for key derivation.
|
|
32
|
+
* @param saveFunction - The function to save the data.
|
|
29
33
|
*/
|
|
30
|
-
constructor(mediator: TwoFaLibMediator, passphraseExtraDict: PassphraseExtraDict, deviceId: DeviceId, privateKey: PrivateKey, symmetricKey: SymmetricKey, encryptedPrivateKey: EncryptedPrivateKey, encryptedSymmetricKey: EncryptedSymmetricKey, salt: Salt);
|
|
34
|
+
constructor(mediator: TwoFaLibMediator, passphraseExtraDict: PassphraseExtraDict, deviceId: DeviceId, privateKey: PrivateKey, symmetricKey: SymmetricKey, encryptedPrivateKey: EncryptedPrivateKey, encryptedSymmetricKey: EncryptedSymmetricKey, salt: Salt, saveFunction?: SaveFunction | undefined);
|
|
31
35
|
private get cryptoLib();
|
|
32
36
|
private get vaultDataManager();
|
|
33
37
|
private get syncManager();
|
|
34
|
-
private get dispatchLibEvent();
|
|
35
38
|
/**
|
|
36
39
|
* Retrieves an encrypted representation of the library's current state.
|
|
37
40
|
* This can be used for secure storage or transmission of the library's data.
|
|
@@ -46,12 +49,21 @@ declare class PersistentStorageManager {
|
|
|
46
49
|
* @returns A promise that resolves with a json encoded string of
|
|
47
50
|
* the partially encrypted library's data.
|
|
48
51
|
*/
|
|
49
|
-
getLockedRepresentation
|
|
52
|
+
private getLockedRepresentation;
|
|
53
|
+
/**
|
|
54
|
+
* Sets the save function for the library.
|
|
55
|
+
* @param saveFunction - The save function to set.
|
|
56
|
+
*/
|
|
57
|
+
setSaveFunction(saveFunction: SaveFunction): void;
|
|
50
58
|
/**
|
|
51
59
|
* Saves the current state of the library.
|
|
52
60
|
* @returns A promise that resolves when the save operation is complete.
|
|
53
61
|
*/
|
|
54
62
|
save(): Promise<void>;
|
|
63
|
+
/**
|
|
64
|
+
* Internal method to perform the actual save operation.
|
|
65
|
+
*/
|
|
66
|
+
private performSave;
|
|
55
67
|
/**
|
|
56
68
|
* Validates the provided passphrase against the current library passphrase.
|
|
57
69
|
* @param salt - The salt used for key derivation.
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { AuthenticationError } from '../TwoFALibError.mjs';
|
|
2
|
-
import { TwoFaLibEvent } from '../TwoFaLibEvent.mjs';
|
|
3
2
|
import TwoFaLib from '../TwoFaLib.mjs';
|
|
4
3
|
import { validatePassphraseStrength } from '../utils/creationUtils.mjs';
|
|
5
4
|
/**
|
|
@@ -17,8 +16,9 @@ class PersistentStorageManager {
|
|
|
17
16
|
* @param encryptedPrivateKey - The encrypted private key
|
|
18
17
|
* @param encryptedSymmetricKey - The encrypted symmetric key
|
|
19
18
|
* @param salt - The salt used for key derivation.
|
|
19
|
+
* @param saveFunction - The function to save the data.
|
|
20
20
|
*/
|
|
21
|
-
constructor(mediator, passphraseExtraDict, deviceId, privateKey, symmetricKey, encryptedPrivateKey, encryptedSymmetricKey, salt) {
|
|
21
|
+
constructor(mediator, passphraseExtraDict, deviceId, privateKey, symmetricKey, encryptedPrivateKey, encryptedSymmetricKey, salt, saveFunction) {
|
|
22
22
|
this.mediator = mediator;
|
|
23
23
|
this.passphraseExtraDict = passphraseExtraDict;
|
|
24
24
|
this.deviceId = deviceId;
|
|
@@ -27,6 +27,8 @@ class PersistentStorageManager {
|
|
|
27
27
|
this.encryptedPrivateKey = encryptedPrivateKey;
|
|
28
28
|
this.encryptedSymmetricKey = encryptedSymmetricKey;
|
|
29
29
|
this.salt = salt;
|
|
30
|
+
this.saveFunction = saveFunction;
|
|
31
|
+
this.savePromise = null;
|
|
30
32
|
}
|
|
31
33
|
get cryptoLib() {
|
|
32
34
|
return this.mediator.getComponent('libraryLoader').getCryptoLib();
|
|
@@ -40,9 +42,6 @@ class PersistentStorageManager {
|
|
|
40
42
|
}
|
|
41
43
|
return this.mediator.getComponent('syncManager');
|
|
42
44
|
}
|
|
43
|
-
get dispatchLibEvent() {
|
|
44
|
-
return this.mediator.getComponent('dispatchLibEvent');
|
|
45
|
-
}
|
|
46
45
|
/**
|
|
47
46
|
* Retrieves an encrypted representation of the library's current state.
|
|
48
47
|
* This can be used for secure storage or transmission of the library's data.
|
|
@@ -82,15 +81,42 @@ class PersistentStorageManager {
|
|
|
82
81
|
};
|
|
83
82
|
return JSON.stringify(lockedRepresentation);
|
|
84
83
|
}
|
|
84
|
+
/**
|
|
85
|
+
* Sets the save function for the library.
|
|
86
|
+
* @param saveFunction - The save function to set.
|
|
87
|
+
*/
|
|
88
|
+
setSaveFunction(saveFunction) {
|
|
89
|
+
this.saveFunction = saveFunction;
|
|
90
|
+
}
|
|
85
91
|
/**
|
|
86
92
|
* Saves the current state of the library.
|
|
87
93
|
* @returns A promise that resolves when the save operation is complete.
|
|
88
94
|
*/
|
|
89
95
|
async save() {
|
|
96
|
+
if (this.saveFunction) {
|
|
97
|
+
// If a save is already in progress, wait for it to complete
|
|
98
|
+
if (this.savePromise) {
|
|
99
|
+
await this.savePromise;
|
|
100
|
+
// recurse
|
|
101
|
+
await this.save();
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
// Start a new save operation
|
|
105
|
+
this.savePromise = this.performSave();
|
|
106
|
+
try {
|
|
107
|
+
await this.savePromise;
|
|
108
|
+
}
|
|
109
|
+
finally {
|
|
110
|
+
this.savePromise = null;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Internal method to perform the actual save operation.
|
|
116
|
+
*/
|
|
117
|
+
async performSave() {
|
|
90
118
|
const lockedRepresentation = await this.getLockedRepresentation();
|
|
91
|
-
this.
|
|
92
|
-
newLockedRepresentationString: lockedRepresentation,
|
|
93
|
-
});
|
|
119
|
+
await this.saveFunction(lockedRepresentation);
|
|
94
120
|
}
|
|
95
121
|
/**
|
|
96
122
|
* Validates the provided passphrase against the current library passphrase.
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type TwoFaLibMediator from '../TwoFaLibMediator.mjs';
|
|
2
|
+
import type { Passphrase } from '../interfaces/CryptoLib.mjs';
|
|
3
|
+
import type SaveFunction from '../interfaces/SaveFunction.mjs';
|
|
4
|
+
/**
|
|
5
|
+
* Manages the public operations related to the vault storage
|
|
6
|
+
*/
|
|
7
|
+
declare class StorageOperationsManager {
|
|
8
|
+
private readonly mediator;
|
|
9
|
+
/**
|
|
10
|
+
* Constructs a new instance of StorageOperationsManager.
|
|
11
|
+
* @param mediator - The mediator for accessing other components.
|
|
12
|
+
*/
|
|
13
|
+
constructor(mediator: TwoFaLibMediator);
|
|
14
|
+
/**
|
|
15
|
+
* @returns The persistent storage manager instance which can be used to store data.
|
|
16
|
+
*/
|
|
17
|
+
get persistentStorage(): import("./PersistentStorageManager.mjs").default;
|
|
18
|
+
/**
|
|
19
|
+
* Forces a save.
|
|
20
|
+
*/
|
|
21
|
+
forceSave(): Promise<void>;
|
|
22
|
+
/**
|
|
23
|
+
* Changes the library's passphrase.
|
|
24
|
+
* @param oldPassphrase - The current passphrase.
|
|
25
|
+
* @param newPassphrase - The new passphrase to set.
|
|
26
|
+
* @returns A promise that resolves when the passphrase change is complete.
|
|
27
|
+
* @throws {AuthenticationError} If the provided old passphrase is incorrect.
|
|
28
|
+
*/
|
|
29
|
+
changePassphrase(oldPassphrase: Passphrase, newPassphrase: Passphrase): Promise<void>;
|
|
30
|
+
/**
|
|
31
|
+
* Sets the save function for the library.
|
|
32
|
+
* @param saveFunction - The save function to set.
|
|
33
|
+
*/
|
|
34
|
+
setSaveFunction(saveFunction: SaveFunction): void;
|
|
35
|
+
}
|
|
36
|
+
export default StorageOperationsManager;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Manages the public operations related to the vault storage
|
|
3
|
+
*/
|
|
4
|
+
class StorageOperationsManager {
|
|
5
|
+
/**
|
|
6
|
+
* Constructs a new instance of StorageOperationsManager.
|
|
7
|
+
* @param mediator - The mediator for accessing other components.
|
|
8
|
+
*/
|
|
9
|
+
constructor(mediator) {
|
|
10
|
+
this.mediator = mediator;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* @returns The persistent storage manager instance which can be used to store data.
|
|
14
|
+
*/
|
|
15
|
+
get persistentStorage() {
|
|
16
|
+
return this.mediator.getComponent('persistentStorageManager');
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Forces a save.
|
|
20
|
+
*/
|
|
21
|
+
async forceSave() {
|
|
22
|
+
return this.persistentStorage.save();
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Changes the library's passphrase.
|
|
26
|
+
* @param oldPassphrase - The current passphrase.
|
|
27
|
+
* @param newPassphrase - The new passphrase to set.
|
|
28
|
+
* @returns A promise that resolves when the passphrase change is complete.
|
|
29
|
+
* @throws {AuthenticationError} If the provided old passphrase is incorrect.
|
|
30
|
+
*/
|
|
31
|
+
async changePassphrase(oldPassphrase, newPassphrase) {
|
|
32
|
+
return this.persistentStorage.changePassphrase(oldPassphrase, newPassphrase);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Sets the save function for the library.
|
|
36
|
+
* @param saveFunction - The save function to set.
|
|
37
|
+
*/
|
|
38
|
+
setSaveFunction(saveFunction) {
|
|
39
|
+
this.persistentStorage.setSaveFunction(saveFunction);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export default StorageOperationsManager;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { TOTP } from 'totp-generator';
|
|
2
2
|
import { EntryNotFoundError, TokenGenerationError } from '../TwoFALibError.mjs';
|
|
3
|
+
import { TwoFaLibEvent } from '../TwoFaLibEvent.mjs';
|
|
3
4
|
import { SUPPORTED_ALGORITHMS, } from '../utils/constants.mjs';
|
|
4
5
|
/**
|
|
5
6
|
* Manages the data within the vault. This class should only be used internally
|
|
@@ -17,6 +18,9 @@ class VaultDataManager {
|
|
|
17
18
|
get persistentStorageManager() {
|
|
18
19
|
return this.mediator.getComponent('persistentStorageManager');
|
|
19
20
|
}
|
|
21
|
+
get dispatchLibEvent() {
|
|
22
|
+
return this.mediator.getComponent('dispatchLibEvent');
|
|
23
|
+
}
|
|
20
24
|
/**
|
|
21
25
|
* @returns The number of entries in the vault.
|
|
22
26
|
*/
|
|
@@ -80,6 +84,7 @@ class VaultDataManager {
|
|
|
80
84
|
return;
|
|
81
85
|
}
|
|
82
86
|
this.vault.push(entry);
|
|
87
|
+
this.dispatchLibEvent(TwoFaLibEvent.Changed);
|
|
83
88
|
if (saveAfter) {
|
|
84
89
|
await this.persistentStorageManager.save();
|
|
85
90
|
}
|
|
@@ -94,6 +99,7 @@ class VaultDataManager {
|
|
|
94
99
|
if (index === -1)
|
|
95
100
|
throw new EntryNotFoundError('Entry not found');
|
|
96
101
|
this.vault.splice(index, 1);
|
|
102
|
+
this.dispatchLibEvent(TwoFaLibEvent.Changed);
|
|
97
103
|
await this.persistentStorageManager.save();
|
|
98
104
|
}
|
|
99
105
|
/**
|
|
@@ -108,6 +114,7 @@ class VaultDataManager {
|
|
|
108
114
|
if (index === -1)
|
|
109
115
|
throw new EntryNotFoundError('Entry not found');
|
|
110
116
|
this.vault[index] = updatedEntry;
|
|
117
|
+
this.dispatchLibEvent(TwoFaLibEvent.Changed);
|
|
111
118
|
await this.persistentStorageManager.save();
|
|
112
119
|
}
|
|
113
120
|
/**
|
|
@@ -116,6 +123,7 @@ class VaultDataManager {
|
|
|
116
123
|
*/
|
|
117
124
|
replaceVault(newVault) {
|
|
118
125
|
this.vault = newVault;
|
|
126
|
+
this.dispatchLibEvent(TwoFaLibEvent.Changed);
|
|
119
127
|
}
|
|
120
128
|
}
|
|
121
129
|
export default VaultDataManager;
|
|
@@ -4,7 +4,7 @@ import type TwoFaLibMediator from '../TwoFaLibMediator.mjs';
|
|
|
4
4
|
/**
|
|
5
5
|
* Manages the public operations related to the vault, including adding, deleting, and updating entries.
|
|
6
6
|
*/
|
|
7
|
-
declare class
|
|
7
|
+
declare class VaultOperationsManager {
|
|
8
8
|
private readonly mediator;
|
|
9
9
|
/**
|
|
10
10
|
* Constructs a new instance of VaultManager.
|
|
@@ -88,4 +88,4 @@ declare class VaultManager {
|
|
|
88
88
|
*/
|
|
89
89
|
updateEntry(entryId: EntryId, updates: Partial<Omit<Entry, 'id'>>): Promise<EntryMeta>;
|
|
90
90
|
}
|
|
91
|
-
export default
|
|
91
|
+
export default VaultOperationsManager;
|
|
@@ -14,7 +14,7 @@ const getMetaForEntry = (entry) => ({
|
|
|
14
14
|
/**
|
|
15
15
|
* Manages the public operations related to the vault, including adding, deleting, and updating entries.
|
|
16
16
|
*/
|
|
17
|
-
class
|
|
17
|
+
class VaultOperationsManager {
|
|
18
18
|
/**
|
|
19
19
|
* Constructs a new instance of VaultManager.
|
|
20
20
|
* @param mediator - The mediator for accessing other components.
|
|
@@ -160,4 +160,4 @@ class VaultManager {
|
|
|
160
160
|
return getMetaForEntry(updatedEntry);
|
|
161
161
|
}
|
|
162
162
|
}
|
|
163
|
-
export default
|
|
163
|
+
export default VaultOperationsManager;
|
|
@@ -6,6 +6,7 @@ import TwoFaLib from '../TwoFaLib.mjs';
|
|
|
6
6
|
import LibraryLoader from '../subclasses/LibraryLoader.mjs';
|
|
7
7
|
import type { LockedRepresentationString } from '../interfaces/Vault.mjs';
|
|
8
8
|
import type { PassphraseExtraDict } from '../interfaces/PassphraseExtraDict.js';
|
|
9
|
+
import SaveFunction from '../interfaces/SaveFunction.mjs';
|
|
9
10
|
/**
|
|
10
11
|
* Evaluates the strength of a passphrase.
|
|
11
12
|
* @param libraryLoader - An instance of LibraryLoader.
|
|
@@ -27,10 +28,11 @@ export declare const validatePassphraseStrength: (libraryLoader: LibraryLoader,
|
|
|
27
28
|
* @param cryptoLib - An instance of CryptoLib that is compatible with the environment.
|
|
28
29
|
* @param deviceType - A unique identifier for this device type (e.g. 2fa-cli).
|
|
29
30
|
* @param passphraseExtraDict - Additional words to be used for passphrase strength evaluation.
|
|
31
|
+
* @param saveFunction - The function to save the data.
|
|
30
32
|
* @param serverUrl - The server URL for syncing.
|
|
31
33
|
* @returns An object with methods to evaluate passphrase strength and create a new TwoFaLib vault.
|
|
32
34
|
*/
|
|
33
|
-
export declare const getTwoFaLibVaultCreationUtils: (cryptoLib: CryptoLib, deviceType: DeviceType, passphraseExtraDict: PassphraseExtraDict, serverUrl?: string) => {
|
|
35
|
+
export declare const getTwoFaLibVaultCreationUtils: (cryptoLib: CryptoLib, deviceType: DeviceType, passphraseExtraDict: PassphraseExtraDict, saveFunction?: SaveFunction, serverUrl?: string) => {
|
|
34
36
|
getPassphraseStrength: (passphrase: Passphrase, passphraseExtraDict: PassphraseExtraDict) => Promise<ZxcvbnResult>;
|
|
35
37
|
createNewTwoFaLibVault: (passphrase: Passphrase) => Promise<{
|
|
36
38
|
twoFaLib: TwoFaLib;
|
|
@@ -56,15 +56,16 @@ export const validatePassphraseStrength = async (libraryLoader, passphrase, pass
|
|
|
56
56
|
* @param deviceType - A unique identifier for the device type e.g. 2fa-cli.
|
|
57
57
|
* @param serverUrl - The server URL for syncing.
|
|
58
58
|
* @param passphraseExtraDict - Additional words to be used for passphrase strength evaluation.
|
|
59
|
+
* @param saveFunction - The function to save the data.
|
|
59
60
|
* @param passphrase - The passphrase to be used to encrypt the private key.
|
|
60
61
|
* @returns Promise resolving to an object containing the newly created TwoFaLib instance and related data.
|
|
61
62
|
*/
|
|
62
|
-
const createNewTwoFaLibVault = async (libraryLoader, deviceType, serverUrl, passphraseExtraDict, passphrase) => {
|
|
63
|
+
const createNewTwoFaLibVault = async (libraryLoader, deviceType, serverUrl, passphraseExtraDict, saveFunction, passphrase) => {
|
|
63
64
|
const cryptoLib = libraryLoader.getCryptoLib();
|
|
64
65
|
const { publicKey, privateKey, symmetricKey, encryptedPrivateKey, encryptedSymmetricKey, salt, } = await cryptoLib.createKeys(passphrase);
|
|
65
66
|
await validatePassphraseStrength(libraryLoader, passphrase, passphraseExtraDict);
|
|
66
67
|
const deviceId = genUuidV4();
|
|
67
|
-
const twoFaLib = new TwoFaLib(deviceType, cryptoLib, passphraseExtraDict, privateKey, symmetricKey, encryptedPrivateKey, encryptedSymmetricKey, salt, publicKey, deviceId, [], {
|
|
68
|
+
const twoFaLib = new TwoFaLib(deviceType, cryptoLib, passphraseExtraDict, privateKey, symmetricKey, encryptedPrivateKey, encryptedSymmetricKey, salt, publicKey, deviceId, [], saveFunction, {
|
|
68
69
|
serverUrl,
|
|
69
70
|
devices: [],
|
|
70
71
|
commandSendQueue: [],
|
|
@@ -82,12 +83,13 @@ const createNewTwoFaLibVault = async (libraryLoader, deviceType, serverUrl, pass
|
|
|
82
83
|
* @param libraryLoader - An instance of LibraryLoader.
|
|
83
84
|
* @param deviceType - A unique identifier for this device type (e.g. 2fa-cli).
|
|
84
85
|
* @param passphraseExtraDict - Additional words to be used for passphrase strength evaluation.
|
|
86
|
+
* @param saveFunction - The function to save the data.
|
|
85
87
|
* @param lockedRepresentationString - The string representation of the locked library state representation.
|
|
86
88
|
* @param passphrase - The passphrase for decrypting the keys.
|
|
87
89
|
* @returns A promise that resolves when loading is complete.
|
|
88
90
|
* @throws {InitializationError} If loading fails due to invalid or corrupted data.
|
|
89
91
|
*/
|
|
90
|
-
const loadTwoFaLibFromLockedRepesentation = async (libraryLoader, deviceType, passphraseExtraDict, lockedRepresentationString, passphrase) => {
|
|
92
|
+
const loadTwoFaLibFromLockedRepesentation = async (libraryLoader, deviceType, passphraseExtraDict, saveFunction, lockedRepresentationString, passphrase) => {
|
|
91
93
|
const cryptoLib = libraryLoader.getCryptoLib();
|
|
92
94
|
const lockedRepresentation = JSON.parse(lockedRepresentationString);
|
|
93
95
|
if (!lockedRepresentation ||
|
|
@@ -105,21 +107,22 @@ const loadTwoFaLibFromLockedRepesentation = async (libraryLoader, deviceType, pa
|
|
|
105
107
|
!vaultState.sync?.devices) {
|
|
106
108
|
throw new InitializationError('encryptedVaultState is incomplete or corrupted');
|
|
107
109
|
}
|
|
108
|
-
return new TwoFaLib(deviceType, cryptoLib, passphraseExtraDict, privateKey, symmetricKey, lockedRepresentation.encryptedPrivateKey, lockedRepresentation.encryptedSymmetricKey, lockedRepresentation.salt, publicKey, vaultState.deviceId, vaultState.vault, vaultState.sync);
|
|
110
|
+
return new TwoFaLib(deviceType, cryptoLib, passphraseExtraDict, privateKey, symmetricKey, lockedRepresentation.encryptedPrivateKey, lockedRepresentation.encryptedSymmetricKey, lockedRepresentation.salt, publicKey, vaultState.deviceId, vaultState.vault, saveFunction, vaultState.sync);
|
|
109
111
|
};
|
|
110
112
|
/**
|
|
111
113
|
* Returns utility functions useful in creating a new twoFaLib vault
|
|
112
114
|
* @param cryptoLib - An instance of CryptoLib that is compatible with the environment.
|
|
113
115
|
* @param deviceType - A unique identifier for this device type (e.g. 2fa-cli).
|
|
114
116
|
* @param passphraseExtraDict - Additional words to be used for passphrase strength evaluation.
|
|
117
|
+
* @param saveFunction - The function to save the data.
|
|
115
118
|
* @param serverUrl - The server URL for syncing.
|
|
116
119
|
* @returns An object with methods to evaluate passphrase strength and create a new TwoFaLib vault.
|
|
117
120
|
*/
|
|
118
|
-
export const getTwoFaLibVaultCreationUtils = (cryptoLib, deviceType, passphraseExtraDict, serverUrl) => {
|
|
121
|
+
export const getTwoFaLibVaultCreationUtils = (cryptoLib, deviceType, passphraseExtraDict, saveFunction, serverUrl) => {
|
|
119
122
|
const libraryLoader = new LibraryLoader(cryptoLib);
|
|
120
123
|
return {
|
|
121
124
|
getPassphraseStrength: getPassphraseStrength.bind(null, libraryLoader),
|
|
122
|
-
createNewTwoFaLibVault: createNewTwoFaLibVault.bind(null, libraryLoader, deviceType, serverUrl, passphraseExtraDict),
|
|
123
|
-
loadTwoFaLibFromLockedRepesentation: loadTwoFaLibFromLockedRepesentation.bind(null, libraryLoader, deviceType, passphraseExtraDict),
|
|
125
|
+
createNewTwoFaLibVault: createNewTwoFaLibVault.bind(null, libraryLoader, deviceType, serverUrl, passphraseExtraDict, saveFunction),
|
|
126
|
+
loadTwoFaLibFromLockedRepesentation: loadTwoFaLibFromLockedRepesentation.bind(null, libraryLoader, deviceType, passphraseExtraDict, saveFunction),
|
|
124
127
|
};
|
|
125
128
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "favalib",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.8",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -28,10 +28,10 @@
|
|
|
28
28
|
"@types/qrcode": "^1.5.5",
|
|
29
29
|
"@types/uuid": "^10.0.0",
|
|
30
30
|
"@types/whatwg-url": "^13.0.0",
|
|
31
|
-
"@vitest/coverage-v8": "^3.1.
|
|
32
|
-
"eslint": "^9.
|
|
33
|
-
"type-fest": "^4.
|
|
34
|
-
"vitest": "^3.1.
|
|
31
|
+
"@vitest/coverage-v8": "^3.1.4",
|
|
32
|
+
"eslint": "^9.27.0",
|
|
33
|
+
"type-fest": "^4.41.0",
|
|
34
|
+
"vitest": "^3.1.4",
|
|
35
35
|
"vitest-websocket-mock": "^0.5.0"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
@@ -43,15 +43,15 @@
|
|
|
43
43
|
"jpake-ts": "^1.0.1",
|
|
44
44
|
"jsqr": "^1.4.0",
|
|
45
45
|
"node-forge": "^1.3.1",
|
|
46
|
-
"openpgp": "^6.1.
|
|
46
|
+
"openpgp": "^6.1.1",
|
|
47
47
|
"qrcode": "^1.5.4",
|
|
48
48
|
"totp-generator": "^1.0.0",
|
|
49
49
|
"typescript-event-target": "^1.1.1",
|
|
50
50
|
"uint8array-extras": "^1.4.0",
|
|
51
|
-
"unws": "^0.3.
|
|
51
|
+
"unws": "^0.3.2",
|
|
52
52
|
"uuid": "^11.1.0",
|
|
53
53
|
"whatwg-url": "^14.2.0",
|
|
54
|
-
"ws": "^8.18.
|
|
54
|
+
"ws": "^8.18.2"
|
|
55
55
|
},
|
|
56
56
|
"engines": {
|
|
57
57
|
"node": ">=20"
|