From cd926ca8f6744fbeafb858f098ab61230955f798 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Thu, 13 Aug 2020 17:30:10 -0400 Subject: [PATCH] allow user registration for sso (#865) --- src/Core/Services/IUserService.cs | 1 + .../Services/Implementations/UserService.cs | 21 ++++++++++++++----- src/Sql/dbo/Tables/User.sql | 2 +- .../2020-08-13_00_AllowNullMasterPassword.sql | 19 +++++++++++++++++ 4 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 util/Migrator/DbScripts/2020-08-13_00_AllowNullMasterPassword.sql diff --git a/src/Core/Services/IUserService.cs b/src/Core/Services/IUserService.cs index 1e87f3a5b0..86417bfbdc 100644 --- a/src/Core/Services/IUserService.cs +++ b/src/Core/Services/IUserService.cs @@ -19,6 +19,7 @@ namespace Bit.Core.Services Task GetAccountRevisionDateByIdAsync(Guid userId); Task SaveUserAsync(User user, bool push = false); Task RegisterUserAsync(User user, string masterPassword, string token, Guid? orgUserId); + Task RegisterUserAsync(User user); Task SendMasterPasswordHintAsync(string email); Task SendTwoFactorEmailAsync(User user); Task VerifyTwoFactorEmailAsync(User user, string token); diff --git a/src/Core/Services/Implementations/UserService.cs b/src/Core/Services/Implementations/UserService.cs index 33b1b99131..21844ccd19 100644 --- a/src/Core/Services/Implementations/UserService.cs +++ b/src/Core/Services/Implementations/UserService.cs @@ -293,8 +293,19 @@ namespace Bit.Core.Services if (result == IdentityResult.Success) { await _mailService.SendWelcomeEmailAsync(user); - await _referenceEventService.RaiseEventAsync( - new ReferenceEvent(ReferenceEventType.Signup, user)); + await _referenceEventService.RaiseEventAsync(new ReferenceEvent(ReferenceEventType.Signup, user)); + } + + return result; + } + + public async Task RegisterUserAsync(User user) + { + var result = await base.CreateAsync(user); + if (result == IdentityResult.Success) + { + await _mailService.SendWelcomeEmailAsync(user); + await _referenceEventService.RaiseEventAsync(new ReferenceEvent(ReferenceEventType.Signup, user)); } return result; @@ -567,7 +578,7 @@ namespace Bit.Core.Services Logger.LogWarning("Change password failed for user {userId}.", user.Id); return IdentityResult.Failed(_identityErrorDescriber.PasswordMismatch()); } - + public async Task SetPasswordAsync(User user, string newMasterPassword, string key) { if (user == null) @@ -580,7 +591,7 @@ namespace Bit.Core.Services Logger.LogWarning("Change password failed for user {userId} - already has password.", user.Id); return IdentityResult.Failed(_identityErrorDescriber.UserAlreadyHasPassword()); } - + var result = await UpdatePasswordHash(user, newMasterPassword); if (!result.Succeeded) { @@ -807,7 +818,7 @@ namespace Bit.Core.Services PlanName = PremiumPlanId, }); } - catch when(!_globalSettings.SelfHosted) + catch when (!_globalSettings.SelfHosted) { await paymentService.CancelAndRecoverChargesAsync(user); throw; diff --git a/src/Sql/dbo/Tables/User.sql b/src/Sql/dbo/Tables/User.sql index f967b9dc23..52b01ec84a 100644 --- a/src/Sql/dbo/Tables/User.sql +++ b/src/Sql/dbo/Tables/User.sql @@ -3,7 +3,7 @@ [Name] NVARCHAR (50) NULL, [Email] NVARCHAR (50) NOT NULL, [EmailVerified] BIT NOT NULL, - [MasterPassword] NVARCHAR (300) NOT NULL, + [MasterPassword] NVARCHAR (300) NULL, [MasterPasswordHint] NVARCHAR (50) NULL, [Culture] NVARCHAR (10) NOT NULL, [SecurityStamp] NVARCHAR (50) NOT NULL, diff --git a/util/Migrator/DbScripts/2020-08-13_00_AllowNullMasterPassword.sql b/util/Migrator/DbScripts/2020-08-13_00_AllowNullMasterPassword.sql new file mode 100644 index 0000000000..87fb993ea0 --- /dev/null +++ b/util/Migrator/DbScripts/2020-08-13_00_AllowNullMasterPassword.sql @@ -0,0 +1,19 @@ +ALTER TABLE + [dbo].[User] +ALTER COLUMN + [MasterPassword] NVARCHAR (300) NULL +GO + +IF EXISTS(SELECT * FROM sys.views WHERE [Name] = 'UserView') +BEGIN + DROP VIEW [dbo].[UserView] +END +GO + +CREATE VIEW [dbo].[UserView] +AS +SELECT + * +FROM + [dbo].[User] +GO