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 {
16 private readonly IAuthenticationService authenticationService = ServiceRef.Provider.GetRequiredService<IAuthenticationService>();
17
22 : base()
23 {
24 }
25
29 [ObservableProperty]
30 [NotifyCanExecuteChangedFor(nameof(ChangePasswordCommand))]
31 private string oldPassword = string.Empty;
32
36 [ObservableProperty]
37 [NotifyPropertyChangedFor(nameof(IsPassword1NotValid))]
38 [NotifyPropertyChangedFor(nameof(IsPassword2NotValid))]
39 [NotifyPropertyChangedFor(nameof(LocalizedValidationError))]
40 [NotifyCanExecuteChangedFor(nameof(ChangePasswordCommand))]
41 private string newPassword = string.Empty;
42
46 [ObservableProperty]
47 [NotifyPropertyChangedFor(nameof(IsPassword2NotValid))]
48 [NotifyPropertyChangedFor(nameof(NewPasswordsMatch))]
49 [NotifyCanExecuteChangedFor(nameof(ChangePasswordCommand))]
50 private string newPassword2 = string.Empty;
51
55 [ObservableProperty]
56 [NotifyPropertyChangedFor(nameof(NewPasswordsMatch))]
57 [NotifyCanExecuteChangedFor(nameof(ChangePasswordCommand))]
58 private bool incorrectPasswordAlertShown = false;
59
63 public bool CanChangePassword => this.NewPasswordStrength == PasswordStrength.Strong && this.NewPasswordMatchesRetypedNewPassword && this.IsConnected && !this.IsBusy;
64
69
73 public bool NewPasswordsMatch => string.IsNullOrEmpty(this.NewPassword) ? string.IsNullOrEmpty(this.NewPassword2) : string.Equals(this.NewPassword, this.NewPassword2, StringComparison.Ordinal);
74
78 public bool IsPassword1NotValid => !string.IsNullOrEmpty(this.NewPassword) && this.NewPasswordStrength != PasswordStrength.Strong;
79
83 public bool IsPassword2NotValid => !string.IsNullOrEmpty(this.NewPassword2) && !this.NewPasswordsMatch;
84
88 public bool NewPasswordMatchesRetypedNewPassword => string.IsNullOrEmpty(this.NewPassword) ? string.IsNullOrEmpty(this.NewPassword2) : string.Equals(this.NewPassword, this.NewPassword2, StringComparison.Ordinal);
89
93 public string LocalizedValidationError => GetLocalizedValidationError(this.NewPasswordStrength);
94
101 {
102 return PasswordStrength switch
103 {
105
112
113 PasswordStrength.ContainsAddress => ServiceRef.Localizer[nameof(AppResources.PasswordContainsAddress)],
114 PasswordStrength.ContainsName => ServiceRef.Localizer[nameof(AppResources.PasswordContainsName)],
115 PasswordStrength.ContainsPersonalNumber => ServiceRef.Localizer[nameof(AppResources.PasswordContainsPersonalNumber)],
116 PasswordStrength.ContainsPhoneNumber => ServiceRef.Localizer[nameof(AppResources.PasswordContainsPhoneNumber)],
117 PasswordStrength.ContainsEMail => ServiceRef.Localizer[nameof(AppResources.PasswordContainsEMail)],
118 PasswordStrength.Strong => string.Empty,
119 _ => throw new NotImplementedException()
120 };
121 }
122
123 public override async Task OnInitializeAsync()
124 {
125 await base.OnInitializeAsync();
126 this.NotifyCommandsCanExecuteChanged();
127 }
128
130 protected override Task XmppService_ConnectionStateChanged(object? Sender, XmppState NewState)
131 {
132 return MainThread.InvokeOnMainThreadAsync(async () =>
133 {
134 await base.XmppService_ConnectionStateChanged(Sender, NewState);
135
136 this.NotifyCommandsCanExecuteChanged();
137 });
138 }
139
141 public override void SetIsBusy(bool IsBusy)
142 {
143 base.SetIsBusy(IsBusy);
144 this.NotifyCommandsCanExecuteChanged();
145 }
146
147 private void NotifyCommandsCanExecuteChanged()
148 {
149 this.ChangePasswordCommand.NotifyCanExecuteChanged();
150 }
151
155 [RelayCommand(CanExecute = nameof(CanChangePassword))]
156 private async Task ChangePassword()
157 {
158 if (!string.IsNullOrEmpty(this.OldPassword) && this.CanChangePassword)
159 {
160 if (await this.authenticationService.CheckPasswordAndUnblockUserAsync(this.OldPassword))
161 {
162 try
163 {
164 string NewPassword = ServiceRef.CryptoService.CreateRandomPassword();
165
166 if (!await ServiceRef.XmppService.ChangePassword(NewPassword))
167 {
168 await ServiceRef.UiService.DisplayAlert(
171 return;
172 }
173
174 ServiceRef.TagProfile.LocalPassword = this.NewPassword;
175 ServiceRef.TagProfile.SetAccount(ServiceRef.TagProfile.Account!, NewPassword, string.Empty);
176
177 await ServiceRef.UiService.DisplayAlert(
180
181 await this.GoBack();
182 }
183 catch (Exception ex)
184 {
185 ServiceRef.LogService.LogException(ex);
186 await ServiceRef.UiService.DisplayException(ex);
187 }
188 }
189 else
190 {
191 this.OldPassword = string.Empty;
192
193 long PasswordAttemptCounter = await this.authenticationService.GetCurrentPasswordCounterAsync();
194 long RemainingAttempts = Math.Max(0, Constants.Password.FirstMaxPasswordAttempts - PasswordAttemptCounter);
195
196 await ServiceRef.UiService.DisplayAlert(
198 ServiceRef.Localizer[nameof(AppResources.PasswordIsInvalid), RemainingAttempts]);
199
200 await this.authenticationService.CheckUserBlockingAsync();
201 }
202 }
203 }
204 }
205}
Constants for Password
Definition: Constants.cs:792
const int FirstMaxPasswordAttempts
Maximum password enetring attempts, first interval
Definition: Constants.cs:802
Authentication constants
Definition: Constants.cs:50
const int MaxPasswordIdenticalSymbols
Maximum number of identical symbols in a password.
Definition: Constants.cs:79
const int MinPasswordLength
Minimum length for password
Definition: Constants.cs:54
const int MinPasswordSymbolsFromDifferentClasses
Minimum number of symbols from at least two character classes (digits, letters, other) in a password.
Definition: Constants.cs:74
const int MaxPasswordSequencedSymbols
Maximum number of sequenced symbols in a password.
Definition: Constants.cs:84
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 PasswordWithNotEnoughLettersOrDigits
Looks up a localized string similar to You must also use at least {0} letters or digits....
static string PasswordWithNotEnoughDigitsOrSigns
Looks up a localized string similar to You must also use at least {0} digits or symbols....
static string PasswordWithTooManyIdenticalSymbols
Looks up a localized string similar to A PIN must not contain more than {0} identical symbols....
static string PasswordContainsName
Looks up a localized string similar to A PIN must not contain any word from the identity names....
static string PasswordContainsAddress
Looks up a localized string similar to A PIN must not contain any word from the identity address....
static string PasswordContainsPhoneNumber
Looks up a localized string similar to A PIN must not contain the identity phone number....
static string PasswordIsInvalid
Looks up a localized string similar to PIN is invalid. You have {0} remaining attempts....
static string PasswordChanged
Looks up a localized string similar to PIN has been successfully changed..
static string PasswordContainsPersonalNumber
Looks up a localized string similar to A PIN must not contain the identity personal number....
static string UnableToChangePassword
Looks up a localized string similar to Unable to change network password..
static string PasswordWithTooManySequencedSymbols
Looks up a localized string similar to A PIN must not contain more than {0} sequenced symbols,...
static string PasswordWithNotEnoughDigitsLettersSigns
Looks up a localized string similar to At least {0} letters, digits or symbols must be used....
static string PasswordContainsEMail
Looks up a localized string similar to A PIN must not contain the identity e-mail address....
static string SuccessTitle
Looks up a localized string similar to Success.
static string PasswordTooShort
Looks up a localized string similar to PIN length must be at least {0} characters....
static string PasswordWithNotEnoughLettersOrSigns
Looks up a localized string similar to You must also use at least {0} letters or symbols....
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 ILogService LogService
Log service.
Definition: ServiceRef.cs:214
static IUiService UiService
Service serializing and managing UI-related tasks.
Definition: ServiceRef.cs:130
static ITagProfile TagProfile
TAG Profile service.
Definition: ServiceRef.cs:202
static ICryptoService CryptoService
Crypto service.
Definition: ServiceRef.cs:286
static IReportingStringLocalizer Localizer
Localization service
Definition: ServiceRef.cs:370
static IXmppService XmppService
The XMPP service for XMPP communication.
Definition: ServiceRef.cs:190
virtual async Task GoBack()
Method called when user wants to navigate to the previous screen.
override async Task OnInitializeAsync()
Method called when view is initialized for the first time. Use this method to implement registration ...
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.
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:102
void SetAccount(string AccountName, string ClientPasswordHash, string ClientPasswordHashMethod)
Set the account name and password for a new account.
Definition: ImplTypes.g.cs:58
PasswordStrength
Represents a result of validating password strength.
XmppState
State of XMPP connection.
Definition: XmppState.cs:7