Skip to content

Commit

Permalink
Implement UI and client service for password change
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Edwards committed Oct 13, 2024
1 parent 8070b57 commit 67daef3
Show file tree
Hide file tree
Showing 8 changed files with 497 additions and 225 deletions.
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);
}
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);
}
}
1 change: 1 addition & 0 deletions Crypter.Web/Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
.AddSingleton<IUserContactInfoSettingsService, UserContactInfoSettingsService>()
.AddSingleton<IUserNotificationSettingsService, UserNotificationSettingsService>()
.AddSingleton<IUserPrivacySettingsService, UserPrivacySettingsService>()
.AddSingleton<IUserPasswordChangeService, UserPasswordChangeService>()
.AddSingleton<TransferHandlerFactory>()
.AddSingleton<Func<ICrypterApiClient>>(sp => sp.GetRequiredService<ICrypterApiClient>);

Expand Down
8 changes: 4 additions & 4 deletions Crypter.Web/Pages/Authenticated/UserSettings.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@*
* Copyright (C) 2023 Crypter File Transfer
* Copyright (C) 2024 Crypter File Transfer
*
* This file is part of the Crypter file transfer project.
*
Expand Down Expand Up @@ -48,7 +48,7 @@
<nav class="mb-3">
<div class="nav nav-tabs" id="nav-tab" role="tablist">
<button class="nav-link active" id="navSettingsPublicDetails-tab" data-bs-toggle="tab" data-bs-target="#navSettingsPublicDetails" type="button" role="tab" aria-controls="navSettingsPublicDetails" aria-selected="true">Public Details</button>
<button class="nav-link" id="navSettingsContactInfo-tab" data-bs-toggle="tab" data-bs-target="#navSettingsContactInfo" type="button" role="tab" aria-controls="navSettingsContactInfo" aria-selected="false">Contact Info</button>
<button class="nav-link" id="navSettingsAccountInfo-tab" data-bs-toggle="tab" data-bs-target="#navSettingsAccountInfo" type="button" role="tab" aria-controls="navSettingsAccountInfo" aria-selected="false">Account Info</button>
<button class="nav-link" id="navSettingsNotificationSettings-tab" data-bs-toggle="tab" data-bs-target="#navSettingsNotificationSettings" type="button" role="tab" aria-controls="navSettingsNotificationSettings" aria-selected="false">Notifications</button>
<button class="nav-link" id="navSettingsPrivacySettings-tab" data-bs-toggle="tab" data-bs-target="#navSettingsPrivacySettings" type="button" role="tab" aria-controls="navSettingsPrivacySettings" aria-selected="false">Privacy</button>
<button class="nav-link" id="navSettingsKeys-tab" data-bs-toggle="tab" data-bs-target="#navSettingsKeys" type="button" role="tab" aria-controls="navSettingsKeys" aria-selected="false">Secret Keys</button>
Expand All @@ -59,8 +59,8 @@
<div class="tab-pane fade show active" id="navSettingsPublicDetails" role="tabpanel" aria-labelledby="navSettingsPublicDetails-tab">
<UserSettingsPublicDetails/>
</div>
<div class="tab-pane fade" id="navSettingsContactInfo" role="tabpanel" aria-labelledby="navSettingsContactInfo-tab">
<UserSettingsContactInfo/>
<div class="tab-pane fade" id="navSettingsAccountInfo" role="tabpanel" aria-labelledby="navSettingsAccountInfo-tab">
<UserSettingsAccountInfo/>
</div>
<div class="tab-pane fade" id="navSettingsNotificationSettings" role="tabpanel" aria-labelledby="navSettingsNotificationSettings-tab">
<UserSettingsNotificationSettings/>
Expand Down
108 changes: 108 additions & 0 deletions Crypter.Web/Shared/UserSettings/UserSettingsAccountInfo.razor
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>
}
Loading

0 comments on commit 67daef3

Please sign in to comment.