Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor SecureStorage into an interface #4668

Merged
merged 10 commits into from Feb 24, 2022
17 changes: 8 additions & 9 deletions src/Essentials/src/SecureStorage/SecureStorage.android.cs
Expand Up @@ -3,16 +3,15 @@
using Android.Content;
using AndroidX.Security.Crypto;
using Javax.Crypto;
using Microsoft.Maui.Essentials.Implementations;

namespace Microsoft.Maui.Essentials
namespace Microsoft.Maui.Essentials.Implementations
{
public static partial class SecureStorage
public partial class SecureStorageImplementation : ISecureStorage
{

static readonly object locker = new object();
static string Alias => SecureStorage.Alias;

static Task<string> PlatformGetAsync(string key)
public Task<string> GetAsync(string key)
{
return Task.Run(() =>
{
Expand Down Expand Up @@ -40,7 +39,7 @@ static Task<string> PlatformGetAsync(string key)
});
}

static Task PlatformSetAsync(string key, string data)
public Task SetAsync(string key, string data)
{
return Task.Run(() =>
{
Expand All @@ -62,7 +61,7 @@ static Task PlatformSetAsync(string key, string data)
});
}

static bool PlatformRemove(string key)
public bool Remove(string key)
{
lock (locker)
{
Expand All @@ -75,7 +74,7 @@ static bool PlatformRemove(string key)
return true;
}

static void PlatformRemoveAll()
public void RemoveAll()
{
lock (locker)
{
Expand All @@ -86,7 +85,7 @@ static void PlatformRemoveAll()
}
}

static ISharedPreferences GetEncryptedSharedPreferences()
ISharedPreferences GetEncryptedSharedPreferences()
{
var context = Application.Context;

Expand Down
Expand Up @@ -9,8 +9,24 @@ namespace Microsoft.Maui.Essentials
{
public static partial class SecureStorage
{
public static SecAccessible DefaultAccessible { get; set; } =
SecAccessible.AfterFirstUnlock;
public static SecAccessible DefaultAccessible
{
get { return Implementations.SecureStorageImplementation.DefaultAccessible; }
set { Implementations.SecureStorageImplementation.DefaultAccessible = value; }
}
Redth marked this conversation as resolved.
Show resolved Hide resolved

public static Task SetAsync(string key, string value, SecAccessible accessible) =>
Implementations.SecureStorageImplementation.SetAsync(key, value, accessible);
Redth marked this conversation as resolved.
Show resolved Hide resolved
}
}

namespace Microsoft.Maui.Essentials.Implementations
mattleibow marked this conversation as resolved.
Show resolved Hide resolved
{
public partial class SecureStorageImplementation : ISecureStorage
{
static string Alias => SecureStorage.Alias;

public static SecAccessible DefaultAccessible { get; set; }

public static Task SetAsync(string key, string value, SecAccessible accessible)
{
Expand All @@ -26,25 +42,25 @@ public static Task SetAsync(string key, string value, SecAccessible accessible)
return Task.CompletedTask;
}

static Task<string> PlatformGetAsync(string key)
public Task<string> GetAsync(string key)
{
var kc = new KeyChain(DefaultAccessible);
var value = kc.ValueForKey(key, Alias);

return Task.FromResult(value);
}

static Task PlatformSetAsync(string key, string data) =>
public Task SetAsync(string key, string data) =>
SetAsync(key, data, DefaultAccessible);

static bool PlatformRemove(string key)
public bool Remove(string key)
{
var kc = new KeyChain(DefaultAccessible);

return kc.Remove(key, Alias);
}

static void PlatformRemoveAll()
public void RemoveAll()
{
var kc = new KeyChain(DefaultAccessible);

Expand Down
12 changes: 6 additions & 6 deletions src/Essentials/src/SecureStorage/SecureStorage.netstandard.cs
@@ -1,20 +1,20 @@
using System.Threading.Tasks;

namespace Microsoft.Maui.Essentials
namespace Microsoft.Maui.Essentials.Implementations
{
/// <include file="../../docs/Microsoft.Maui.Essentials/SecureStorage.xml" path="Type[@FullName='Microsoft.Maui.Essentials.SecureStorage']/Docs" />
public partial class SecureStorage
public partial class SecureStorageImplementation : ISecureStorage
{
static Task<string> PlatformGetAsync(string key) =>
public Task<string> GetAsync(string key) =>
throw ExceptionUtils.NotSupportedOrImplementedException;

static Task PlatformSetAsync(string key, string data) =>
public Task SetAsync(string key, string data) =>
throw ExceptionUtils.NotSupportedOrImplementedException;

static bool PlatformRemove(string key) =>
public bool Remove(string key) =>
throw ExceptionUtils.NotSupportedOrImplementedException;

static void PlatformRemoveAll() =>
public void RemoveAll() =>
throw ExceptionUtils.NotSupportedOrImplementedException;
}
}
33 changes: 29 additions & 4 deletions src/Essentials/src/SecureStorage/SecureStorage.shared.cs
@@ -1,21 +1,34 @@
#nullable enable
using System;
using System.ComponentModel;
using System.Threading.Tasks;
using Microsoft.Maui.Essentials.Implementations;

namespace Microsoft.Maui.Essentials
{
public interface ISecureStorage
{
Task<string> GetAsync(string key);
Task SetAsync(string key, string value);
bool Remove(string key);
void RemoveAll();
}

/// <include file="../../docs/Microsoft.Maui.Essentials/SecureStorage.xml" path="Type[@FullName='Microsoft.Maui.Essentials.SecureStorage']/Docs" />
public static partial class SecureStorage
{
#if !NETSTANDARD
// Special Alias that is only used for Secure Storage. All others should use: Preferences.GetPrivatePreferencesSharedName
internal static readonly string Alias = Preferences.GetPrivatePreferencesSharedName("preferences");
#endif

/// <include file="../../docs/Microsoft.Maui.Essentials/SecureStorage.xml" path="//Member[@MemberName='GetAsync']/Docs" />
public static Task<string> GetAsync(string key)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key));

return PlatformGetAsync(key);
return Current.GetAsync(key);
}

/// <include file="../../docs/Microsoft.Maui.Essentials/SecureStorage.xml" path="//Member[@MemberName='SetAsync'][0]/Docs" />
Expand All @@ -27,15 +40,27 @@ public static Task SetAsync(string key, string value)
if (value == null)
throw new ArgumentNullException(nameof(value));
Redth marked this conversation as resolved.
Show resolved Hide resolved

return PlatformSetAsync(key, value);
return Current.SetAsync(key, value);
}

/// <include file="../../docs/Microsoft.Maui.Essentials/SecureStorage.xml" path="//Member[@MemberName='Remove']/Docs" />
public static bool Remove(string key)
=> PlatformRemove(key);
=> Current.Remove(key);

/// <include file="../../docs/Microsoft.Maui.Essentials/SecureStorage.xml" path="//Member[@MemberName='RemoveAll']/Docs" />
public static void RemoveAll()
=> PlatformRemoveAll();
=> Current.RemoveAll();

static ISecureStorage? currentImplementation;

[EditorBrowsable(EditorBrowsableState.Never)]
public static ISecureStorage Current =>
currentImplementation ??= new SecureStorageImplementation();

[EditorBrowsable(EditorBrowsableState.Never)]
public static void SetCurrent(ISecureStorage? implementation)
{
currentImplementation = implementation;
}
}
}
12 changes: 6 additions & 6 deletions src/Essentials/src/SecureStorage/SecureStorage.tizen.cs
Expand Up @@ -2,11 +2,11 @@
using System.Threading.Tasks;
using Tizen.Security.SecureRepository;

namespace Microsoft.Maui.Essentials
namespace Microsoft.Maui.Essentials.Implementations
{
public partial class SecureStorage
public partial class SecureStorageImplementation : ISecureStorage
{
static Task<string> PlatformGetAsync(string key)
public Task<string> GetAsync(string key)
{
try
{
Expand All @@ -27,7 +27,7 @@ static Task<string> PlatformGetAsync(string key)
}
}

static Task PlatformSetAsync(string key, string data)
public Task SetAsync(string key, string data)
{
try
{
Expand All @@ -54,7 +54,7 @@ static Task PlatformSetAsync(string key, string data)
}
}

static void PlatformRemoveAll()
public void RemoveAll()
{
try
{
Expand All @@ -69,7 +69,7 @@ static void PlatformRemoveAll()
}
}

static bool PlatformRemove(string key)
public bool Remove(string key)
{
try
{
Expand Down
14 changes: 8 additions & 6 deletions src/Essentials/src/SecureStorage/SecureStorage.uwp.cs
Expand Up @@ -5,11 +5,13 @@
using Windows.Security.Cryptography.DataProtection;
using Windows.Storage;

namespace Microsoft.Maui.Essentials
namespace Microsoft.Maui.Essentials.Implementations
{
public partial class SecureStorage
public partial class SecureStorageImplementation : ISecureStorage
{
static async Task<string> PlatformGetAsync(string key)
static string Alias => SecureStorage.Alias;

public async Task<string> GetAsync(string key)
{
var settings = GetSettings(Alias);

Expand All @@ -25,7 +27,7 @@ static async Task<string> PlatformGetAsync(string key)
return Encoding.UTF8.GetString(buffer.ToArray());
}

static async Task PlatformSetAsync(string key, string data)
public async Task SetAsync(string key, string data)
{
var settings = GetSettings(Alias);

Expand All @@ -41,7 +43,7 @@ static async Task PlatformSetAsync(string key, string data)
settings.Values[key] = encBytes;
}

static bool PlatformRemove(string key)
public bool Remove(string key)
{
var settings = GetSettings(Alias);

Expand All @@ -54,7 +56,7 @@ static bool PlatformRemove(string key)
return false;
}

static void PlatformRemoveAll()
public void RemoveAll()
{
var settings = GetSettings(Alias);

Expand Down