-
-
Notifications
You must be signed in to change notification settings - Fork 436
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: should validate password before deleting vault (#2400)
- Loading branch information
1 parent
43e4e0e
commit 875e8c3
Showing
7 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
packages/services/src/Domain/Vault/UseCase/AuthorizeVaultDeletion.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { VaultListingInterface } from '@standardnotes/models' | ||
import { ProtectionsClientInterface } from '../../Protection/ProtectionClientInterface' | ||
import { VaultLockServiceInterface } from '../../VaultLock/VaultLockServiceInterface' | ||
import { | ||
ChallengeReason, | ||
Challenge, | ||
ChallengePrompt, | ||
ChallengeServiceInterface, | ||
ChallengeValidation, | ||
} from '../../Challenge' | ||
import { ChallengeStrings } from '../../Strings/Messages' | ||
import { ValidateVaultPassword } from '../../VaultLock/UseCase/ValidateVaultPassword' | ||
import { Result, UseCaseInterface } from '@standardnotes/domain-core' | ||
|
||
export class AuthorizeVaultDeletion implements UseCaseInterface<boolean> { | ||
constructor( | ||
private vaultLocks: VaultLockServiceInterface, | ||
private protection: ProtectionsClientInterface, | ||
private challenges: ChallengeServiceInterface, | ||
private _validateVaultPassword: ValidateVaultPassword, | ||
) {} | ||
|
||
async execute(vault: VaultListingInterface): Promise<Result<boolean>> { | ||
if (!this.vaultLocks.isVaultLockable(vault)) { | ||
const authorized = await this.protection.authorizeAction(ChallengeReason.Custom, { | ||
fallBackToAccountPassword: true, | ||
requireAccountPassword: false, | ||
forcePrompt: true, | ||
}) | ||
return Result.ok(authorized) | ||
} | ||
|
||
const challenge = new Challenge( | ||
[new ChallengePrompt(ChallengeValidation.None, undefined, 'Password')], | ||
ChallengeReason.Custom, | ||
true, | ||
ChallengeStrings.DeleteVault(vault.name), | ||
ChallengeStrings.EnterVaultPassword, | ||
) | ||
|
||
return new Promise((resolve) => { | ||
this.challenges.addChallengeObserver(challenge, { | ||
onCancel() { | ||
resolve(Result.ok(false)) | ||
}, | ||
onNonvalidatedSubmit: async (challengeResponse) => { | ||
const value = challengeResponse.getDefaultValue() | ||
if (!value) { | ||
this.challenges.completeChallenge(challenge) | ||
resolve(Result.ok(false)) | ||
return | ||
} | ||
|
||
const password = value.value as string | ||
|
||
const validPassword = this._validateVaultPassword.execute(vault, password).getValue() | ||
if (!validPassword) { | ||
this.challenges.setValidationStatusForChallenge(challenge, value, false) | ||
resolve(Result.ok(false)) | ||
return | ||
} | ||
|
||
this.challenges.completeChallenge(challenge) | ||
resolve(Result.ok(true)) | ||
}, | ||
}) | ||
|
||
void this.challenges.promptForChallengeResponse(challenge) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters