-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement UI and client service for password change
- Loading branch information
1 parent
8070b57
commit 67daef3
Showing
8 changed files
with
497 additions
and
225 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
Crypter.Common.Client/Interfaces/Services/UserSettings/IUserPasswordChangeService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright (C) 2024 Crypter File Transfer | ||
* | ||
* This file is part of the Crypter file transfer project. | ||
* | ||
* Crypter is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* The Crypter source code is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* You can be released from the requirements of the aforementioned license | ||
* by purchasing a commercial license. Buying such a license is mandatory | ||
* as soon as you develop commercial activities involving the Crypter source | ||
* code without disclosing the source code of your own applications. | ||
* | ||
* Contact the current copyright holder to discuss commercial license options. | ||
*/ | ||
|
||
using System.Threading.Tasks; | ||
using Crypter.Common.Contracts.Features.UserAuthentication.PasswordChange; | ||
using Crypter.Common.Primitives; | ||
using EasyMonads; | ||
|
||
namespace Crypter.Common.Client.Interfaces.Services.UserSettings; | ||
|
||
public interface IUserPasswordChangeService | ||
{ | ||
Task<Either<PasswordChangeError, Unit>> ChangePasswordAsync(Password oldPassword, Password newPassword); | ||
} |
113 changes: 113 additions & 0 deletions
113
Crypter.Common.Client/Services/UserSettings/UserPasswordChangeService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Copyright (C) 2024 Crypter File Transfer | ||
* | ||
* This file is part of the Crypter file transfer project. | ||
* | ||
* Crypter is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* The Crypter source code is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* You can be released from the requirements of the aforementioned license | ||
* by purchasing a commercial license. Buying such a license is mandatory | ||
* as soon as you develop commercial activities involving the Crypter source | ||
* code without disclosing the source code of your own applications. | ||
* | ||
* Contact the current copyright holder to discuss commercial license options. | ||
*/ | ||
|
||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Crypter.Common.Client.Interfaces.HttpClients; | ||
using Crypter.Common.Client.Interfaces.Services; | ||
using Crypter.Common.Client.Interfaces.Services.UserSettings; | ||
using Crypter.Common.Contracts.Features.UserAuthentication; | ||
using Crypter.Common.Contracts.Features.UserAuthentication.PasswordChange; | ||
using Crypter.Common.Primitives; | ||
using Crypter.Crypto.Common; | ||
using EasyMonads; | ||
|
||
namespace Crypter.Common.Client.Services.UserSettings; | ||
|
||
public class UserPasswordChangeService : IUserPasswordChangeService | ||
{ | ||
private readonly ICrypterApiClient _crypterApiClient; | ||
private readonly ICryptoProvider _cryptoProvider; | ||
private readonly IUserKeysService _userKeysService; | ||
private readonly IUserPasswordService _userPasswordService; | ||
private readonly IUserSessionService _userSessionService; | ||
|
||
public UserPasswordChangeService(ICrypterApiClient crypterApiClient, ICryptoProvider cryptoProvider, IUserKeysService userKeysService, IUserPasswordService userPasswordService, IUserSessionService userSessionService) | ||
{ | ||
_crypterApiClient = crypterApiClient; | ||
_cryptoProvider = cryptoProvider; | ||
_userKeysService = userKeysService; | ||
_userPasswordService = userPasswordService; | ||
_userSessionService = userSessionService; | ||
} | ||
|
||
public async Task<Either<PasswordChangeError, Unit>> ChangePasswordAsync(Password oldPassword, Password newPassword) | ||
{ | ||
return await _userSessionService.Session.ToEither(PasswordChangeError.UnknownError) | ||
.BindAsync(session => _userKeysService.MasterKey.ToEither(PasswordChangeError.UnknownError) | ||
.BindAsync(async masterKey => | ||
{ | ||
Username username = Username.From(session.Username); | ||
return await _userPasswordService.DeriveUserAuthenticationPasswordAsync(username, newPassword, _userPasswordService.CurrentPasswordVersion) | ||
.MatchAsync( | ||
() => PasswordChangeError.PasswordHashFailure, | ||
async newVersionedPassword => await _userPasswordService.DeriveUserCredentialKeyAsync(username, newPassword, _userPasswordService.CurrentPasswordVersion) | ||
.ToEitherAsync(PasswordChangeError.PasswordHashFailure) | ||
.BindAsync(async credentialKey => | ||
{ | ||
byte[] nonce = _cryptoProvider.Random.GenerateRandomBytes((int)_cryptoProvider.Encryption.NonceSize); | ||
byte[] encryptedMasterKey = _cryptoProvider.Encryption.Encrypt(credentialKey, nonce, masterKey); | ||
|
||
return await _userPasswordService.DeriveUserAuthenticationPasswordAsync(username, oldPassword, _userPasswordService.CurrentPasswordVersion) | ||
.MatchAsync( | ||
() => PasswordChangeError.PasswordHashFailure, | ||
async oldVersionedPassword => await ChangePasswordRecursiveAsync(username, oldPassword, [oldVersionedPassword], newVersionedPassword, encryptedMasterKey, nonce)); | ||
})); | ||
})); | ||
} | ||
|
||
private async Task<Either<PasswordChangeError, Unit>> ChangePasswordRecursiveAsync(Username username, Password oldPassword, List<VersionedPassword> oldPasswords, VersionedPassword newPassword, byte[] encryptedMasterKey, byte[] nonce) | ||
{ | ||
return await SendPasswordChangeRequest(oldPasswords, newPassword, encryptedMasterKey, nonce) | ||
.MatchAsync( | ||
async error => | ||
{ | ||
int oldestPasswordVersionAttempted = oldPasswords.Min(x => x.Version); | ||
if (error == PasswordChangeError.InvalidOldPasswordVersion && oldestPasswordVersionAttempted > 0) | ||
{ | ||
return await _userPasswordService | ||
.DeriveUserAuthenticationPasswordAsync(username, oldPassword, oldestPasswordVersionAttempted - 1) | ||
.MatchAsync( | ||
() => PasswordChangeError.PasswordHashFailure, | ||
async previousVersionedPassword => | ||
{ | ||
oldPasswords.Add(previousVersionedPassword); | ||
return await ChangePasswordRecursiveAsync(username, oldPassword, oldPasswords, newPassword, encryptedMasterKey, nonce); | ||
}); | ||
} | ||
return error; | ||
}, | ||
response => response, | ||
PasswordChangeError.UnknownError); | ||
} | ||
|
||
private async Task<Either<PasswordChangeError, Unit>> SendPasswordChangeRequest(List<VersionedPassword> oldPasswords, VersionedPassword newPassword, byte[] encryptedMasterKey, byte[] nonce) | ||
{ | ||
PasswordChangeRequest request = new PasswordChangeRequest(oldPasswords, newPassword, encryptedMasterKey, nonce); | ||
return await _crypterApiClient.UserAuthentication.ChangePasswordAsync(request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
Crypter.Web/Shared/UserSettings/UserSettingsAccountInfo.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
@* | ||
* Copyright (C) 2024 Crypter File Transfer | ||
* | ||
* This file is part of the Crypter file transfer project. | ||
* | ||
* Crypter is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* The Crypter source code is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* You can be released from the requirements of the aforementioned license | ||
* by purchasing a commercial license. Buying such a license is mandatory | ||
* as soon as you develop commercial activities involving the Crypter source | ||
* code without disclosing the source code of your own applications. | ||
* | ||
* Contact the current copyright holder to discuss commercial license options. | ||
*@ | ||
|
||
@if (_isDataReady) | ||
{ | ||
<h3>Contact Info</h3> | ||
<form> | ||
<div class="mb-3"> | ||
<label for="contactInfoEmailAddress" class="form-label">Email Address</label> | ||
<input @bind="_emailAddressEdit" type="email" class="form-control" id="contactInfoEmailAddress" name="email" placeholder="Email Not Set" readonly="@(!_isEditingEmailAddress)"/> | ||
@if (_isEditingEmailAddress) | ||
{ | ||
@if (!string.IsNullOrEmpty(_emailAddressError)) | ||
{ | ||
<span class="text-danger">@_emailAddressError</span> | ||
} | ||
} | ||
else | ||
{ | ||
@if (_emailAddressVerified) | ||
{ | ||
<span class="text-success">Verified</span> | ||
} | ||
else | ||
{ | ||
<span class="text-danger">Not verified</span> | ||
} | ||
} | ||
</div> | ||
<div class="mb-3" hidden="@(!_isEditingEmailAddress)"> | ||
<label for="contactInfoCurrentPassword" class="form-label">Current Password</label> | ||
<input @bind="_emailAddressPassword" type="password" class="form-control" id="contactInfoCurrentPassword"/> | ||
@if (!string.IsNullOrEmpty(_emailAddressPasswordError)) | ||
{ | ||
<span class="text-danger">@_emailAddressPasswordError</span> | ||
} | ||
</div> | ||
<button type="button" class="btn btn-secondary mx-auto" @onclick="OnEditContactInfoClicked" hidden="@_isEditingEmailAddress">Edit</button> | ||
<button type="button" class="btn btn-secondary mx-auto" @onclick="OnCancelForEditContactInfoClicked" hidden="@(!_isEditingEmailAddress)">Cancel</button> | ||
<button type="button" class="btn btn-primary mx-auto" @onclick="async () => await OnSaveContactInfoClickedAsync()" hidden="@(!_isEditingEmailAddress)">Save</button> | ||
@if (!string.IsNullOrEmpty(_genericEmailAddressError)) | ||
{ | ||
<br/> | ||
<span class="text-danger">@_genericEmailAddressError</span> | ||
} | ||
</form> | ||
|
||
<h3>Password</h3> | ||
<form> | ||
<div class="mb-3" hidden="@(!_isEditingPassword)"> | ||
<label for="passwordChangeOldPassword" class="form-label">Current Password</label> | ||
<input @bind="_passwordChangeOldPassword" type="password" class="form-control" id="passwordChangeOldPassword"/> | ||
@if (!string.IsNullOrEmpty(_oldPasswordError)) | ||
{ | ||
<span class="text-danger">@_oldPasswordError</span> | ||
} | ||
<br/> | ||
|
||
<label for="passwordChangeNewPassword" class="form-label">New Password</label> | ||
<input @bind="_passwordChangeNewPassword" type="password" class="form-control" id="passwordChangeNewPassword"/> | ||
@if (!string.IsNullOrEmpty(_newPasswordError)) | ||
{ | ||
<span class="text-danger">@_newPasswordError</span> | ||
} | ||
<br/> | ||
|
||
<label for="passwordChangeConfirmPassword" class="form-label">Confirm New Password</label> | ||
<input @bind="_passwordChangeConfirmPassword" type="password" class="form-control" id="passwordChangeConfirmPassword"/> | ||
@if (!string.IsNullOrEmpty(_confirmPasswordError)) | ||
{ | ||
<span class="text-danger">@_confirmPasswordError</span> | ||
} | ||
</div> | ||
<div class="mb-3>"> | ||
<button type="submit" class="btn btn-primary" @onclick:preventDefault @onclick="OnChangePasswordClicked" hidden="@(_isEditingPassword)">Change Password</button> | ||
<button type="button" class="btn btn-secondary mx-auto" @onclick="OnCancelForChangePasswordClicked" hidden="@(!_isEditingPassword)">Cancel</button> | ||
<button type="button" class="btn btn-primary mx-auto" @onclick="async () => await OnSavePasswordChangeClickAsync()" hidden="@(!_isEditingPassword)">Save</button> | ||
@if (!string.IsNullOrEmpty(_passwordChangeError)) | ||
{ | ||
<br/> | ||
<span class="text-danger">@_passwordChangeError</span> | ||
} | ||
</div> | ||
</form> | ||
} |
Oops, something went wrong.