mirror of
https://github.com/wabbajack-tools/wabbajack.git
synced 2024-08-30 18:42:17 +00:00
Merge pull request #178 from wabbajack-tools/better-key-storage
Rework nexus key caching
This commit is contained in:
commit
8a118fadfa
@ -11,8 +11,11 @@ using System.Net.Http.Headers;
|
|||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Runtime.Serialization.Json;
|
using System.Runtime.Serialization.Json;
|
||||||
using System.Security.Authentication;
|
using System.Security.Authentication;
|
||||||
|
using System.Security.Cryptography;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.WindowsAPICodePack.Shell;
|
||||||
|
using Syroot.Windows.IO;
|
||||||
using Wabbajack.Common;
|
using Wabbajack.Common;
|
||||||
using Wabbajack.Lib.Downloaders;
|
using Wabbajack.Lib.Downloaders;
|
||||||
using WebSocketSharp;
|
using WebSocketSharp;
|
||||||
@ -23,6 +26,7 @@ namespace Wabbajack.Lib.NexusApi
|
|||||||
public class NexusApiClient : ViewModel
|
public class NexusApiClient : ViewModel
|
||||||
{
|
{
|
||||||
private static readonly string API_KEY_CACHE_FILE = "nexus.key_cache";
|
private static readonly string API_KEY_CACHE_FILE = "nexus.key_cache";
|
||||||
|
private static string _additionalEntropy = "vtP2HF6ezg";
|
||||||
|
|
||||||
private readonly HttpClient _httpClient;
|
private readonly HttpClient _httpClient;
|
||||||
|
|
||||||
@ -55,11 +59,31 @@ namespace Wabbajack.Lib.NexusApi
|
|||||||
{
|
{
|
||||||
lock (_getAPIKeyLock)
|
lock (_getAPIKeyLock)
|
||||||
{
|
{
|
||||||
// check if there exists a cached api key
|
// Clean up old location
|
||||||
var fi = new FileInfo(API_KEY_CACHE_FILE);
|
if (File.Exists(API_KEY_CACHE_FILE))
|
||||||
if (fi.Exists)
|
|
||||||
{
|
{
|
||||||
return File.ReadAllText(API_KEY_CACHE_FILE);
|
File.Delete(API_KEY_CACHE_FILE);
|
||||||
|
}
|
||||||
|
|
||||||
|
var cacheFolder = Path.Combine(new KnownFolder(KnownFolderType.LocalAppData).Path, "Wabbajack");
|
||||||
|
if (!Directory.Exists(cacheFolder))
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(cacheFolder);
|
||||||
|
}
|
||||||
|
|
||||||
|
var cacheFile = Path.Combine(cacheFolder, _additionalEntropy);
|
||||||
|
if (File.Exists(cacheFile))
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return Encoding.UTF8.GetString(
|
||||||
|
ProtectedData.Unprotect(File.ReadAllBytes(cacheFile),
|
||||||
|
Encoding.UTF8.GetBytes(_additionalEntropy), DataProtectionScope.CurrentUser));
|
||||||
|
}
|
||||||
|
catch (CryptographicException)
|
||||||
|
{
|
||||||
|
File.Delete(cacheFile);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var env_key = Environment.GetEnvironmentVariable("NEXUSAPIKEY");
|
var env_key = Environment.GetEnvironmentVariable("NEXUSAPIKEY");
|
||||||
@ -90,7 +114,8 @@ namespace Wabbajack.Lib.NexusApi
|
|||||||
// get the api key from the socket and cache it
|
// get the api key from the socket and cache it
|
||||||
api_key.Task.Wait();
|
api_key.Task.Wait();
|
||||||
var result = api_key.Task.Result;
|
var result = api_key.Task.Result;
|
||||||
File.WriteAllText(API_KEY_CACHE_FILE, result);
|
File.WriteAllBytes(cacheFile, ProtectedData.Protect(Encoding.UTF8.GetBytes(result),
|
||||||
|
Encoding.UTF8.GetBytes(_additionalEntropy), DataProtectionScope.CurrentUser));
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -63,6 +63,7 @@
|
|||||||
<Reference Include="System.Drawing" />
|
<Reference Include="System.Drawing" />
|
||||||
<Reference Include="System.IO.Compression" />
|
<Reference Include="System.IO.Compression" />
|
||||||
<Reference Include="System.Net" />
|
<Reference Include="System.Net" />
|
||||||
|
<Reference Include="System.Security" />
|
||||||
<Reference Include="System.Transactions" />
|
<Reference Include="System.Transactions" />
|
||||||
<Reference Include="System.Web" />
|
<Reference Include="System.Web" />
|
||||||
<Reference Include="System.Windows" />
|
<Reference Include="System.Windows" />
|
||||||
|
Loading…
Reference in New Issue
Block a user