-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathVaultManagerService.cs
More file actions
95 lines (81 loc) · 5.04 KB
/
VaultManagerService.cs
File metadata and controls
95 lines (81 loc) · 5.04 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using OwlCore.Storage;
using SecureFolderFS.Core.Cryptography;
using SecureFolderFS.Core.Routines.Operational;
using SecureFolderFS.Sdk.Services;
using SecureFolderFS.Shared.ComponentModel;
using SecureFolderFS.Shared.Models;
using SecureFolderFS.Storage.Extensions;
namespace SecureFolderFS.UI.ServiceImplementation
{
/// <inheritdoc cref="IVaultManagerService"/>
public class VaultManagerService : IVaultManagerService
{
/// <inheritdoc/>
public virtual async Task<IDisposable> CreateAsync(IFolder vaultFolder, IKeyUsage passkey, VaultOptions vaultOptions, CancellationToken cancellationToken = default)
{
using var creationRoutine = (await VaultRoutines.CreateRoutinesAsync(vaultFolder, StreamSerializer.Instance, cancellationToken)).CreateVault();
await creationRoutine.InitAsync(cancellationToken);
creationRoutine.SetCredentials(passkey);
creationRoutine.SetOptions(vaultOptions);
if (vaultFolder is IModifiableFolder modifiableFolder)
{
var readmeFile = await modifiableFolder.CreateFileAsync(Sdk.Constants.Vault.VAULT_README_FILENAME, true, cancellationToken);
await readmeFile.WriteAllTextAsync(Sdk.Constants.Vault.VAULT_README_MESSAGE, Encoding.UTF8, cancellationToken);
}
return await creationRoutine.FinalizeAsync(cancellationToken);
}
/// <inheritdoc/>
public virtual async Task<IDisposable> UnlockAsync(IFolder vaultFolder, IKeyUsage passkey, CancellationToken cancellationToken = default)
{
var routines = await VaultRoutines.CreateRoutinesAsync(vaultFolder, StreamSerializer.Instance, cancellationToken);
using var unlockRoutine = routines.UnlockVault();
await unlockRoutine.InitAsync(cancellationToken);
unlockRoutine.SetCredentials(passkey);
return await unlockRoutine.FinalizeAsync(cancellationToken);
}
/// <inheritdoc/>
public virtual async Task<IDisposable> RecoverAsync(IFolder vaultFolder, string encodedRecoveryKey, CancellationToken cancellationToken = default)
{
using var recoveryKey = KeyPair.CombineRecoveryKey(encodedRecoveryKey);
var routines = await VaultRoutines.CreateRoutinesAsync(vaultFolder, StreamSerializer.Instance, cancellationToken);
using var recoveryRoutine = routines.RecoverVault();
await recoveryRoutine.InitAsync(cancellationToken);
recoveryRoutine.SetCredentials(recoveryKey);
return await recoveryRoutine.FinalizeAsync(cancellationToken);
}
/// <inheritdoc/>
public virtual async Task ModifyComplementationAsync(IFolder vaultFolder, IDisposable unlockContract, ComplementationCredentials credentials, VaultOptions vaultOptions, CancellationToken cancellationToken = default)
{
using var complementationRoutine = (await VaultRoutines.CreateRoutinesAsync(vaultFolder, StreamSerializer.Instance, cancellationToken)).ModifyComplementation();
await complementationRoutine.InitAsync(cancellationToken);
complementationRoutine.SetUnlockContract(unlockContract);
complementationRoutine.SetOptions(vaultOptions);
complementationRoutine.SetCredentials(credentials, cancellationToken);
using var result = await complementationRoutine.FinalizeAsync(cancellationToken);
}
/// <inheritdoc/>
public virtual async Task ModifyAuthenticationAsync(IFolder vaultFolder, IDisposable unlockContract, IKeyUsage newPasskey, VaultOptions vaultOptions, CancellationToken cancellationToken = default)
{
using var credentialsRoutine = (await VaultRoutines.CreateRoutinesAsync(vaultFolder, StreamSerializer.Instance, cancellationToken)).ModifyCredentials();
await credentialsRoutine.InitAsync(cancellationToken);
credentialsRoutine.SetUnlockContract(unlockContract);
credentialsRoutine.SetOptions(vaultOptions);
credentialsRoutine.SetCredentials(newPasskey);
using var result = await credentialsRoutine.FinalizeAsync(cancellationToken);
}
/// <inheritdoc/>
public virtual async Task ModifyAuthenticationAsync(IFolder vaultFolder, IDisposable unlockContract, IKeyUsage oldPasskey, IKeyUsage newPasskey, VaultOptions vaultOptions, CancellationToken cancellationToken = default)
{
using var credentialsRoutine = (await VaultRoutines.CreateRoutinesAsync(vaultFolder, StreamSerializer.Instance, cancellationToken)).ModifyCredentials();
await credentialsRoutine.InitAsync(cancellationToken);
credentialsRoutine.SetUnlockContract(unlockContract);
credentialsRoutine.SetOptions(vaultOptions);
credentialsRoutine.SetCredentials(oldPasskey, newPasskey, cancellationToken);
using var result = await credentialsRoutine.FinalizeAsync(cancellationToken);
}
}
}