Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ChangePasswordViewModel.cs
1using CommunityToolkit.Mvvm.ComponentModel;
2using CommunityToolkit.Mvvm.Input;
8
10{
15 {
20 : base()
21 {
22 }
23
27 [ObservableProperty]
28 [NotifyCanExecuteChangedFor(nameof(ChangePasswordCommand))]
29 private string oldPassword = string.Empty;
30
34 [ObservableProperty]
35 [NotifyPropertyChangedFor(nameof(IsPassword1NotValid))]
36 [NotifyPropertyChangedFor(nameof(IsPassword2NotValid))]
37 [NotifyPropertyChangedFor(nameof(LocalizedValidationError))]
38 [NotifyCanExecuteChangedFor(nameof(ChangePasswordCommand))]
39 private string newPassword = string.Empty;
40
44 [ObservableProperty]
45 [NotifyPropertyChangedFor(nameof(IsPassword2NotValid))]
46 [NotifyPropertyChangedFor(nameof(NewPasswordsMatch))]
47 [NotifyCanExecuteChangedFor(nameof(ChangePasswordCommand))]
48 private string newPassword2 = string.Empty;
49
53 [ObservableProperty]
54 [NotifyPropertyChangedFor(nameof(NewPasswordsMatch))]
55 [NotifyCanExecuteChangedFor(nameof(ChangePasswordCommand))]
56 private bool incorrectPasswordAlertShown = false;
57
61 public bool CanChangePassword => this.NewPasswordStrength == PasswordStrength.Strong && this.NewPasswordMatchesRetypedNewPassword && this.IsConnected && !this.IsBusy;
62
67
71 public bool NewPasswordsMatch => string.IsNullOrEmpty(this.NewPassword) ? string.IsNullOrEmpty(this.NewPassword2) : string.Equals(this.NewPassword, this.NewPassword2, StringComparison.Ordinal);
72
76 public bool IsPassword1NotValid => !string.IsNullOrEmpty(this.NewPassword) && this.NewPasswordStrength != PasswordStrength.Strong;
77
81 public bool IsPassword2NotValid => !string.IsNullOrEmpty(this.NewPassword2) && !this.NewPasswordsMatch;
82
86 public bool NewPasswordMatchesRetypedNewPassword => string.IsNullOrEmpty(this.NewPassword) ? string.IsNullOrEmpty(this.NewPassword2) : string.Equals(this.NewPassword, this.NewPassword2, StringComparison.Ordinal);
87
92
93 protected override async Task OnInitialize()
94 {
95 await base.OnInitialize();
96 this.NotifyCommandsCanExecuteChanged();
97 }
98
100 protected override Task XmppService_ConnectionStateChanged(object? Sender, XmppState NewState)
101 {
102 return MainThread.InvokeOnMainThreadAsync(async () =>
103 {
104 await base.XmppService_ConnectionStateChanged(Sender, NewState);
105
106 this.NotifyCommandsCanExecuteChanged();
107 });
108 }
109
111 public override void SetIsBusy(bool IsBusy)
112 {
113 base.SetIsBusy(IsBusy);
114 this.NotifyCommandsCanExecuteChanged();
115 }
116
117 private void NotifyCommandsCanExecuteChanged()
118 {
119 this.ChangePasswordCommand.NotifyCanExecuteChanged();
120 }
121
125 [RelayCommand(CanExecute = nameof(CanChangePassword))]
126 private async Task ChangePassword()
127 {
128 if (!string.IsNullOrEmpty(this.OldPassword) && this.CanChangePassword)
129 {
130 if (await App.CheckPasswordAndUnblockUser(this.OldPassword))
131 {
132 try
133 {
134 string NewPassword = ServiceRef.CryptoService.CreateRandomPassword();
135
136 if (!await ServiceRef.XmppService.ChangePassword(NewPassword))
137 {
138 await ServiceRef.UiService.DisplayAlert(
139 ServiceRef.Localizer[nameof(AppResources.ErrorTitle)],
140 ServiceRef.Localizer[nameof(AppResources.UnableToChangePassword)]);
141 return;
142 }
143
144 ServiceRef.TagProfile.LocalPassword = this.NewPassword;
145 ServiceRef.TagProfile.SetAccount(ServiceRef.TagProfile.Account!, NewPassword, string.Empty);
146
147 await ServiceRef.UiService.DisplayAlert(
148 ServiceRef.Localizer[nameof(AppResources.SuccessTitle)],
149 ServiceRef.Localizer[nameof(AppResources.PasswordChanged)]);
150
151 await this.GoBack();
152 }
153 catch (Exception ex)
154 {
155 ServiceRef.LogService.LogException(ex);
156 await ServiceRef.UiService.DisplayException(ex);
157 }
158 }
159 else
160 {
161 this.OldPassword = string.Empty;
162
163 long PasswordAttemptCounter = await App.GetCurrentPasswordCounter();
164 long RemainingAttempts = Math.Max(0, Constants.Password.FirstMaxPasswordAttempts - PasswordAttemptCounter);
165
166 await ServiceRef.UiService.DisplayAlert(
167 ServiceRef.Localizer[nameof(AppResources.ErrorTitle)],
168 ServiceRef.Localizer[nameof(AppResources.PasswordIsInvalid), RemainingAttempts]);
169
170 await App.CheckUserBlocking();
171 }
172 }
173 }
174 }
175}
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 ILogService LogService
Log service.
Definition: ServiceRef.cs:91
static IUiService UiService
Service serializing and managing UI-related tasks.
Definition: ServiceRef.cs:55
static ITagProfile TagProfile
TAG Profile service.
Definition: ServiceRef.cs:79
static ICryptoService CryptoService
Crypto service.
Definition: ServiceRef.cs:163
static IStringLocalizer Localizer
Localization service
Definition: ServiceRef.cs:235
static IXmppService XmppService
The XMPP service for XMPP communication.
Definition: ServiceRef.cs:67
virtual async Task GoBack()
Method called when user wants to navigate to the previous screen.
override Task XmppService_ConnectionStateChanged(object? Sender, XmppState NewState)
Listens to connection state changes from the XMPP server.
bool IsPassword2NotValid
If Second New password entry is not valid.
override void SetIsBusy(bool IsBusy)
Sets the IsBusy property.
bool NewPasswordsMatch
Gets the value indicating whether the entered NewPassword is the same as the entered NewPassword2.
override async Task OnInitialize()
Method called when view is initialized for the first time. Use this method to implement registration ...
static string GetLocalizedValidationError(PasswordStrength PasswordStrength)
Gets a localized error message, given a Password strength.
A view model that holds the XMPP state.
PasswordStrength ValidatePasswordStrength(string? Password)
Validates if the Password is strong enough.
string? Account
The account name for this profile
Definition: ITagProfile.cs:101
void SetAccount(string AccountName, string ClientPasswordHash, string ClientPasswordHashMethod)
Set the account name and password for a new account.
PasswordStrength
Represents a result of validating password strength.
XmppState
State of XMPP connection.
Definition: XmppState.cs:7