Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
CheckPasswordViewModel.cs
1using System.Runtime.CompilerServices;
2using System.Security.Cryptography;
3using System.Text;
4using CommunityToolkit.Mvvm.ComponentModel;
5using CommunityToolkit.Mvvm.Input;
6using Microsoft.Maui.Controls.Shapes;
10using Waher.Content;
12
14{
18 public partial class CheckPasswordViewModel : ReturningPopupViewModel<string>
19 {
21 {
22 this.PurposeInfo = purpose;
23 this.IsPasswordHidden = true;
24 }
25
29 [ObservableProperty]
30 [NotifyCanExecuteChangedFor(nameof(EnterPasswordCommand))]
31 private string passwordText = string.Empty;
32
36 [ObservableProperty]
37 [NotifyPropertyChangedFor(nameof(PasswordVisibilityPathData))]
38 private bool isPasswordHidden;
39
43 public Geometry PasswordVisibilityPathData => this.IsPasswordHidden ? Geometries.VisibilityOnPath : Geometries.VisibilityOffPath;
44
45
49 public bool CanEnterPassword => !string.IsNullOrEmpty(this.PasswordText);
50
55
59 [RelayCommand(CanExecute = nameof(CanEnterPassword))]
60 private async Task EnterPassword()
61 {
62 if (!string.IsNullOrEmpty(this.PasswordText))
63 {
64 string Password = this.PasswordText;
65
66 if (await App.CheckPasswordAndUnblockUser(Password))
67 {
68 await ServiceRef.UiService.PopAsync();
69 this.result.TrySetResult(Password);
70 }
71 else
72 {
73 this.PasswordText = string.Empty;
74
75 long PasswordAttemptCounter = await App.GetCurrentPasswordCounter();
76 long RemainingAttempts = Math.Max(0, Constants.Password.FirstMaxPasswordAttempts - PasswordAttemptCounter);
77
78 await ServiceRef.UiService.DisplayAlert(
79 ServiceRef.Localizer[nameof(AppResources.ErrorTitle)],
80 ServiceRef.Localizer[nameof(AppResources.PasswordIsInvalid), RemainingAttempts]);
81
82 await App.CheckUserBlocking();
83 }
84 }
85 }
86
90 [RelayCommand]
91 private async Task Cancel()
92 {
93 await ServiceRef.UiService.PopAsync();
94 this.result.TrySetResult(null);
95 }
96
97 [RelayCommand]
98 private void TogglePasswordVisibility()
99 {
100 this.IsPasswordHidden = !this.IsPasswordHidden;
101 }
102 }
103
104 // TODO: remove or implement this
105 /* Recover password
106 try
107 {
108 string AcceptLanguage = App.SelectedLanguage.TwoLetterISOLanguageName;
109
110 if (AcceptLanguage != "en")
111 AcceptLanguage += ";q=1,en;q=0.9";
112
113 byte[] nonceData = new byte[32];
114
115 using (RandomNumberGenerator Rnd = RandomNumberGenerator.Create())
116 {
117 Rnd.GetBytes(nonceData);
118 }
119 string nonce = Guid.NewGuid().ToString("n");
120 string host = ServiceRef.TagProfile.Domain;
121 string s = $"{ServiceRef.TagProfile.Account}:{host}:{nonce}";
122 string NewNetworkPassword = ServiceRef.CryptoService.CreateRandomPassword();
123 if (!await ServiceRef.XmppService.ChangePassword(NewNetworkPassword))
124 throw new Exception("password failed");
125 ServiceRef.TagProfile.SetAccount(ServiceRef.TagProfile.Account!, NewNetworkPassword, string.Empty);
126
127 byte[] keyBytes = Encoding.UTF8.GetBytes(NewNetworkPassword);
128 byte[] dataBytes = Encoding.UTF8.GetBytes(s);
129
130 string signature;
131 using (System.Security.Cryptography.HMACSHA256 hmac = new System.Security.Cryptography.HMACSHA256(keyBytes))
132 {
133 byte[] signatureBytes = hmac.ComputeHash(dataBytes);
134 signature = Convert.ToBase64String(signatureBytes);
135 }
136
137 object result2 = await InternetContent.PostAsync(
138 new Uri("https://" + host + "/Agent/Account/Login"),
139 new Dictionary<string, object>()
140 {
141 { "userName", ServiceRef.TagProfile.Account },
142 { "nonce", nonce.ToString() },
143 { "signature", signature },
144 { "seconds", 3600 }
145 },
146 new KeyValuePair<string, string>("Accept", "application/json"),
147 new KeyValuePair<string, string>("Accept-Language", AcceptLanguage),
148 new KeyValuePair<string, string>("Accept-Encoding", "0")
149 );
150 string jwt = string.Empty;
151 if (result2 is Dictionary<string, object> Response)
152 {
153 if (Response.TryGetValue("jwt", out object? Obj) && Obj is string JWT)
154 jwt = JWT;
155 }
156 Console.WriteLine("JWT: " + jwt);
157 object result = await InternetContent.PostAsync(new Uri("https://" + host + "/Agent/Account/Recover"),
158 new Dictionary<string, object>
159 {
160 { "country", ""},
161 { "eMail", ServiceRef.TagProfile.EMail },
162 { "personalNr", ""},
163 { "phoneNr", ""},
164 { "userName", ServiceRef.TagProfile.Account }
165 },
166 new KeyValuePair<string, string>("Accept", "application/json"),
167 new KeyValuePair<string, string>("Accept-Language", AcceptLanguage),
168 new KeyValuePair<string, string>("Accept-Encoding", "0"),
169 new KeyValuePair<string, string>("Authorization", "Bearer " + jwt)
170 );
171
172 }
173 catch (Exception ex)
174 {
175 Console.WriteLine(ex.Message);
176 }
177 */
178}
The Application class, representing an instance of the Neuro-Access app.
Definition: App.xaml.cs:69
static async Task< bool > CheckPasswordAndUnblockUser(string Password)
Check the Password and reset the blocking counters if it matches
Definition: App.xaml.cs:1077
static async Task CheckUserBlocking()
Verify if the user is blocked and show an alert
Definition: App.xaml.cs:1037
Constants for Password
Definition: Constants.cs:650
const int FirstMaxPasswordAttempts
Maximum password enetring attempts, first interval
Definition: Constants.cs:660
A set of never changing property constants and helpful values.
Definition: Constants.cs:7
Base class that references services in the app.
Definition: ServiceRef.cs:31
static IUiService UiService
Service serializing and managing UI-related tasks.
Definition: ServiceRef.cs:55
static IStringLocalizer Localizer
Localization service
Definition: ServiceRef.cs:235
Static class containing SVG Paths for symbols used in the app.
Definition: Geometries.cs:11
View model for page letting the user enter a password to be verified with the password defined by the...
Geometry PasswordVisibilityPathData
The path data for the password visibility icon
AuthenticationPurpose PurposeInfo
The purpose of the authentication request.
AuthenticationPurpose
Purpose for requesting the user to authenticate itself.