Skip to content

Commit

Permalink
Add registry keys
Browse files Browse the repository at this point in the history
  • Loading branch information
timokoessler committed Nov 27, 2024
1 parent dfa1183 commit 9655402
Show file tree
Hide file tree
Showing 10 changed files with 327 additions and 68 deletions.
33 changes: 33 additions & 0 deletions Guard.Core/InstallationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ public static void Init(InstallationType installationType, Version version)
}
else
{
// Check if the app data path is set in the registry
string? registryAppDataPath = RegistrySettings.GetAppDataPath();
if (registryAppDataPath != null)
{
appDataFolderPath = registryAppDataPath;
return;
}

appDataFolderPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"2FAGuard"
Expand Down Expand Up @@ -131,5 +139,30 @@ public static string GetMutexName()
return prefix
+ Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(hashContent)));
}

public static (bool, string?) CheckAppDataFolder()
{
try
{
string path = GetAppDataFolderPath();
if (!Path.IsPathRooted(path))
{
return (false, "The application data path is not an absolute path");
}

if (Directory.Exists(path))
{
return (true, null);
}

Directory.CreateDirectory(path);

return (true, null);
}
catch (Exception e)
{
return (false, e.Message);
}
}
}
}
6 changes: 1 addition & 5 deletions Guard.Core/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ public static void Init()
log = new LoggerConfiguration()
.WriteTo.Debug()
.WriteTo.File(
Path.Combine(
InstallationContext.GetAppDataFolderPath(),
"logs",
"log.txt"
),
Path.Combine(InstallationContext.GetAppDataFolderPath(), "logs", "log.txt"),
rollingInterval: RollingInterval.Day,
retainedFileCountLimit: 3
)
Expand Down
102 changes: 102 additions & 0 deletions Guard.Core/RegistrySettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using Microsoft.Win32;

namespace Guard.Core
{
public class RegistrySettings
{
private static T? GetValue<T>(string keyPath, string value, T? defaultValue)
{
try
{
return (T?)Registry.GetValue(keyPath, value, defaultValue);
}
catch
{
return defaultValue;
}
}

private static bool GetValue(string keyPath, string value, bool defaultValue)
{
try
{
int? val = (int?)Registry.GetValue(keyPath, value, defaultValue ? 1 : 0);
if (val == null)
{
return defaultValue;
}
return val != 0;
}
catch
{
return defaultValue;
}
}

public static string? GetAppDataPath()
{
string? path = GetValue<string>(
@"HKEY_CURRENT_USER\Software\Policies\2FAGuard",
"AppDataPath",
null
);
if (path != null)
{
path = Environment.ExpandEnvironmentVariables(path);
}
return path;
}

public static (bool hideSkip, bool hideWinHello, bool hidePassword) GetSetupHideOptions()
{
bool hideSkip = GetValue(
@"HKEY_CURRENT_USER\Software\Policies\2FAGuard\Setup",
"HideSkip",
false
);
bool hideWinHello = GetValue(
@"HKEY_CURRENT_USER\Software\Policies\2FAGuard\Setup",
"HideWinHello",
false
);
bool hidePassword = GetValue(
@"HKEY_CURRENT_USER\Software\Policies\2FAGuard\Setup",
"HidePassword",
false
);

// If all options are hidden, show them all
if (hideSkip && hideWinHello && hidePassword)
{
return (false, false, false);
}

return (hideSkip, hideWinHello, hidePassword);
}

public static (
bool hideWinHello,
bool hidePreventScreenRecording,
bool hideSecurityKey
) GetSettingsPageOptions()
{
bool hideWinHello = GetValue(
@"HKEY_CURRENT_USER\Software\Policies\2FAGuard\Settings",
"HideWinHello",
false
);
bool hidePreventRecording = GetValue(
@"HKEY_CURRENT_USER\Software\Policies\2FAGuard\Settings",
"HidePreventRecording",
false
);
bool hideSecurityKey = GetValue(
@"HKEY_CURRENT_USER\Software\Policies\2FAGuard\Settings",
"HideSecurityKey",
false
);

return (hideWinHello, hidePreventRecording, hideSecurityKey);
}
}
}
19 changes: 18 additions & 1 deletion Guard.WPF/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,25 @@ protected override async void OnStartup(StartupEventArgs e)

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

I18n.Init();

(bool appDataFolderOk, string? appDataFolderError) =
InstallationContext.CheckAppDataFolder();
if (!appDataFolderOk)
{
var uiMessageBox = new Wpf.Ui.Controls.MessageBox
{
Title = "Error checking app data folder",
Content = appDataFolderError,
CloseButtonText = I18n.GetString("i.unsupported.exit"),
};
await uiMessageBox.ShowDialogAsync();
Shutdown();
return;
}

Log.Init();
SettingsManager.Init();
I18n.Init();

if (!notAlreadyRunning)
{
Expand Down Expand Up @@ -131,6 +147,7 @@ Process process in Process.GetProcessesByName(
Shutdown();
return;
}

if (!EncryptionHelper.IsSupported())
{
var uiMessageBox = new Wpf.Ui.Controls.MessageBox
Expand Down
9 changes: 6 additions & 3 deletions Guard.WPF/Resources/Strings.de.xaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?xml version="1.0"?>
<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">
<?xml version="1.0" ?>
<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">

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

<!-- Application setup -->
<system:String x:Key="i.welcome.subtext">Vielen Dank fürs Herunterladen! Bitte wähle, wie du deine Token sichern möchtest.</system:String>
<system:String x:Key="i.welcome.subtext">Willkommen! Bitte wähle, wie du deine Token vor unbefugtem Zugriff schützen möchtest</system:String>
<system:String x:Key="i.welcome.hello">Windows Hello oder Passwort</system:String>
<system:String x:Key="i.welcome.hello.description">Einfach und sicher, Passwort als Backup</system:String>
<system:String x:Key="i.welcome.hello.notavailable">Windows Hello ist nicht verfügbar</system:String>
Expand Down
59 changes: 15 additions & 44 deletions Guard.WPF/Views/Pages/Settings.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,12 @@
FontWeight="Medium"
Text="{DynamicResource i.settings.security}" />

<Grid Margin="0,20,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<UniformGrid
x:Name="SecuritySettingsGrid"
Margin="0,20,0,0"
Columns="2">
<ui:CardControl
Grid.Column="0"
x:Name="WindowsHelloCard"
Margin="0,0,0,15"
Icon="{ui:SymbolIcon Fingerprint24}">
<ui:CardControl.Header>
Expand All @@ -162,10 +161,7 @@
</ui:CardControl.Header>
<ui:ToggleSwitch x:Name="WinHelloSwitch" Grid.Column="1" />
</ui:CardControl>
<ui:CardControl
Grid.Column="1"
Margin="15,0,0,15"
Icon="{ui:SymbolIcon Password24}">
<ui:CardControl Margin="0,0,0,15" Icon="{ui:SymbolIcon Password24}">
<ui:CardControl.Header>
<Grid Margin="0,0,35,0">
<Grid.RowDefinitions>
Expand All @@ -188,14 +184,9 @@
Content="{DynamicResource i.settings.password.button}"
Icon="{ui:SymbolIcon Pen24}" />
</ui:CardControl>
</Grid>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>

<ui:CardControl
Grid.Column="0"
x:Name="PreventRecordingCard"
Margin="0,0,0,15"
Icon="{ui:SymbolIcon Record24}">
<ui:CardControl.Header>
Expand All @@ -216,10 +207,7 @@
</ui:CardControl.Header>
<ui:ToggleSwitch x:Name="ScreenRecordingSwitch" Grid.Column="1" />
</ui:CardControl>
<ui:CardControl
Grid.Column="1"
Margin="15,0,0,15"
Icon="{ui:SymbolIcon LockClosed24}">
<ui:CardControl Margin="0,0,0,15" Icon="{ui:SymbolIcon LockClosed24}">
<ui:CardControl.Header>
<Grid Margin="0,0,35,0">
<Grid.RowDefinitions>
Expand All @@ -238,16 +226,7 @@
</ui:CardControl.Header>
<ui:ToggleSwitch x:Name="ScreenLockSwitch" Grid.Column="1" />
</ui:CardControl>
</Grid>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ui:CardControl
Grid.Column="0"
Margin="0,0,0,15"
Icon="{ui:SymbolIcon ClockLock24}">
<ui:CardControl Margin="0,0,0,15" Icon="{ui:SymbolIcon ClockLock24}">
<ui:CardControl.Header>
<Grid Margin="0,0,35,0">
<Grid.RowDefinitions>
Expand All @@ -270,8 +249,8 @@
MinWidth="125" />
</ui:CardControl>
<ui:CardControl
Grid.Column="1"
Margin="15,0,0,15"
x:Name="SecurityKeyCard"
Margin="0,0,0,15"
Icon="{ui:SymbolIcon UsbStick24}">
<ui:CardControl.Header>
<Grid Margin="0,0,35,0">
Expand All @@ -296,16 +275,8 @@
Content="{DynamicResource i.settings.webauthn.button}"
Icon="{ui:SymbolIcon Settings24}" />
</ui:CardControl>
</Grid>
<Grid Margin="0,0,0,10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ui:CardControl
Grid.Column="0"
Margin="0,0,0,15"
Icon="{ui:SymbolIcon Delete24}">

<ui:CardControl Margin="0,0,0,15" Icon="{ui:SymbolIcon Delete24}">
<ui:CardControl.Header>
<Grid Margin="0,0,35,0">
<Grid.RowDefinitions>
Expand All @@ -328,7 +299,7 @@
Content="{DynamicResource i.settings.reset.button}"
Icon="{ui:SymbolIcon ArrowReset24}" />
</ui:CardControl>
</Grid>
</UniformGrid>

<TextBlock
FontSize="20"
Expand Down
41 changes: 41 additions & 0 deletions Guard.WPF/Views/Pages/Settings.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Guard.WPF.Core.Security;
using Guard.WPF.Views.Dialogs;
using Guard.WPF.Views.Pages.Preferences;
using Guard.WPF.Views.UIComponents;
using Wpf.Ui.Controls;

namespace Guard.WPF.Views.Pages
Expand Down Expand Up @@ -147,6 +148,28 @@ public Settings()
LockTimeComboBox.SelectionChanged += OnLockTimeSelectionChanged;

WebAuthnBtn.IsEnabled = Auth.IsLoginEnabled();

ApplyRegistrySettings();
FixSecuritySettingsGridSpacing();
}

private void ApplyRegistrySettings()
{
(bool hideWinHello, bool hidePreventScreenRecording, bool hideSecurityKey) =
RegistrySettings.GetSettingsPageOptions();

if (hideWinHello)
{
WindowsHelloCard.Visibility = Visibility.Collapsed;
}
if (hidePreventScreenRecording)
{
PreventRecordingCard.Visibility = Visibility.Collapsed;
}
if (hideSecurityKey)
{
SecurityKeyCard.Visibility = Visibility.Collapsed;
}
}

private void SetSelectedLanguage(LanguageSetting lang)
Expand Down Expand Up @@ -523,5 +546,23 @@ private async void CheckWindowsHelloAvailability()
ignoreWinHelloSwitchEvents = false;
}
}

private void FixSecuritySettingsGridSpacing()
{
int i = 0;
foreach (CardControl card in SecuritySettingsGrid.Children)
{
if (card.Visibility == Visibility.Collapsed)
{
continue;
}

if (i % 2 != 0)
{
card.Margin = new Thickness(15, 0, 0, 15);
}
++i;
}
}
}
}
Loading

0 comments on commit 9655402

Please sign in to comment.