Skip to content

Commit 61b00aa

Browse files
committed
Apply code review
1 parent 7b42969 commit 61b00aa

7 files changed

Lines changed: 55 additions & 10 deletions

File tree

src/Platforms/SecureFolderFS.UI/ServiceImplementation/ResourceLocalizationService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public ResourceLocalizationService()
6363
{
6464
try
6565
{
66-
return ResourceManager.GetString(resourceKey);
66+
return ResourceManager.GetString(resourceKey, CurrentCulture);
6767
}
6868
catch (Exception ex)
6969
{
Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,58 @@
11
using System.Collections.Generic;
22
using SecureFolderFS.Shared.ComponentModel;
3+
using SecureFolderFS.Shared.Extensions;
34

45
namespace SecureFolderFS.Sdk.AppModels
56
{
7+
/// <summary>
8+
/// Represents a model that stores session credentials of vaults.
9+
/// </summary>
610
public class PersistedCredentialsModel
711
{
12+
private readonly Dictionary<string, IKeyUsage> _credentials;
13+
14+
/// <summary>
15+
/// Gets the read-only collection of credentials.
16+
/// </summary>
17+
public IReadOnlyDictionary<string, IKeyUsage> Credentials => _credentials;
18+
19+
/// <summary>
20+
/// A single instance of <see cref="PersistedCredentialsModel"/>.
21+
/// </summary>
822
public static PersistedCredentialsModel Instance { get; } = new();
923

24+
public PersistedCredentialsModel()
25+
{
26+
_credentials = new();
27+
}
28+
1029
/// <summary>
11-
/// Gets the dictionary of credentials associated with a vault ID.
30+
/// Adds a new key-value pair to the credential store or updates the value for an existing key.
1231
/// </summary>
13-
public Dictionary<string, IKeyUsage> Credentials { get; } = new();
32+
/// <param name="vaultId">The identifier for the vault associated with the key.</param>
33+
/// <param name="keyUsage">An implementation of <see cref="IKeyUsage"/> that contains the key to be added or updated.</param>
34+
public void SetOrAdd(string vaultId, IKeyUsage keyUsage)
35+
{
36+
_credentials.Get(vaultId)?.Dispose();
37+
_credentials[vaultId] = keyUsage;
38+
}
39+
40+
/// <summary>
41+
/// Removes a key-value pair associated with the specified vault identifier from the credential store.
42+
/// </summary>
43+
/// <param name="vaultId">The identifier for the vault whose associated key-value pair is to be removed.</param>
44+
/// <returns>
45+
/// <c>true</c> if the key-value pair was successfully removed; otherwise, <c>false</c>.
46+
/// </returns>
47+
public bool Remove(string vaultId)
48+
{
49+
if (_credentials.Remove(vaultId, out var value))
50+
{
51+
value.Dispose();
52+
return true;
53+
}
54+
55+
return false;
56+
}
1457
}
1558
}

src/Sdk/SecureFolderFS.Sdk/ViewModels/Controls/Authentication/PersistedAuthenticationViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ protected override async Task ProvideCredentialsAsync(CancellationToken cancella
5757
{
5858
if (!PersistedCredentialsModel.Instance.Credentials.TryGetValue(_vaultId, out var credentials))
5959
{
60-
PersistedCredentialsModel.Instance.Credentials.Remove(_vaultId);
60+
PersistedCredentialsModel.Instance.Remove(_vaultId);
6161
credentials = ManagedKey.Empty;
6262
}
6363

src/Sdk/SecureFolderFS.Sdk/ViewModels/Controls/LoginViewModel.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ private async Task DiscardSavedCredentialsAsync(CancellationToken cancellationTo
174174
var vaultOptions = await VaultService.GetVaultOptionsAsync(_vaultFolder, cancellationToken);
175175
if (!string.IsNullOrEmpty(vaultOptions.VaultId))
176176
{
177-
PersistedCredentialsModel.Instance.Credentials.Remove(vaultOptions.VaultId);
177+
PersistedCredentialsModel.Instance.Remove(vaultOptions.VaultId);
178178
AreCredentialsSaved = false;
179179

180180
_loginSequence?.Dispose();
@@ -184,8 +184,9 @@ private async Task DiscardSavedCredentialsAsync(CancellationToken cancellationTo
184184
RestartLoginProcess();
185185
}
186186
}
187-
catch (Exception)
187+
catch (Exception ex)
188188
{
189+
CurrentViewModel = new ErrorViewModel(Result.Failure(ex));
189190
}
190191
}
191192

@@ -215,7 +216,7 @@ private async Task<bool> TryUnlockAsync(CancellationToken cancellationToken = de
215216
if (_loginViewMode is LoginViewType.Full or LoginViewType.Constrained && ShouldSaveCredentials && !AreCredentialsSaved)
216217
{
217218
var keySequenceCopy = KeySequence.Key.ToArray();
218-
PersistedCredentialsModel.Instance.Credentials[_vaultOptions.VaultId] = SecureKey.TakeOwnership(keySequenceCopy);
219+
PersistedCredentialsModel.Instance.SetOrAdd(_vaultOptions.VaultId, SecureKey.TakeOwnership(keySequenceCopy));
219220
}
220221

221222
VaultUnlocked?.Invoke(this, new(unlockContract, _vaultFolder, false));

src/Sdk/SecureFolderFS.Sdk/ViewModels/Controls/Widgets/Health/HealthWidgetViewModel.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ private async void HealthViewModel_StateChanged(object? sender, EventArgs e)
9999

100100
// Persist last scanned date, and severity
101101
var serialized = await StreamSerializer.Instance.TrySerializeToStringAsync(new HealthDataModel(lastScanDate, HealthViewModel.Severity)).ConfigureAwait(false);
102-
await WidgetModel.SetWidgetDataAsync(serialized).ConfigureAwait(false);
102+
if (!string.IsNullOrEmpty(serialized))
103+
await WidgetModel.SetWidgetDataAsync(serialized).ConfigureAwait(false);
103104
}
104105

105106
/// <inheritdoc/>

src/Sdk/SecureFolderFS.Sdk/ViewModels/Views/Credentials/CredentialsConfirmationViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ await VaultManagerService.ModifyAuthenticationAsync(_vaultFolder, UnlockContract
106106
}, cancellationToken);
107107

108108
if (!string.IsNullOrEmpty(configuredOptions.VaultId))
109-
PersistedCredentialsModel.Instance.Credentials.Remove(configuredOptions.VaultId);
109+
PersistedCredentialsModel.Instance.Remove(configuredOptions.VaultId);
110110

111111
// Revoke (invalidate) old configured credentials if those are different from newly configured ones.
112112
// If both are the same, the authentication method should override the old ones; otherwise we would be deleting

src/Sdk/SecureFolderFS.Sdk/ViewModels/Views/Credentials/CredentialsResetViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public async Task ConfirmAsync(CancellationToken cancellationToken)
7070

7171
await VaultManagerService.ModifyAuthenticationAsync(_vaultFolder, _unlockContract, key, newOptions, cancellationToken);
7272
if (!string.IsNullOrEmpty(configuredOptions.VaultId))
73-
PersistedCredentialsModel.Instance.Credentials.Remove(configuredOptions.VaultId);
73+
PersistedCredentialsModel.Instance.Remove(configuredOptions.VaultId);
7474
}
7575

7676
private void RegisterViewModel_CredentialsProvided(object? sender, CredentialsProvidedEventArgs e)

0 commit comments

Comments
 (0)