-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathVaultRoutines.cs
More file actions
74 lines (62 loc) · 2.47 KB
/
VaultRoutines.cs
File metadata and controls
74 lines (62 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using OwlCore.Storage;
using SecureFolderFS.Core.Validators;
using SecureFolderFS.Core.VaultAccess;
using SecureFolderFS.Shared.ComponentModel;
using SecureFolderFS.Shared.Extensions;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace SecureFolderFS.Core.Routines.Operational
{
// TODO: Needs docs
public sealed class VaultRoutines
{
private readonly IFolder _vaultFolder;
private readonly IResult _validationResult;
public VaultReader VaultReader { get; }
public VaultWriter VaultWriter { get; }
private VaultRoutines(IFolder vaultFolder, IAsyncSerializer<Stream> serializer, IResult validationResult)
{
_vaultFolder = vaultFolder;
VaultReader = new VaultReader(vaultFolder, serializer);
VaultWriter = new VaultWriter(vaultFolder, serializer);
_validationResult = validationResult;
}
public ICreationRoutine CreateVault()
{
// Only in the case of creation the validation is not triggered
return new CreationRoutine(_vaultFolder, VaultWriter);
}
public ICredentialsRoutine UnlockVault()
{
CheckVaultValidation();
return new UnlockRoutine(VaultReader);
}
public ICredentialsRoutine RecoverVault()
{
CheckVaultValidation();
return new RecoverRoutine(VaultReader);
}
public IModifyCredentialsRoutine ModifyCredentials()
{
CheckVaultValidation();
return new ModifyCredentialsRoutine(VaultReader, VaultWriter);
}
public ModifyComplementationRoutine ModifyComplementation()
{
CheckVaultValidation();
return new ModifyComplementationRoutine(VaultReader, VaultWriter);
}
private void CheckVaultValidation()
{
if (!_validationResult.Successful)
throw _validationResult.Exception ?? new InvalidDataException("Vault is not valid");
}
public static async Task<VaultRoutines> CreateRoutinesAsync(IFolder vaultFolder, IAsyncSerializer<Stream> serializer, CancellationToken cancellationToken = default)
{
var vaultValidator = new VaultValidator(serializer);
var validationResult = await vaultValidator.TryValidateAsync(vaultFolder, cancellationToken);
return new VaultRoutines(vaultFolder, serializer, validationResult);
}
}
}