1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-18 11:38:15 -05:00

change email/password adjustments

This commit is contained in:
Kyle Spearrin 2017-04-17 14:53:07 -04:00
parent 184cf0c0df
commit e732996cd9
12 changed files with 98 additions and 19 deletions

View File

@ -82,7 +82,8 @@ namespace Bit.Api.Controllers
// NOTE: It is assumed that the eventual repository call will make sure the updated // NOTE: It is assumed that the eventual repository call will make sure the updated
// ciphers belong to user making this call. Therefore, no check is done here. // ciphers belong to user making this call. Therefore, no check is done here.
var ciphers = model.Ciphers.Select(c => c.ToCipher(user.Id)); var ciphers = model.Data.Ciphers.Select(c => c.ToCipher(user.Id));
var folders = model.Data.Folders.Select(c => c.ToFolder(user.Id));
var result = await _userService.ChangeEmailAsync( var result = await _userService.ChangeEmailAsync(
user, user,
@ -90,7 +91,9 @@ namespace Bit.Api.Controllers
model.NewEmail, model.NewEmail,
model.NewMasterPasswordHash, model.NewMasterPasswordHash,
model.Token, model.Token,
ciphers); ciphers,
folders,
model.Data.PrivateKey);
if(result.Succeeded) if(result.Succeeded)
{ {
@ -114,13 +117,16 @@ namespace Bit.Api.Controllers
// NOTE: It is assumed that the eventual repository call will make sure the updated // NOTE: It is assumed that the eventual repository call will make sure the updated
// ciphers belong to user making this call. Therefore, no check is done here. // ciphers belong to user making this call. Therefore, no check is done here.
var ciphers = model.Ciphers.Select(c => c.ToCipher(user.Id)); var ciphers = model.Data.Ciphers.Select(c => c.ToCipher(user.Id));
var folders = model.Data.Folders.Select(c => c.ToFolder(user.Id));
var result = await _userService.ChangePasswordAsync( var result = await _userService.ChangePasswordAsync(
user, user,
model.MasterPasswordHash, model.MasterPasswordHash,
model.NewMasterPasswordHash, model.NewMasterPasswordHash,
ciphers); ciphers,
folders,
model.Data.PrivateKey);
if(result.Succeeded) if(result.Succeeded)
{ {

View File

@ -17,6 +17,6 @@ namespace Bit.Core.Models.Api
[Required] [Required]
public string Token { get; set; } public string Token { get; set; }
[Required] [Required]
public CipherRequestModel[] Ciphers { get; set; } public DataReloadRequestModel Data { get; set; }
} }
} }

View File

@ -11,6 +11,6 @@ namespace Bit.Core.Models.Api
[StringLength(300)] [StringLength(300)]
public string NewMasterPasswordHash { get; set; } public string NewMasterPasswordHash { get; set; }
[Required] [Required]
public CipherRequestModel[] Ciphers { get; set; } public DataReloadRequestModel Data { get; set; }
} }
} }

View File

@ -18,8 +18,6 @@ namespace Bit.Core.Models.Api
public string Id { get; set; } public string Id { get; set; }
[StringLength(36)] [StringLength(36)]
public string OrganizationId { get; set; } public string OrganizationId { get; set; }
[StringLength(36)]
public string FolderId { get; set; }
[Required] [Required]
[EncryptedString] [EncryptedString]
[StringLength(300)] [StringLength(300)]

View File

@ -0,0 +1,15 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace Bit.Core.Models.Api
{
public class DataReloadRequestModel
{
[Required]
public IEnumerable<LoginWithIdRequestModel> Ciphers { get; set; }
[Required]
public IEnumerable<FolderWithIdRequestModel> Folders { get; set; }
[Required]
public string PrivateKey { get; set; }
}
}

View File

@ -27,4 +27,18 @@ namespace Bit.Core.Models.Api
return existingFolder; return existingFolder;
} }
} }
public class FolderWithIdRequestModel : FolderRequestModel
{
public Guid Id { get; set; }
public new Folder ToFolder(Guid userId)
{
return ToFolder(new Folder
{
UserId = userId,
Id = Id
});
}
}
} }

View File

@ -3,6 +3,7 @@ using System.ComponentModel.DataAnnotations;
using Bit.Core.Utilities; using Bit.Core.Utilities;
using Newtonsoft.Json; using Newtonsoft.Json;
using Core.Models.Data; using Core.Models.Data;
using Bit.Core.Models.Table;
namespace Bit.Core.Models.Api namespace Bit.Core.Models.Api
{ {
@ -48,4 +49,18 @@ namespace Bit.Core.Models.Api
return existingLogin; return existingLogin;
} }
} }
public class LoginWithIdRequestModel : LoginRequestModel
{
public Guid Id { get; set; }
public Cipher ToCipher(Guid userId)
{
return ToCipherDetails(new CipherDetails
{
UserId = userId,
Id = Id
});
}
}
} }

View File

@ -18,7 +18,7 @@ namespace Bit.Core.Repositories
Task UpsertAsync(CipherDetails cipher); Task UpsertAsync(CipherDetails cipher);
Task ReplaceAsync(Cipher obj, IEnumerable<Guid> subvaultIds); Task ReplaceAsync(Cipher obj, IEnumerable<Guid> subvaultIds);
Task UpdatePartialAsync(Guid id, Guid userId, Guid? folderId, bool favorite); Task UpdatePartialAsync(Guid id, Guid userId, Guid? folderId, bool favorite);
Task UpdateUserEmailPasswordAndCiphersAsync(User user, IEnumerable<Cipher> ciphers); Task UpdateUserEmailPasswordAndCiphersAsync(User user, IEnumerable<Cipher> ciphers, IEnumerable<Folder> folders);
Task CreateAsync(IEnumerable<Cipher> ciphers, IEnumerable<Folder> folders); Task CreateAsync(IEnumerable<Cipher> ciphers, IEnumerable<Folder> folders);
} }
} }

View File

@ -154,7 +154,7 @@ namespace Bit.Core.Repositories.SqlServer
} }
} }
public Task UpdateUserEmailPasswordAndCiphersAsync(User user, IEnumerable<Cipher> ciphers) public Task UpdateUserEmailPasswordAndCiphersAsync(User user, IEnumerable<Cipher> ciphers, IEnumerable<Folder> folders)
{ {
if(ciphers.Count() == 0) if(ciphers.Count() == 0)
{ {
@ -179,6 +179,7 @@ namespace Bit.Core.Repositories.SqlServer
cmd.Parameters.Add("@EmailVerified", SqlDbType.NVarChar).Value = user.EmailVerified; cmd.Parameters.Add("@EmailVerified", SqlDbType.NVarChar).Value = user.EmailVerified;
cmd.Parameters.Add("@MasterPassword", SqlDbType.NVarChar).Value = user.MasterPassword; cmd.Parameters.Add("@MasterPassword", SqlDbType.NVarChar).Value = user.MasterPassword;
cmd.Parameters.Add("@SecurityStamp", SqlDbType.NVarChar).Value = user.SecurityStamp; cmd.Parameters.Add("@SecurityStamp", SqlDbType.NVarChar).Value = user.SecurityStamp;
cmd.Parameters.Add("@PrivateKey", SqlDbType.VarChar).Value = user.PrivateKey;
cmd.Parameters.Add("@RevisionDate", SqlDbType.DateTime2).Value = user.RevisionDate; cmd.Parameters.Add("@RevisionDate", SqlDbType.DateTime2).Value = user.RevisionDate;
cmd.ExecuteNonQuery(); cmd.ExecuteNonQuery();
} }
@ -188,7 +189,11 @@ namespace Bit.Core.Repositories.SqlServer
var sqlCreateTemp = @" var sqlCreateTemp = @"
SELECT TOP 0 * SELECT TOP 0 *
INTO #TempCipher INTO #TempCipher
FROM [dbo].[Cipher]"; FROM [dbo].[Cipher]
SELECT TOP 0 *
INTO #TempFolder
FROM [dbo].[Folder]";
using(var cmd = new SqlCommand(sqlCreateTemp, connection, transaction)) using(var cmd = new SqlCommand(sqlCreateTemp, connection, transaction))
{ {
@ -204,6 +209,13 @@ namespace Bit.Core.Repositories.SqlServer
bulkCopy.WriteToServer(dataTable); bulkCopy.WriteToServer(dataTable);
} }
using(var bulkCopy = new SqlBulkCopy(connection, SqlBulkCopyOptions.KeepIdentity, transaction))
{
bulkCopy.DestinationTableName = "#TempFolder";
var dataTable = BuildFoldersTable(folders);
bulkCopy.WriteToServer(dataTable);
}
// 4. Insert into real tables from temp tables and clean up. // 4. Insert into real tables from temp tables and clean up.
var sqlUpdate = @" var sqlUpdate = @"
@ -219,7 +231,20 @@ namespace Bit.Core.Repositories.SqlServer
WHERE WHERE
C.[UserId] = @UserId C.[UserId] = @UserId
DROP TABLE #TempCipher"; UPDATE
[dbo].[Folder]
SET
[Name] = TF.[Name],
[RevisionDate] = TF.[RevisionDate]
FROM
[dbo].[Folder] F
INNER JOIN
#TempFolder TF ON F.Id = TF.Id
WHERE
F.[UserId] = @UserId
DROP TABLE #TempCipher
DROP TABLE #TempFolder";
using(var cmd = new SqlCommand(sqlUpdate, connection, transaction)) using(var cmd = new SqlCommand(sqlUpdate, connection, transaction))
{ {

View File

@ -18,8 +18,10 @@ namespace Bit.Core.Services
Task<IdentityResult> RegisterUserAsync(User user, string masterPassword); Task<IdentityResult> RegisterUserAsync(User user, string masterPassword);
Task SendMasterPasswordHintAsync(string email); Task SendMasterPasswordHintAsync(string email);
Task InitiateEmailChangeAsync(User user, string newEmail); Task InitiateEmailChangeAsync(User user, string newEmail);
Task<IdentityResult> ChangeEmailAsync(User user, string masterPassword, string newEmail, string newMasterPassword, string token, IEnumerable<Cipher> ciphers); Task<IdentityResult> ChangeEmailAsync(User user, string masterPassword, string newEmail, string newMasterPassword,
Task<IdentityResult> ChangePasswordAsync(User user, string currentMasterPasswordHash, string newMasterPasswordHash, IEnumerable<Cipher> ciphers); string token, IEnumerable<Cipher> ciphers, IEnumerable<Folder> folders, string privateKey);
Task<IdentityResult> ChangePasswordAsync(User user, string currentMasterPasswordHash, string newMasterPasswordHash,
IEnumerable<Cipher> ciphers, IEnumerable<Folder> folders, string privateKey);
Task<IdentityResult> RefreshSecurityStampAsync(User user, string masterPasswordHash); Task<IdentityResult> RefreshSecurityStampAsync(User user, string masterPasswordHash);
Task GetTwoFactorAsync(User user, Enums.TwoFactorProviderType provider); Task GetTwoFactorAsync(User user, Enums.TwoFactorProviderType provider);
Task<bool> RecoverTwoFactorAsync(string email, string masterPassword, string recoveryCode); Task<bool> RecoverTwoFactorAsync(string email, string masterPassword, string recoveryCode);

View File

@ -170,7 +170,7 @@ namespace Bit.Core.Services
} }
public async Task<IdentityResult> ChangeEmailAsync(User user, string masterPassword, string newEmail, public async Task<IdentityResult> ChangeEmailAsync(User user, string masterPassword, string newEmail,
string newMasterPassword, string token, IEnumerable<Cipher> ciphers) string newMasterPassword, string token, IEnumerable<Cipher> ciphers, IEnumerable<Folder> folders, string privateKey)
{ {
var verifyPasswordResult = _passwordHasher.VerifyHashedPassword(user, user.MasterPassword, masterPassword); var verifyPasswordResult = _passwordHasher.VerifyHashedPassword(user, user.MasterPassword, masterPassword);
if(verifyPasswordResult == PasswordVerificationResult.Failed) if(verifyPasswordResult == PasswordVerificationResult.Failed)
@ -199,10 +199,11 @@ namespace Bit.Core.Services
user.Email = newEmail; user.Email = newEmail;
user.EmailVerified = true; user.EmailVerified = true;
user.RevisionDate = user.AccountRevisionDate = DateTime.UtcNow; user.RevisionDate = user.AccountRevisionDate = DateTime.UtcNow;
user.PrivateKey = privateKey;
if(ciphers.Any()) if(ciphers.Any())
{ {
await _cipherRepository.UpdateUserEmailPasswordAndCiphersAsync(user, ciphers); await _cipherRepository.UpdateUserEmailPasswordAndCiphersAsync(user, ciphers, folders);
} }
else else
{ {
@ -218,7 +219,7 @@ namespace Bit.Core.Services
} }
public async Task<IdentityResult> ChangePasswordAsync(User user, string masterPassword, string newMasterPassword, public async Task<IdentityResult> ChangePasswordAsync(User user, string masterPassword, string newMasterPassword,
IEnumerable<Cipher> ciphers) IEnumerable<Cipher> ciphers, IEnumerable<Folder> folders, string privateKey)
{ {
if(user == null) if(user == null)
{ {
@ -234,9 +235,10 @@ namespace Bit.Core.Services
} }
user.RevisionDate = user.AccountRevisionDate = DateTime.UtcNow; user.RevisionDate = user.AccountRevisionDate = DateTime.UtcNow;
user.PrivateKey = privateKey;
if(ciphers.Any()) if(ciphers.Any())
{ {
await _cipherRepository.UpdateUserEmailPasswordAndCiphersAsync(user, ciphers); await _cipherRepository.UpdateUserEmailPasswordAndCiphersAsync(user, ciphers, folders);
} }
else else
{ {

View File

@ -4,6 +4,7 @@
@EmailVerified BIT, @EmailVerified BIT,
@MasterPassword NVARCHAR(300), @MasterPassword NVARCHAR(300),
@SecurityStamp NVARCHAR(50), @SecurityStamp NVARCHAR(50),
@PrivateKey VARCHAR(MAX),
@RevisionDate DATETIME2(7) @RevisionDate DATETIME2(7)
AS AS
BEGIN BEGIN
@ -16,6 +17,7 @@ BEGIN
[EmailVerified] = @EmailVerified, [EmailVerified] = @EmailVerified,
[MasterPassword] = @MasterPassword, [MasterPassword] = @MasterPassword,
[SecurityStamp] = @SecurityStamp, [SecurityStamp] = @SecurityStamp,
[PrivateKey] = @PrivateKey,
[RevisionDate] = @RevisionDate, [RevisionDate] = @RevisionDate,
[AccountRevisionDate] = @RevisionDate [AccountRevisionDate] = @RevisionDate
WHERE WHERE