Skip to content

Commit 9655402

Browse files
committed
Add registry keys
1 parent dfa1183 commit 9655402

File tree

10 files changed

+327
-68
lines changed

10 files changed

+327
-68
lines changed

Guard.Core/InstallationContext.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ public static void Init(InstallationType installationType, Version version)
3939
}
4040
else
4141
{
42+
// Check if the app data path is set in the registry
43+
string? registryAppDataPath = RegistrySettings.GetAppDataPath();
44+
if (registryAppDataPath != null)
45+
{
46+
appDataFolderPath = registryAppDataPath;
47+
return;
48+
}
49+
4250
appDataFolderPath = Path.Combine(
4351
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
4452
"2FAGuard"
@@ -131,5 +139,30 @@ public static string GetMutexName()
131139
return prefix
132140
+ Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(hashContent)));
133141
}
142+
143+
public static (bool, string?) CheckAppDataFolder()
144+
{
145+
try
146+
{
147+
string path = GetAppDataFolderPath();
148+
if (!Path.IsPathRooted(path))
149+
{
150+
return (false, "The application data path is not an absolute path");
151+
}
152+
153+
if (Directory.Exists(path))
154+
{
155+
return (true, null);
156+
}
157+
158+
Directory.CreateDirectory(path);
159+
160+
return (true, null);
161+
}
162+
catch (Exception e)
163+
{
164+
return (false, e.Message);
165+
}
166+
}
134167
}
135168
}

Guard.Core/Log.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ public static void Init()
1212
log = new LoggerConfiguration()
1313
.WriteTo.Debug()
1414
.WriteTo.File(
15-
Path.Combine(
16-
InstallationContext.GetAppDataFolderPath(),
17-
"logs",
18-
"log.txt"
19-
),
15+
Path.Combine(InstallationContext.GetAppDataFolderPath(), "logs", "log.txt"),
2016
rollingInterval: RollingInterval.Day,
2117
retainedFileCountLimit: 3
2218
)

Guard.Core/RegistrySettings.cs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using Microsoft.Win32;
2+
3+
namespace Guard.Core
4+
{
5+
public class RegistrySettings
6+
{
7+
private static T? GetValue<T>(string keyPath, string value, T? defaultValue)
8+
{
9+
try
10+
{
11+
return (T?)Registry.GetValue(keyPath, value, defaultValue);
12+
}
13+
catch
14+
{
15+
return defaultValue;
16+
}
17+
}
18+
19+
private static bool GetValue(string keyPath, string value, bool defaultValue)
20+
{
21+
try
22+
{
23+
int? val = (int?)Registry.GetValue(keyPath, value, defaultValue ? 1 : 0);
24+
if (val == null)
25+
{
26+
return defaultValue;
27+
}
28+
return val != 0;
29+
}
30+
catch
31+
{
32+
return defaultValue;
33+
}
34+
}
35+
36+
public static string? GetAppDataPath()
37+
{
38+
string? path = GetValue<string>(
39+
@"HKEY_CURRENT_USER\Software\Policies\2FAGuard",
40+
"AppDataPath",
41+
null
42+
);
43+
if (path != null)
44+
{
45+
path = Environment.ExpandEnvironmentVariables(path);
46+
}
47+
return path;
48+
}
49+
50+
public static (bool hideSkip, bool hideWinHello, bool hidePassword) GetSetupHideOptions()
51+
{
52+
bool hideSkip = GetValue(
53+
@"HKEY_CURRENT_USER\Software\Policies\2FAGuard\Setup",
54+
"HideSkip",
55+
false
56+
);
57+
bool hideWinHello = GetValue(
58+
@"HKEY_CURRENT_USER\Software\Policies\2FAGuard\Setup",
59+
"HideWinHello",
60+
false
61+
);
62+
bool hidePassword = GetValue(
63+
@"HKEY_CURRENT_USER\Software\Policies\2FAGuard\Setup",
64+
"HidePassword",
65+
false
66+
);
67+
68+
// If all options are hidden, show them all
69+
if (hideSkip && hideWinHello && hidePassword)
70+
{
71+
return (false, false, false);
72+
}
73+
74+
return (hideSkip, hideWinHello, hidePassword);
75+
}
76+
77+
public static (
78+
bool hideWinHello,
79+
bool hidePreventScreenRecording,
80+
bool hideSecurityKey
81+
) GetSettingsPageOptions()
82+
{
83+
bool hideWinHello = GetValue(
84+
@"HKEY_CURRENT_USER\Software\Policies\2FAGuard\Settings",
85+
"HideWinHello",
86+
false
87+
);
88+
bool hidePreventRecording = GetValue(
89+
@"HKEY_CURRENT_USER\Software\Policies\2FAGuard\Settings",
90+
"HidePreventRecording",
91+
false
92+
);
93+
bool hideSecurityKey = GetValue(
94+
@"HKEY_CURRENT_USER\Software\Policies\2FAGuard\Settings",
95+
"HideSecurityKey",
96+
false
97+
);
98+
99+
return (hideWinHello, hidePreventRecording, hideSecurityKey);
100+
}
101+
}
102+
}

Guard.WPF/App.xaml.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,25 @@ protected override async void OnStartup(StartupEventArgs e)
3030

3131
singleInstanceMutex = new Mutex(true, mutexName, out bool notAlreadyRunning);
3232

33+
I18n.Init();
34+
35+
(bool appDataFolderOk, string? appDataFolderError) =
36+
InstallationContext.CheckAppDataFolder();
37+
if (!appDataFolderOk)
38+
{
39+
var uiMessageBox = new Wpf.Ui.Controls.MessageBox
40+
{
41+
Title = "Error checking app data folder",
42+
Content = appDataFolderError,
43+
CloseButtonText = I18n.GetString("i.unsupported.exit"),
44+
};
45+
await uiMessageBox.ShowDialogAsync();
46+
Shutdown();
47+
return;
48+
}
49+
3350
Log.Init();
3451
SettingsManager.Init();
35-
I18n.Init();
3652

3753
if (!notAlreadyRunning)
3854
{
@@ -131,6 +147,7 @@ Process process in Process.GetProcessesByName(
131147
Shutdown();
132148
return;
133149
}
150+
134151
if (!EncryptionHelper.IsSupported())
135152
{
136153
var uiMessageBox = new Wpf.Ui.Controls.MessageBox

Guard.WPF/Resources/Strings.de.xaml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
<?xml version="1.0"?>
2-
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:system="clr-namespace:System;assembly=mscorlib">
1+
<?xml version="1.0" ?>
2+
<ResourceDictionary
3+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5+
xmlns:system="clr-namespace:System;assembly=mscorlib">
36

47
<!-- Nav -->
58
<system:String x:Key="i.title">2FAGuard</system:String>
@@ -92,7 +95,7 @@
9295
<system:String x:Key="i.webauthn.delete.yes">Entfernen</system:String>
9396

9497
<!-- Application setup -->
95-
<system:String x:Key="i.welcome.subtext">Vielen Dank fürs Herunterladen! Bitte wähle, wie du deine Token sichern möchtest.</system:String>
98+
<system:String x:Key="i.welcome.subtext">Willkommen! Bitte wähle, wie du deine Token vor unbefugtem Zugriff schützen möchtest</system:String>
9699
<system:String x:Key="i.welcome.hello">Windows Hello oder Passwort</system:String>
97100
<system:String x:Key="i.welcome.hello.description">Einfach und sicher, Passwort als Backup</system:String>
98101
<system:String x:Key="i.welcome.hello.notavailable">Windows Hello ist nicht verfügbar</system:String>

Guard.WPF/Views/Pages/Settings.xaml

Lines changed: 15 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,12 @@
135135
FontWeight="Medium"
136136
Text="{DynamicResource i.settings.security}" />
137137

138-
<Grid Margin="0,20,0,0">
139-
<Grid.ColumnDefinitions>
140-
<ColumnDefinition Width="*" />
141-
<ColumnDefinition Width="*" />
142-
</Grid.ColumnDefinitions>
138+
<UniformGrid
139+
x:Name="SecuritySettingsGrid"
140+
Margin="0,20,0,0"
141+
Columns="2">
143142
<ui:CardControl
144-
Grid.Column="0"
143+
x:Name="WindowsHelloCard"
145144
Margin="0,0,0,15"
146145
Icon="{ui:SymbolIcon Fingerprint24}">
147146
<ui:CardControl.Header>
@@ -162,10 +161,7 @@
162161
</ui:CardControl.Header>
163162
<ui:ToggleSwitch x:Name="WinHelloSwitch" Grid.Column="1" />
164163
</ui:CardControl>
165-
<ui:CardControl
166-
Grid.Column="1"
167-
Margin="15,0,0,15"
168-
Icon="{ui:SymbolIcon Password24}">
164+
<ui:CardControl Margin="0,0,0,15" Icon="{ui:SymbolIcon Password24}">
169165
<ui:CardControl.Header>
170166
<Grid Margin="0,0,35,0">
171167
<Grid.RowDefinitions>
@@ -188,14 +184,9 @@
188184
Content="{DynamicResource i.settings.password.button}"
189185
Icon="{ui:SymbolIcon Pen24}" />
190186
</ui:CardControl>
191-
</Grid>
192-
<Grid>
193-
<Grid.ColumnDefinitions>
194-
<ColumnDefinition Width="*" />
195-
<ColumnDefinition Width="*" />
196-
</Grid.ColumnDefinitions>
187+
197188
<ui:CardControl
198-
Grid.Column="0"
189+
x:Name="PreventRecordingCard"
199190
Margin="0,0,0,15"
200191
Icon="{ui:SymbolIcon Record24}">
201192
<ui:CardControl.Header>
@@ -216,10 +207,7 @@
216207
</ui:CardControl.Header>
217208
<ui:ToggleSwitch x:Name="ScreenRecordingSwitch" Grid.Column="1" />
218209
</ui:CardControl>
219-
<ui:CardControl
220-
Grid.Column="1"
221-
Margin="15,0,0,15"
222-
Icon="{ui:SymbolIcon LockClosed24}">
210+
<ui:CardControl Margin="0,0,0,15" Icon="{ui:SymbolIcon LockClosed24}">
223211
<ui:CardControl.Header>
224212
<Grid Margin="0,0,35,0">
225213
<Grid.RowDefinitions>
@@ -238,16 +226,7 @@
238226
</ui:CardControl.Header>
239227
<ui:ToggleSwitch x:Name="ScreenLockSwitch" Grid.Column="1" />
240228
</ui:CardControl>
241-
</Grid>
242-
<Grid>
243-
<Grid.ColumnDefinitions>
244-
<ColumnDefinition Width="*" />
245-
<ColumnDefinition Width="*" />
246-
</Grid.ColumnDefinitions>
247-
<ui:CardControl
248-
Grid.Column="0"
249-
Margin="0,0,0,15"
250-
Icon="{ui:SymbolIcon ClockLock24}">
229+
<ui:CardControl Margin="0,0,0,15" Icon="{ui:SymbolIcon ClockLock24}">
251230
<ui:CardControl.Header>
252231
<Grid Margin="0,0,35,0">
253232
<Grid.RowDefinitions>
@@ -270,8 +249,8 @@
270249
MinWidth="125" />
271250
</ui:CardControl>
272251
<ui:CardControl
273-
Grid.Column="1"
274-
Margin="15,0,0,15"
252+
x:Name="SecurityKeyCard"
253+
Margin="0,0,0,15"
275254
Icon="{ui:SymbolIcon UsbStick24}">
276255
<ui:CardControl.Header>
277256
<Grid Margin="0,0,35,0">
@@ -296,16 +275,8 @@
296275
Content="{DynamicResource i.settings.webauthn.button}"
297276
Icon="{ui:SymbolIcon Settings24}" />
298277
</ui:CardControl>
299-
</Grid>
300-
<Grid Margin="0,0,0,10">
301-
<Grid.ColumnDefinitions>
302-
<ColumnDefinition Width="*" />
303-
<ColumnDefinition Width="*" />
304-
</Grid.ColumnDefinitions>
305-
<ui:CardControl
306-
Grid.Column="0"
307-
Margin="0,0,0,15"
308-
Icon="{ui:SymbolIcon Delete24}">
278+
279+
<ui:CardControl Margin="0,0,0,15" Icon="{ui:SymbolIcon Delete24}">
309280
<ui:CardControl.Header>
310281
<Grid Margin="0,0,35,0">
311282
<Grid.RowDefinitions>
@@ -328,7 +299,7 @@
328299
Content="{DynamicResource i.settings.reset.button}"
329300
Icon="{ui:SymbolIcon ArrowReset24}" />
330301
</ui:CardControl>
331-
</Grid>
302+
</UniformGrid>
332303

333304
<TextBlock
334305
FontSize="20"

Guard.WPF/Views/Pages/Settings.xaml.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Guard.WPF.Core.Security;
1111
using Guard.WPF.Views.Dialogs;
1212
using Guard.WPF.Views.Pages.Preferences;
13+
using Guard.WPF.Views.UIComponents;
1314
using Wpf.Ui.Controls;
1415

1516
namespace Guard.WPF.Views.Pages
@@ -147,6 +148,28 @@ public Settings()
147148
LockTimeComboBox.SelectionChanged += OnLockTimeSelectionChanged;
148149

149150
WebAuthnBtn.IsEnabled = Auth.IsLoginEnabled();
151+
152+
ApplyRegistrySettings();
153+
FixSecuritySettingsGridSpacing();
154+
}
155+
156+
private void ApplyRegistrySettings()
157+
{
158+
(bool hideWinHello, bool hidePreventScreenRecording, bool hideSecurityKey) =
159+
RegistrySettings.GetSettingsPageOptions();
160+
161+
if (hideWinHello)
162+
{
163+
WindowsHelloCard.Visibility = Visibility.Collapsed;
164+
}
165+
if (hidePreventScreenRecording)
166+
{
167+
PreventRecordingCard.Visibility = Visibility.Collapsed;
168+
}
169+
if (hideSecurityKey)
170+
{
171+
SecurityKeyCard.Visibility = Visibility.Collapsed;
172+
}
150173
}
151174

152175
private void SetSelectedLanguage(LanguageSetting lang)
@@ -523,5 +546,23 @@ private async void CheckWindowsHelloAvailability()
523546
ignoreWinHelloSwitchEvents = false;
524547
}
525548
}
549+
550+
private void FixSecuritySettingsGridSpacing()
551+
{
552+
int i = 0;
553+
foreach (CardControl card in SecuritySettingsGrid.Children)
554+
{
555+
if (card.Visibility == Visibility.Collapsed)
556+
{
557+
continue;
558+
}
559+
560+
if (i % 2 != 0)
561+
{
562+
card.Margin = new Thickness(15, 0, 0, 15);
563+
}
564+
++i;
565+
}
566+
}
526567
}
527568
}

0 commit comments

Comments
 (0)