tests: vault locking (#2394)
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
import { ClientDisplayableError } from '@standardnotes/responses'
|
||||
import { ItemManagerInterface } from '../../Item/ItemManagerInterface'
|
||||
import { VaultListingInterface } from '@standardnotes/models'
|
||||
import { MutatorClientInterface } from '../../Mutator/MutatorClientInterface'
|
||||
import { KeySystemKeyManagerInterface } from '../../KeySystem/KeySystemKeyManagerInterface'
|
||||
import { GetVaultItems } from './GetVaultItems'
|
||||
|
||||
export class DeleteVault {
|
||||
constructor(
|
||||
private items: ItemManagerInterface,
|
||||
private mutator: MutatorClientInterface,
|
||||
private keys: KeySystemKeyManagerInterface,
|
||||
private _getVaultItems: GetVaultItems,
|
||||
) {}
|
||||
|
||||
async execute(vault: VaultListingInterface): Promise<ClientDisplayableError | void> {
|
||||
@@ -24,7 +24,7 @@ export class DeleteVault {
|
||||
const itemsKeys = this.keys.getKeySystemItemsKeys(vault.systemIdentifier)
|
||||
await this.mutator.setItemsToBeDeleted(itemsKeys)
|
||||
|
||||
const vaultItems = this.items.itemsBelongingToKeySystem(vault.systemIdentifier)
|
||||
const vaultItems = this._getVaultItems.execute(vault).getValue()
|
||||
await this.mutator.setItemsToBeDeleted(vaultItems)
|
||||
|
||||
await this.mutator.setItemToBeDeleted(vault)
|
||||
|
||||
11
packages/services/src/Domain/Vault/UseCase/GetVaultItems.ts
Normal file
11
packages/services/src/Domain/Vault/UseCase/GetVaultItems.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { Result, SyncUseCaseInterface } from '@standardnotes/domain-core'
|
||||
import { DecryptedItemInterface, ItemContent, VaultListingInterface } from '@standardnotes/models'
|
||||
import { ItemManagerInterface } from '../../Item/ItemManagerInterface'
|
||||
|
||||
export class GetVaultItems implements SyncUseCaseInterface<DecryptedItemInterface[]> {
|
||||
constructor(private items: ItemManagerInterface) {}
|
||||
|
||||
execute(vault: VaultListingInterface): Result<DecryptedItemInterface<ItemContent>[]> {
|
||||
return Result.ok(this.items.items.filter((item) => item.key_system_identifier === vault.systemIdentifier))
|
||||
}
|
||||
}
|
||||
@@ -114,7 +114,7 @@ export class RotateVaultKey implements UseCaseInterface<VaultListingInterface> {
|
||||
return Result.ok()
|
||||
}
|
||||
|
||||
const isOwner = this._isVaultOwner.execute({ sharedVault: params.vault }).getValue()
|
||||
const isOwner = this._isVaultOwner.execute(params.vault).getValue()
|
||||
|
||||
if (!isOwner) {
|
||||
return Result.ok()
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { ValidateVaultPassword } from './../VaultLock/UseCase/ValidateVaultPassword'
|
||||
import { IsVaultOwner } from './../VaultUser/UseCase/IsVaultOwner'
|
||||
import { SendVaultDataChangedMessage } from './../SharedVaults/UseCase/SendVaultDataChangedMessage'
|
||||
import { isClientDisplayableError } from '@standardnotes/responses'
|
||||
import {
|
||||
@@ -5,6 +7,7 @@ import {
|
||||
FileItem,
|
||||
KeySystemIdentifier,
|
||||
KeySystemRootKeyStorageMode,
|
||||
SharedVaultListingInterface,
|
||||
VaultListingInterface,
|
||||
VaultListingMutator,
|
||||
isNote,
|
||||
@@ -48,6 +51,8 @@ export class VaultService
|
||||
private _deleteVault: DeleteVault,
|
||||
private _rotateVaultKey: RotateVaultKey,
|
||||
private _sendVaultDataChangeMessage: SendVaultDataChangedMessage,
|
||||
private _isVaultOwner: IsVaultOwner,
|
||||
private _validateVaultPassword: ValidateVaultPassword,
|
||||
eventBus: InternalEventBusInterface,
|
||||
) {
|
||||
super(eventBus)
|
||||
@@ -238,8 +243,39 @@ export class VaultService
|
||||
throw new Error('Attempting to change vault options on a locked vault')
|
||||
}
|
||||
|
||||
if (!this._isVaultOwner.execute(dto.vault).getValue()) {
|
||||
throw new Error('Third party vault options should be changed via changeThirdPartyVaultStorageOptions')
|
||||
}
|
||||
|
||||
const result = await this._changeVaultKeyOptions.execute(dto)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
async changeThirdPartyVaultStorageOptions(dto: {
|
||||
vault: SharedVaultListingInterface
|
||||
newStorageMode: KeySystemRootKeyStorageMode | undefined
|
||||
vaultPassword: string
|
||||
}): Promise<Result<void>> {
|
||||
if (this.vaultLocks.isVaultLocked(dto.vault)) {
|
||||
throw new Error('Attempting to change vault options on a locked vault')
|
||||
}
|
||||
|
||||
if (this._isVaultOwner.execute(dto.vault).getValue()) {
|
||||
throw new Error('First party vault options should be changed via changeVaultKeyOptions')
|
||||
}
|
||||
|
||||
const validPassword = this._validateVaultPassword.execute(dto.vault, dto.vaultPassword).getValue()
|
||||
if (!validPassword) {
|
||||
return Result.fail('Invalid vault password')
|
||||
}
|
||||
|
||||
const result = await this._changeVaultKeyOptions.execute({
|
||||
vault: dto.vault,
|
||||
newStorageMode: dto.newStorageMode,
|
||||
newPasswordOptions: undefined,
|
||||
})
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import {
|
||||
DecryptedItemInterface,
|
||||
KeySystemIdentifier,
|
||||
KeySystemRootKeyStorageMode,
|
||||
SharedVaultListingInterface,
|
||||
VaultListingInterface,
|
||||
} from '@standardnotes/models'
|
||||
import { AbstractService } from '../Service/AbstractService'
|
||||
@@ -36,5 +37,11 @@ export interface VaultServiceInterface
|
||||
params: { name: string; description: string },
|
||||
): Promise<VaultListingInterface>
|
||||
rotateVaultRootKey(vault: VaultListingInterface, vaultPassword?: string): Promise<void>
|
||||
|
||||
changeVaultKeyOptions(dto: ChangeVaultKeyOptionsDTO): Promise<Result<void>>
|
||||
changeThirdPartyVaultStorageOptions(dto: {
|
||||
vault: SharedVaultListingInterface
|
||||
newStorageMode: KeySystemRootKeyStorageMode | undefined
|
||||
vaultPassword: string
|
||||
}): Promise<Result<void>>
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user