mirror of
https://github.com/BeyondDimension/SteamTools.git
synced 2026-10-25 03:09:54 +08:00
security fix
This commit is contained in:
+84
@@ -0,0 +1,84 @@
|
||||
// https://github.com/xamarin/Essentials/blob/1.6.1/Xamarin.Essentials/SecureStorage/SecureStorage.uwp.cs
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.Application.Models;
|
||||
using System.Runtime.InteropServices.WindowsRuntime;
|
||||
using System.Runtime.Versioning;
|
||||
using System.Threading.Tasks;
|
||||
using Windows.Security.Cryptography.DataProtection;
|
||||
|
||||
namespace System.Application.Services.Implementation
|
||||
{
|
||||
/// <summary>
|
||||
/// https://docs.microsoft.com/en-us/uwp/api/windows.security.cryptography.dataprotection.dataprotectionprovider?view=winrt-10240
|
||||
/// </summary>
|
||||
[SupportedOSPlatform("Windows10.0.10240.0")]
|
||||
internal sealed class Windows10SecurityService : SecurityService
|
||||
{
|
||||
public Windows10SecurityService(IOptions<AppSettings> options) : base(options)
|
||||
{
|
||||
}
|
||||
|
||||
static async Task<byte[]?> E__(byte[]? bytes)
|
||||
{
|
||||
if (bytes == null) return null;
|
||||
|
||||
// LOCAL=user and LOCAL=machine do not require enterprise auth capability
|
||||
var provider = new DataProtectionProvider("LOCAL=user");
|
||||
|
||||
var buffer = await provider.ProtectAsync(bytes.AsBuffer());
|
||||
|
||||
var encBytes = buffer.ToArray();
|
||||
|
||||
return encBytes;
|
||||
}
|
||||
|
||||
static async Task<byte[]?> D__(byte[]? encBytes)
|
||||
{
|
||||
if (encBytes == null) return null;
|
||||
|
||||
var provider = new DataProtectionProvider();
|
||||
|
||||
var buffer = await provider.UnprotectAsync(encBytes.AsBuffer());
|
||||
|
||||
return buffer.ToArray();
|
||||
}
|
||||
|
||||
public override Task<byte[]?> E(string? value)
|
||||
{
|
||||
var value2 = E_(value);
|
||||
return E__(value2);
|
||||
}
|
||||
|
||||
public override Task<byte[]?> EB(byte[]? value)
|
||||
{
|
||||
var value2 = EB_(value);
|
||||
return E__(value2);
|
||||
}
|
||||
|
||||
public override async Task<string?> D(byte[]? value)
|
||||
{
|
||||
try
|
||||
{
|
||||
var value2 = await D__(value);
|
||||
return D_(value2);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task<byte[]?> DB(byte[]? value)
|
||||
{
|
||||
try
|
||||
{
|
||||
var value2 = await D__(value);
|
||||
return DB_(value2);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
-67
@@ -1,67 +0,0 @@
|
||||
// https://github.com/xamarin/Essentials/blob/1.6.1/Xamarin.Essentials/SecureStorage/SecureStorage.shared.cs
|
||||
using System.Application;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace System.Security
|
||||
{
|
||||
/// <summary>
|
||||
/// https://docs.microsoft.com/en-us/uwp/api/windows.security.cryptography.dataprotection.dataprotectionprovider?view=winrt-10240
|
||||
/// </summary>
|
||||
internal sealed partial class Windows10DesktopClientStorage : IStorage
|
||||
{
|
||||
// Special Alias that is only used for Secure Storage. All others should use: Preferences.GetPrivatePreferencesSharedName
|
||||
internal static readonly string Alias = $"{BuildConfig.APPLICATION_ID}.xamarinessentials";
|
||||
|
||||
public string? Get(string key)
|
||||
{
|
||||
Func<Task<string?>> func = () => GetAsync(key);
|
||||
var result = func.RunSync();
|
||||
return result;
|
||||
}
|
||||
|
||||
public Task<string?> GetAsync(string key)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(key))
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
return PlatformGetAsync(key);
|
||||
}
|
||||
|
||||
public void Set(string key, string? value)
|
||||
{
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
Remove(key);
|
||||
return;
|
||||
}
|
||||
Func<Task> func = () => SetAsync(key, value);
|
||||
func.RunSync();
|
||||
}
|
||||
|
||||
public Task SetAsync(string key, string? value)
|
||||
{
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
Remove(key);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
return SetAsync_(key, value);
|
||||
}
|
||||
|
||||
static Task SetAsync_(string key, string? value)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(key))
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
|
||||
if (value == null)
|
||||
throw new ArgumentNullException(nameof(value));
|
||||
|
||||
return PlatformSetAsync(key, value);
|
||||
}
|
||||
|
||||
public bool Remove(string key) => PlatformRemove(key);
|
||||
|
||||
public Task<bool> RemoveAsync(string key) => Task.FromResult(Remove(key));
|
||||
|
||||
public static void RemoveAll() => PlatformRemoveAll();
|
||||
}
|
||||
}
|
||||
-73
@@ -1,73 +0,0 @@
|
||||
// https://github.com/xamarin/Essentials/blob/1.6.1/Xamarin.Essentials/SecureStorage/SecureStorage.uwp.cs
|
||||
#pragma warning disable CA1416 // 验证平台兼容性
|
||||
using System.Runtime.InteropServices.WindowsRuntime;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Windows.Security.Cryptography.DataProtection;
|
||||
using Windows.Storage;
|
||||
|
||||
namespace System.Security
|
||||
{
|
||||
partial class Windows10DesktopClientStorage
|
||||
{
|
||||
static async Task<string?> PlatformGetAsync(string key)
|
||||
{
|
||||
var settings = GetSettings(Alias);
|
||||
|
||||
if (settings.Values[key] is not byte[] encBytes)
|
||||
return null;
|
||||
|
||||
var provider = new DataProtectionProvider();
|
||||
|
||||
var buffer = await provider.UnprotectAsync(encBytes.AsBuffer());
|
||||
|
||||
return Encoding.UTF8.GetString(buffer.ToArray());
|
||||
}
|
||||
|
||||
static async Task PlatformSetAsync(string key, string data)
|
||||
{
|
||||
var settings = GetSettings(Alias);
|
||||
|
||||
var bytes = Encoding.UTF8.GetBytes(data);
|
||||
|
||||
// LOCAL=user and LOCAL=machine do not require enterprise auth capability
|
||||
var provider = new DataProtectionProvider("LOCAL=user");
|
||||
|
||||
var buffer = await provider.ProtectAsync(bytes.AsBuffer());
|
||||
|
||||
var encBytes = buffer.ToArray();
|
||||
|
||||
settings.Values[key] = encBytes;
|
||||
}
|
||||
|
||||
static bool PlatformRemove(string key)
|
||||
{
|
||||
var settings = GetSettings(Alias);
|
||||
|
||||
if (settings.Values.ContainsKey(key))
|
||||
{
|
||||
settings.Values.Remove(key);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static void PlatformRemoveAll()
|
||||
{
|
||||
var settings = GetSettings(Alias);
|
||||
|
||||
settings.Values.Clear();
|
||||
}
|
||||
|
||||
static ApplicationDataContainer GetSettings(string name)
|
||||
{
|
||||
var localSettings = ApplicationData.Current.LocalSettings;
|
||||
|
||||
if (!localSettings.Containers.ContainsKey(name))
|
||||
localSettings.CreateContainer(name, ApplicationDataCreateDisposition.Always);
|
||||
return localSettings.Containers[name];
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore CA1416 // 验证平台兼容性
|
||||
@@ -19,10 +19,10 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||
{
|
||||
services.AddSingleton<IDesktopPlatformService, WindowsDesktopPlatformServiceImpl>();
|
||||
services.AddSingleton<ISteamworksLocalApiService, SteamworksLocalApiServiceImpl>();
|
||||
//if (Environment.OSVersion.Version.Major >= 10)
|
||||
//{
|
||||
// services.AddSingleton<IStorage, Windows10DesktopClientStorage>();
|
||||
//}
|
||||
if (Environment.OSVersion.Version.Major >= 10)
|
||||
{
|
||||
services.AddSingleton<ISecurityService, Windows10SecurityService>();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
-9
@@ -8,11 +8,6 @@
|
||||
<UseWindowsForms>false</UseWindowsForms>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="Security\Windows10DesktopClientStorage.cs" />
|
||||
<Compile Remove="Security\Windows10DesktopClientStorage.uwp.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="..\System.Common.CoreLib\Properties\AssemblyInfo.cs">
|
||||
<Link>Properties\AssemblyInfo.cs</Link>
|
||||
@@ -32,8 +27,4 @@
|
||||
<ProjectReference Include="..\System.Application.SteamTools.Client.Desktop\System.Application.SteamTools.Client.Desktop.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Security\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user