-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Auth/poc/master password service example #7349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Patrick-Pimentel-Bitwarden
wants to merge
29
commits into
main
Choose a base branch
from
auth/poc/master-password-service-example
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
5ad515b
feat(master-password): Master Password Service - Initial implementation.
Patrick-Pimentel-Bitwarden 41bdd72
misc changes, don't put me up
Patrick-Pimentel-Bitwarden 39c61c7
feat(master-password): Master Password Service - Added data payloads.…
Patrick-Pimentel-Bitwarden 2b83f8c
Merge remote-tracking branch 'origin' into auth/poc/master-password-s…
Patrick-Pimentel-Bitwarden d5245b6
feat(master-password): Master Password Service - Changed a comment or…
Patrick-Pimentel-Bitwarden 2c2ef07
feat(master-password): Master Password Service - Master password serv…
Patrick-Pimentel-Bitwarden 53d4806
feat(master-password): Master Password Service - Added in more fixes …
Patrick-Pimentel-Bitwarden 380035d
Merge branch 'main' into auth/poc/master-password-service-example
Patrick-Pimentel-Bitwarden f31c6c4
feat(master-password): Master Password Service - Added in more change…
Patrick-Pimentel-Bitwarden 0cd0954
Merge remote-tracking branch 'origin' into auth/poc/master-password-s…
Patrick-Pimentel-Bitwarden 909ea5a
feat(master-password): Master Password Service - Lots of misc changes…
Patrick-Pimentel-Bitwarden 3f6a1e0
feat(master-password): Master Password Service - Removed commands and…
Patrick-Pimentel-Bitwarden 8385cae
feat(master-password): Master Password Service - Fixed validation error
Patrick-Pimentel-Bitwarden cb98256
feat(master-password): Master Password Service - Made changed to the …
Patrick-Pimentel-Bitwarden 8fb2b71
feat(master-password): Master Password Service - Fixes to the self se…
Patrick-Pimentel-Bitwarden c6331ba
feat(master-password): Master Password Service - Added update temp pa…
Patrick-Pimentel-Bitwarden 9252b93
Merge remote-tracking branch 'origin' into auth/poc/master-password-s…
Patrick-Pimentel-Bitwarden 6f80b6d
test(master-password): Master Password Service - Updated the change k…
Patrick-Pimentel-Bitwarden d527c60
fix(master-password): Master Password Service - Updated update-temp-p…
Patrick-Pimentel-Bitwarden 6feb4ce
fix(master-password): Master Password Service - Updated accounts cont…
Patrick-Pimentel-Bitwarden 4d942f3
fix(master-password): Master Password Service - Fixed master password…
Patrick-Pimentel-Bitwarden d2b93c0
feat(master-password): Master Password Service - Did some renames and…
Patrick-Pimentel-Bitwarden 04a6846
feat(master-password): Master Password Service - Added in self servic…
Patrick-Pimentel-Bitwarden 3b8ce57
feat(master-password): Master Password Service - Added in change kdf …
Patrick-Pimentel-Bitwarden b9c9060
test(master-password): Master Password Service - Fixed lots of tests …
Patrick-Pimentel-Bitwarden 510b417
test(master-password): Master Password Service - Fixed name of comman…
Patrick-Pimentel-Bitwarden 86da309
fix(master-password): Master Password Service - Removed redundant val…
Patrick-Pimentel-Bitwarden bde6a8b
Merge remote-tracking branch 'origin' into auth/poc/master-password-s…
Patrick-Pimentel-Bitwarden aeb77a8
docs(master-password): Master Password Service - Final changes before…
Patrick-Pimentel-Bitwarden File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
| using Bit.Core.Auth.Models.Api.Request.Accounts; | ||
| using Bit.Core.Auth.Services; | ||
| using Bit.Core.Auth.UserFeatures.TdeOffboardingPassword.Interfaces; | ||
| using Bit.Core.Auth.UserFeatures.TempPassword.Interfaces; | ||
| using Bit.Core.Auth.UserFeatures.TwoFactorAuth.Interfaces; | ||
| using Bit.Core.Auth.UserFeatures.UserMasterPassword.Interfaces; | ||
| using Bit.Core.Enums; | ||
|
|
@@ -25,64 +26,47 @@ | |
| using Bit.Core.Services; | ||
| using Bit.Core.Utilities; | ||
| using Microsoft.AspNetCore.Authorization; | ||
| using Microsoft.AspNetCore.Identity; | ||
| using Microsoft.AspNetCore.Mvc; | ||
|
|
||
| namespace Bit.Api.Auth.Controllers; | ||
|
|
||
| [Route("accounts")] | ||
| [Authorize(Policies.Application)] | ||
| public class AccountsController : Controller | ||
| public class AccountsController( | ||
| IOrganizationService organizationService, | ||
| IOrganizationUserRepository organizationUserRepository, | ||
| IProviderUserRepository providerUserRepository, | ||
| IUserService userService, | ||
| ISelfServicePasswordChangeCommand selfServicePasswordChangeCommand, | ||
| IPolicyService policyService, | ||
| IFinishSsoJitProvisionMasterPasswordCommand finishSsoJitProvisionMasterPasswordCommand, | ||
| ISetInitialMasterPasswordCommandV1 setInitialMasterPasswordCommandV1, | ||
| ITdeSetPasswordCommand tdeSetPasswordCommand, | ||
| ITdeOffboardingPasswordCommand tdeOffboardingPasswordCommand, | ||
| IReplaceAdminSetTemporaryPasswordCommand replaceAdminSetTemporaryPasswordCommand, | ||
| ITwoFactorIsEnabledQuery twoFactorIsEnabledQuery, | ||
| IUserAccountKeysQuery userAccountKeysQuery, | ||
| ITwoFactorEmailService twoFactorEmailService, | ||
| IChangeKdfCommand changeKdfCommand, | ||
| IUserRepository userRepository) : Controller | ||
| { | ||
| private readonly IOrganizationService _organizationService; | ||
| private readonly IOrganizationUserRepository _organizationUserRepository; | ||
| private readonly IProviderUserRepository _providerUserRepository; | ||
| private readonly IUserService _userService; | ||
| private readonly IPolicyService _policyService; | ||
| private readonly ISetInitialMasterPasswordCommandV1 _setInitialMasterPasswordCommandV1; | ||
| private readonly IFinishSsoJitProvisionMasterPasswordCommand _finishSsoJitProvisionMasterPasswordCommand; | ||
| private readonly ITdeSetPasswordCommand _tdeSetPasswordCommand; | ||
| private readonly ITdeOffboardingPasswordCommand _tdeOffboardingPasswordCommand; | ||
| private readonly ITwoFactorIsEnabledQuery _twoFactorIsEnabledQuery; | ||
| private readonly IFeatureService _featureService; | ||
| private readonly IUserAccountKeysQuery _userAccountKeysQuery; | ||
| private readonly ITwoFactorEmailService _twoFactorEmailService; | ||
| private readonly IChangeKdfCommand _changeKdfCommand; | ||
| private readonly IUserRepository _userRepository; | ||
|
|
||
| public AccountsController( | ||
| IOrganizationService organizationService, | ||
| IOrganizationUserRepository organizationUserRepository, | ||
| IProviderUserRepository providerUserRepository, | ||
| IUserService userService, | ||
| IPolicyService policyService, | ||
| IFinishSsoJitProvisionMasterPasswordCommand finishSsoJitProvisionMasterPasswordCommand, | ||
| ISetInitialMasterPasswordCommandV1 setInitialMasterPasswordCommandV1, | ||
| ITdeSetPasswordCommand tdeSetPasswordCommand, | ||
| ITdeOffboardingPasswordCommand tdeOffboardingPasswordCommand, | ||
| ITwoFactorIsEnabledQuery twoFactorIsEnabledQuery, | ||
| IFeatureService featureService, | ||
| IUserAccountKeysQuery userAccountKeysQuery, | ||
| ITwoFactorEmailService twoFactorEmailService, | ||
| IChangeKdfCommand changeKdfCommand, | ||
| IUserRepository userRepository | ||
| ) | ||
| { | ||
| _organizationService = organizationService; | ||
| _organizationUserRepository = organizationUserRepository; | ||
| _providerUserRepository = providerUserRepository; | ||
| _userService = userService; | ||
| _policyService = policyService; | ||
| _finishSsoJitProvisionMasterPasswordCommand = finishSsoJitProvisionMasterPasswordCommand; | ||
| _setInitialMasterPasswordCommandV1 = setInitialMasterPasswordCommandV1; | ||
| _tdeSetPasswordCommand = tdeSetPasswordCommand; | ||
| _tdeOffboardingPasswordCommand = tdeOffboardingPasswordCommand; | ||
| _twoFactorIsEnabledQuery = twoFactorIsEnabledQuery; | ||
| _featureService = featureService; | ||
| _userAccountKeysQuery = userAccountKeysQuery; | ||
| _twoFactorEmailService = twoFactorEmailService; | ||
| _changeKdfCommand = changeKdfCommand; | ||
| _userRepository = userRepository; | ||
| } | ||
| private readonly IOrganizationService _organizationService = organizationService; | ||
| private readonly IOrganizationUserRepository _organizationUserRepository = organizationUserRepository; | ||
| private readonly IProviderUserRepository _providerUserRepository = providerUserRepository; | ||
| private readonly IUserService _userService = userService; | ||
| private readonly ISelfServicePasswordChangeCommand _selfServicePasswordChangeCommand = selfServicePasswordChangeCommand; | ||
| private readonly IPolicyService _policyService = policyService; | ||
| private readonly ISetInitialMasterPasswordCommandV1 _setInitialMasterPasswordCommandV1 = setInitialMasterPasswordCommandV1; | ||
| private readonly IFinishSsoJitProvisionMasterPasswordCommand _finishSsoJitProvisionMasterPasswordCommand = finishSsoJitProvisionMasterPasswordCommand; | ||
| private readonly ITdeSetPasswordCommand _tdeSetPasswordCommand = tdeSetPasswordCommand; | ||
| private readonly ITdeOffboardingPasswordCommand _tdeOffboardingPasswordCommand = tdeOffboardingPasswordCommand; | ||
| private readonly IReplaceAdminSetTemporaryPasswordCommand _replaceAdminSetTemporaryPasswordCommand = replaceAdminSetTemporaryPasswordCommand; | ||
| private readonly ITwoFactorIsEnabledQuery _twoFactorIsEnabledQuery = twoFactorIsEnabledQuery; | ||
| private readonly IUserAccountKeysQuery _userAccountKeysQuery = userAccountKeysQuery; | ||
| private readonly ITwoFactorEmailService _twoFactorEmailService = twoFactorEmailService; | ||
| private readonly IChangeKdfCommand _changeKdfCommand = changeKdfCommand; | ||
| private readonly IUserRepository _userRepository = userRepository; | ||
|
|
||
|
|
||
| [HttpPost("password-hint")] | ||
|
|
@@ -188,6 +172,14 @@ public async Task PostVerifyEmailToken([FromBody] VerifyEmailRequestModel model) | |
| throw new BadRequestException(ModelState); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// For a user updating an existing password. | ||
| /// | ||
| /// If calling this when a user does not have a master password, it will fail. | ||
| /// </summary> | ||
| /// <param name="model"></param> | ||
| /// <exception cref="UnauthorizedAccessException"></exception> | ||
| /// <exception cref="BadRequestException"></exception> | ||
| [HttpPost("password")] | ||
| public async Task PostPassword([FromBody] PasswordRequestModel model) | ||
| { | ||
|
|
@@ -197,8 +189,23 @@ public async Task PostPassword([FromBody] PasswordRequestModel model) | |
| throw new UnauthorizedAccessException(); | ||
| } | ||
|
|
||
| var result = await _userService.ChangePasswordAsync(user, model.MasterPasswordHash, | ||
| model.NewMasterPasswordHash, model.MasterPasswordHint, model.Key); | ||
| IdentityResult result; | ||
| if (model.RequestHasNewDataTypes()) | ||
| { | ||
| result = await _selfServicePasswordChangeCommand.ChangePasswordAsync( | ||
| user, | ||
| model.MasterPasswordHash, | ||
| model.UnlockData!.ToData(), | ||
| model.AuthenticationData!.ToData(), | ||
| model.MasterPasswordHint); | ||
| } | ||
| // To be removed in PM-33141 | ||
| else | ||
| { | ||
| result = await _userService.ChangePasswordAsync(user, model.MasterPasswordHash, | ||
| model.NewMasterPasswordHash, model.MasterPasswordHint, model.Key); | ||
| } | ||
|
|
||
| if (result.Succeeded) | ||
| { | ||
| return; | ||
|
|
@@ -222,7 +229,7 @@ public async Task PostSetPasswordAsync([FromBody] SetInitialPasswordRequestModel | |
| throw new UnauthorizedAccessException(); | ||
| } | ||
|
|
||
| if (model.IsV2Request()) | ||
| if (model.RequestHasNewDataTypes()) | ||
| { | ||
| if (model.IsTdeSetPasswordRequest()) | ||
| { | ||
|
|
@@ -235,7 +242,7 @@ public async Task PostSetPasswordAsync([FromBody] SetInitialPasswordRequestModel | |
| } | ||
| else | ||
| { | ||
| // TODO removed with https://bitwarden.atlassian.net/browse/PM-27327 | ||
| // To be removed in PM-33141 | ||
| try | ||
| { | ||
| user = model.ToUser(user); | ||
|
|
@@ -248,8 +255,8 @@ public async Task PostSetPasswordAsync([FromBody] SetInitialPasswordRequestModel | |
|
|
||
| var result = await _setInitialMasterPasswordCommandV1.SetInitialMasterPasswordAsync( | ||
| user, | ||
| model.MasterPasswordHash, | ||
| model.Key, | ||
| model.MasterPasswordHash!, | ||
| model.Key!, | ||
| model.OrgIdentifier); | ||
|
|
||
| if (result.Succeeded) | ||
|
|
@@ -288,7 +295,7 @@ public async Task<MasterPasswordPolicyResponseModel> PostVerifyPassword([FromBod | |
| } | ||
|
|
||
| [HttpPost("kdf")] | ||
| public async Task PostKdf([FromBody] PasswordRequestModel model) | ||
| public async Task PostKdf([FromBody] ChangeKdfRequestModel model) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to double check if this will break anything with the sdk |
||
| { | ||
| var user = await _userService.GetUserByPrincipalAsync(User); | ||
| if (user == null) | ||
|
|
@@ -301,7 +308,10 @@ public async Task PostKdf([FromBody] PasswordRequestModel model) | |
| throw new BadRequestException("AuthenticationData and UnlockData must be provided."); | ||
| } | ||
|
|
||
| var result = await _changeKdfCommand.ChangeKdfAsync(user, model.MasterPasswordHash, model.AuthenticationData.ToData(), model.UnlockData.ToData()); | ||
| var result = await _changeKdfCommand.ChangeKdfAsync(user, | ||
| model.MasterPasswordHash, | ||
| model.AuthenticationData.ToData(), | ||
| model.UnlockData.ToData()); | ||
| if (result.Succeeded) | ||
| { | ||
| return; | ||
|
|
@@ -646,7 +656,25 @@ public async Task PutUpdateTempPasswordAsync([FromBody] UpdateTempPasswordReques | |
| throw new UnauthorizedAccessException(); | ||
| } | ||
|
|
||
| var result = await _userService.UpdateTempPasswordAsync(user, model.NewMasterPasswordHash, model.Key, model.MasterPasswordHint); | ||
| IdentityResult result; | ||
| if (model.RequestHasNewDataTypes()) | ||
| { | ||
| result = await _replaceAdminSetTemporaryPasswordCommand.Replace( | ||
| user, | ||
| model.UnlockData!.ToData(), | ||
| model.AuthenticationData!.ToData(), | ||
| model.MasterPasswordHint); | ||
| } | ||
| // To be removed in PM-33141 | ||
| else | ||
| { | ||
| result = await _userService.UpdateTempPasswordAsync( | ||
| user, | ||
| model.NewMasterPasswordHash, | ||
| model.Key, | ||
| model.MasterPasswordHint); | ||
| } | ||
|
|
||
| if (result.Succeeded) | ||
| { | ||
| return; | ||
|
|
@@ -669,7 +697,17 @@ public async Task PutUpdateTdePasswordAsync([FromBody] UpdateTdeOffboardingPassw | |
| throw new UnauthorizedAccessException(); | ||
| } | ||
|
|
||
| var result = await _tdeOffboardingPasswordCommand.UpdateTdeOffboardingPasswordAsync(user, model.NewMasterPasswordHash, model.Key, model.MasterPasswordHint); | ||
| IdentityResult result; | ||
| if (model.RequestHasNewDataTypes()) | ||
| { | ||
| result = await _tdeOffboardingPasswordCommand.UpdateTdeOffboardingPasswordAsync(user, model.UnlockData!.ToData(), model.AuthenticationData!.ToData(), model.MasterPasswordHint); | ||
| } | ||
| // To be removed in PM-33141 | ||
| else | ||
| { | ||
| result = await _tdeOffboardingPasswordCommand.UpdateTdeOffboardingPasswordAsync(user, model.NewMasterPasswordHash!, model.Key!, model.MasterPasswordHint); | ||
| } | ||
|
|
||
| if (result.Succeeded) | ||
| { | ||
| return; | ||
|
|
||
This file contains hidden or 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
45 changes: 45 additions & 0 deletions
45
src/Api/Auth/Models/Request/Accounts/ChangeKdfRequestModel.cs
This file contains hidden or 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,45 @@ | ||
| using System.ComponentModel.DataAnnotations; | ||
| using Bit.Core.KeyManagement.Models.Api.Request; | ||
|
|
||
| namespace Bit.Api.Auth.Models.Request.Accounts; | ||
|
|
||
| /// <summary> | ||
| /// We recognize this probably doesn't need the obsolete fields but we are leaving it in to be cleaned up anywho | ||
| /// later. | ||
| /// </summary> | ||
| public class ChangeKdfRequestModel : IValidatableObject | ||
| { | ||
| [Required] | ||
| public required string MasterPasswordHash { get; set; } | ||
| [Obsolete("To be removed in PM-33141")] | ||
| [StringLength(300)] | ||
| public string? NewMasterPasswordHash { get; set; } | ||
| [Obsolete("To be removed in PM-33141")] | ||
| public string? Key { get; set; } | ||
|
|
||
| // Should be made required in PM-33141 | ||
| public MasterPasswordAuthenticationDataRequestModel? AuthenticationData { get; set; } | ||
| // Should be made required in PM-33141 | ||
| public MasterPasswordUnlockDataRequestModel? UnlockData { get; set; } | ||
|
|
||
| // To be removed in PM-33141 | ||
| public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) | ||
| { | ||
| var hasNewPayloads = AuthenticationData is not null && UnlockData is not null; | ||
| var hasLegacyPayloads = NewMasterPasswordHash is not null && Key is not null; | ||
|
|
||
| if (hasNewPayloads && hasLegacyPayloads) | ||
| { | ||
| yield return new ValidationResult( | ||
| "Cannot provide both new payloads (UnlockData/AuthenticationData) and legacy payloads (NewMasterPasswordHash/Key).", | ||
| [nameof(AuthenticationData), nameof(UnlockData), nameof(NewMasterPasswordHash), nameof(Key)]); | ||
| } | ||
|
|
||
| if (!hasNewPayloads && !hasLegacyPayloads) | ||
| { | ||
| yield return new ValidationResult( | ||
| "Must provide either new payloads (UnlockData/AuthenticationData) or legacy payloads (NewMasterPasswordHash/Key).", | ||
| [nameof(AuthenticationData), nameof(UnlockData), nameof(NewMasterPasswordHash), nameof(Key)]); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.