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;
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 {
20 private readonly IAuthenticationService authenticationService = ServiceRef.Provider.GetRequiredService<IAuthenticationService>();
21
23 {
24 this.PurposeInfo = purpose;
25 this.IsPasswordHidden = true;
26 }
27
31 [ObservableProperty]
32 [NotifyCanExecuteChangedFor(nameof(EnterPasswordCommand))]
33 private string passwordText = string.Empty;
34
38 [ObservableProperty]
39 [NotifyPropertyChangedFor(nameof(PasswordVisibilityPathData))]
40 private bool isPasswordHidden;
41
45 public Geometry PasswordVisibilityPathData => this.IsPasswordHidden ? Geometries.VisibilityOnPath : Geometries.VisibilityOffPath;
46
47
51 public bool CanEnterPassword => !string.IsNullOrEmpty(this.PasswordText);
52
57
61 [RelayCommand(CanExecute = nameof(CanEnterPassword))]
62 private async Task EnterPassword()
63 {
64 if (!string.IsNullOrEmpty(this.PasswordText))
65 {
66 string Password = this.PasswordText;
67
68 if (await this.authenticationService.CheckPasswordAndUnblockUserAsync(Password))
69 {
70 this.TrySetResult(Password);
71 await ServiceRef.PopupService.PopAsync();
72 }
73 else
74 {
75 this.PasswordText = string.Empty;
76
77 long PasswordAttemptCounter = await this.authenticationService.GetCurrentPasswordCounterAsync();
78 long RemainingAttempts = Math.Max(0, Constants.Password.FirstMaxPasswordAttempts - PasswordAttemptCounter);
79
80 await ServiceRef.UiService.DisplayAlert(
82 ServiceRef.Localizer[nameof(AppResources.PasswordIsInvalid), RemainingAttempts]);
83
84 await this.authenticationService.CheckUserBlockingAsync();
85 }
86 }
87 }
88
92 [RelayCommand]
93 private async Task Cancel()
94 {
95 await ServiceRef.PopupService.PopAsync();
96 }
97
98 [RelayCommand]
99 private void TogglePasswordVisibility()
100 {
101 this.IsPasswordHidden = !this.IsPasswordHidden;
102 }
103
104 protected override Task OnPopAsync()
105 {
106 try
107 {
108 this.TrySetResult(null);
109 }
110 catch (Exception)
111 {
112 // ignored
113 }
114
115 return base.OnPopAsync();
116 }
117 }
118
119 // TODO: remove or implement this
120 /* Recover password
121 try
122 {
123 string AcceptLanguage = App.SelectedLanguage.TwoLetterISOLanguageName;
124
125 if (AcceptLanguage != "en")
126 AcceptLanguage += ";q=1,en;q=0.9";
127
128 byte[] nonceData = new byte[32];
129
130 using (RandomNumberGenerator Rnd = RandomNumberGenerator.Create())
131 {
132 Rnd.GetBytes(nonceData);
133 }
134 string nonce = Guid.NewGuid().ToString("n");
135 string host = ServiceRef.TagProfile.Domain;
136 string s = $"{ServiceRef.TagProfile.Account}:{host}:{nonce}";
137 string NewNetworkPassword = ServiceRef.CryptoService.CreateRandomPassword();
138 if (!await ServiceRef.XmppService.ChangePassword(NewNetworkPassword))
139 throw new Exception("password failed");
140 ServiceRef.TagProfile.SetAccount(ServiceRef.TagProfile.Account!, NewNetworkPassword, string.Empty);
141
142 byte[] keyBytes = Encoding.UTF8.GetBytes(NewNetworkPassword);
143 byte[] dataBytes = Encoding.UTF8.GetBytes(s);
144
145 string signature;
146 using (System.Security.Cryptography.HMACSHA256 hmac = new System.Security.Cryptography.HMACSHA256(keyBytes))
147 {
148 byte[] signatureBytes = hmac.ComputeHash(dataBytes);
149 signature = Convert.ToBase64String(signatureBytes);
150 }
151
152 ContentResponse result2 = await InternetContent.PostAsync(
153 new Uri("https://" + host + "/Agent/Account/Login"),
154 new Dictionary<string, object>()
155 {
156 { "userName", ServiceRef.TagProfile.Account },
157 { "nonce", nonce.ToString() },
158 { "signature", signature },
159 { "seconds", 3600 }
160 },
161 new KeyValuePair<string, string>("Accept", "application/json"),
162 new KeyValuePair<string, string>("Accept-Language", AcceptLanguage),
163 new KeyValuePair<string, string>("Accept-Encoding", "0")
164 );
165 result2.AssertOk();
166 string jwt = string.Empty;
167 if (result2.Decoded is Dictionary<string, object> Response)
168 {
169 if (Response.TryGetValue("jwt", out object? Obj) && Obj is string JWT)
170 jwt = JWT;
171 }
172 Console.WriteLine("JWT: " + jwt);
173 ContentResponse result = await InternetContent.PostAsync(new Uri("https://" + host + "/Agent/Account/Recover"),
174 new Dictionary<string, object>
175 {
176 { "country", ""},
177 { "eMail", ServiceRef.TagProfile.EMail },
178 { "personalNr", ""},
179 { "phoneNr", ""},
180 { "userName", ServiceRef.TagProfile.Account }
181 },
182 new KeyValuePair<string, string>("Accept", "application/json"),
183 new KeyValuePair<string, string>("Accept-Language", AcceptLanguage),
184 new KeyValuePair<string, string>("Accept-Encoding", "0"),
185 new KeyValuePair<string, string>("Authorization", "Bearer " + jwt)
186 );
187
188 result.AssertOk();
189 }
190 catch (Exception ex)
191 {
192 Console.WriteLine(ex.Message);
193 }
194 */
195}
Constants for Password
Definition: Constants.cs:792
const int FirstMaxPasswordAttempts
Maximum password enetring attempts, first interval
Definition: Constants.cs:802
A set of never changing property constants and helpful values.
Definition: Constants.cs:24
A strongly-typed resource class, for looking up localized strings, etc.
static string PasswordIsInvalid
Looks up a localized string similar to PIN is invalid. You have {0} remaining attempts....
static string ErrorTitle
Looks up a localized string similar to An error has occurred.
Base class that references services in the app.
Definition: ServiceRef.cs:43
static IServiceProvider Provider
The service provider for the app. This is set before the app is started, and will be used to resolve ...
Definition: ServiceRef.cs:48
static IUiService UiService
Service serializing and managing UI-related tasks.
Definition: ServiceRef.cs:130
static IPopupService PopupService
Popup service for presenting application popups.
Definition: ServiceRef.cs:142
static IReportingStringLocalizer Localizer
Localization service
Definition: ServiceRef.cs:370
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.