forked from trimble-oss/dba-dash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEncryptedConfig.cs
140 lines (123 loc) · 4.17 KB
/
EncryptedConfig.cs
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using Newtonsoft.Json;
namespace DBADash
{
public class EncryptedConfig
{
private static readonly string tempKey = Path.Combine(AppContext.BaseDirectory, "ServiceConfig.TempKey");
private static readonly string random = "Ys8A#QJowc#h#5te4QjumXv4aWYN9F";
private static readonly string cred = "DBADash_Config_" + EncryptText.GetShortHash(AppContext.BaseDirectory.ToLowerInvariant());
public string ProtectedConfig { get; set; }
public EncryptedConfig()
{
}
public string GetConfig()
{
var password = GetPassword();
return GetConfig(password);
}
public string GetConfig(string password)
{
try
{
return ProtectedConfig.DecryptString(password);
}
catch (Exception ex)
{
throw new AggregateException("Failed to decrypt config. Check that the password is correct", ex);
}
}
public EncryptedConfig(string config, string password)
{
SetConfig(config, password);
}
public EncryptedConfig(string config)
{
var password = GetPassword();
SetConfig(config, password);
}
private void SetConfig(string config, string password)
{
if (password == null)
{
throw new ArgumentNullException(nameof(password), @"Password is not set");
}
ProtectedConfig = config.EncryptString(password);
}
public static string GetPassword()
{
string password = null;
if (File.Exists(tempKey))
{
password = File.ReadAllText(tempKey);
password = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? password.MachineDecryptString() : password.DecryptString(random);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
password = Creds.GetPassword(cred);
}
return password;
}
public static void SetPassword(string password, bool createTempKey)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
SavePassword(password);
}
if (createTempKey)
{
var encryptedPass = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? password.MachineEncryptString()
: password.EncryptString(random);
File.WriteAllText(tempKey, encryptedPass);
}
}
[SupportedOSPlatform("windows")]
public static void SavePassword(string password)
{
Creds.SetPassword(cred, password);
}
public static void ClearPassword()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Creds.Remove(cred);
}
if (File.Exists(tempKey))
{
File.Delete(tempKey);
}
}
public string Serialize()
{
return JsonConvert.SerializeObject(this, Formatting.Indented);
}
public static EncryptedConfig Deserialize(string config)
{
return JsonConvert.DeserializeObject<EncryptedConfig>(config);
}
public static bool HasTempKey => File.Exists(tempKey);
[SupportedOSPlatform("windows")]
public static bool ValidateKeyMatch()
{
var credsPwd = Creds.GetPassword(cred);
var keyPassword = GetPassword();
return credsPwd == keyPassword;
}
[SupportedOSPlatform("windows")]
public static void DeleteTempKey(bool validate)
{
if (File.Exists(tempKey))
{
if (validate && !ValidateKeyMatch())
{
throw new Exception("Password validation against temp key failed");
}
File.Delete(tempKey);
}
}
}
}