-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAskPassword.xaml.cs
134 lines (113 loc) · 4.22 KB
/
AskPassword.xaml.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
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using MahApps.Metro.Controls;
namespace TwoFactor
{
public partial class AskPassword : MetroWindow
{
public int FaildedAttempts { get; set; }
public bool ForSettingsChange { get; set; }
public AskPassword()
{
InitializeComponent();
//styles and texts
this.Title = Localizer.GetLocalized("askpassword-title");
textbox_password.Focus();
//darkmode detected then change colors
if (Classes.Globals.IsDarkMode())
{
this.Background = Classes.ResourceController.BrushDarkModeBackground;
textbox_password.Style = Classes.ResourceController.StyleTextBoxDarkMode;
button_ok.Content = Classes.IconController.GetButtonIcon(Classes.Enums.Icon.Check, Classes.ResourceController.BrushBlack, Localizer.GetLocalized("about-ok"));
}
else
{
button_ok.Content = Classes.IconController.GetButtonIcon(Classes.Enums.Icon.Check, Classes.ResourceController.BrushWhite, Localizer.GetLocalized("about-ok"));
}
}
/// <summary>
/// Check if the password is correct 3 times. After 3 fails closes the app
/// </summary>
private void Button_ok_Click(object sender, RoutedEventArgs e)
{
string pass1 = textbox_password.Text.Trim();
var mainwindow = ((MainWindow)Application.Current.MainWindow);
//if textbox is empty
if (string.IsNullOrEmpty(pass1))
{
textbox_password.Style = Classes.ResourceController.StyleTextBoxError;
return;
}
//check password, if it hits 3 errors then close on app open. 1 time with settings change
if (pass1 != Classes.WindowsPassword.GetPassword())
{
FaildedAttempts++;
textbox_password.Text = "";
if (FaildedAttempts == 1 && ForSettingsChange)
{
this.DialogResult = true;
}
else if (FaildedAttempts == 3)
{
MessageBox.Show(Localizer.GetLocalized("askpassword-3times"), "", MessageBoxButton.OK, MessageBoxImage.Warning);
mainwindow.CloseApp();
}
return;
}
//password ok
if (ForSettingsChange)
{
((Settings)Owner).PasswordIsCorrect = true;
}
else
{
mainwindow.PasswordIsCorrect = true;
}
this.DialogResult = true;
}
/// <summary>
/// Handles the keypresses in the window to close it on escape or enter
/// </summary>
private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return || e.Key == Key.Enter)
{
Button_ok_Click(null, null);
}
else if (e.Key == Key.Escape && ForSettingsChange)
{
this.DialogResult = true;
}
else if (e.Key == Key.Escape)
{
((MainWindow)Application.Current.MainWindow).CloseApp();
}
}
/// <summary>
/// Triggers the Button_ok_Click when pressing enter in the textbox
/// </summary>
private void TextBox_KeyEnterUpdate(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
Button_ok_Click(null, null);
}
}
/// <summary>
/// Changes the textbox style when something is types from red to the normal color it the textbox was empty
/// </summary>
private void TextBoxResult_TextChanged(object sender, TextChangedEventArgs e)
{
if (Classes.Globals.IsDarkMode())
{
textbox_password.Style = Classes.ResourceController.StyleTextBoxDarkMode;
}
else
{
textbox_password.Style = Classes.ResourceController.StyleTextBoxNormal;
}
}
}
}