1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-09 07:08:15 -05:00

"user key" schema and api changes

This commit is contained in:
Kyle Spearrin 2017-05-31 09:54:32 -04:00
parent bdce4064b2
commit a01d5d9a51
16 changed files with 97 additions and 59 deletions

View File

@ -78,23 +78,8 @@ namespace Bit.Api.Controllers
public async Task PutEmail([FromBody]EmailRequestModel model) public async Task PutEmail([FromBody]EmailRequestModel model)
{ {
var user = await _userService.GetUserByPrincipalAsync(User); var user = await _userService.GetUserByPrincipalAsync(User);
var result = await _userService.ChangeEmailAsync(user, model.MasterPasswordHash, model.NewEmail,
// NOTE: It is assumed that the eventual repository call will make sure the updated model.NewMasterPasswordHash, model.Token, model.Key);
// ciphers belong to user making this call. Therefore, no check is done here.
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(
user,
model.MasterPasswordHash,
model.NewEmail,
model.NewMasterPasswordHash,
model.Token,
ciphers,
folders,
model.Data.PrivateKey);
if(result.Succeeded) if(result.Succeeded)
{ {
return; return;
@ -112,22 +97,43 @@ namespace Bit.Api.Controllers
[HttpPut("password")] [HttpPut("password")]
[HttpPost("password")] [HttpPost("password")]
public async Task PutPassword([FromBody]PasswordRequestModel model) public async Task PutPassword([FromBody]PasswordRequestModel model)
{
var user = await _userService.GetUserByPrincipalAsync(User);
var result = await _userService.ChangePasswordAsync(user, model.MasterPasswordHash,
model.NewMasterPasswordHash, model.Key);
if(result.Succeeded)
{
return;
}
foreach(var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
await Task.Delay(2000);
throw new BadRequestException(ModelState);
}
[HttpPut("key")]
[HttpPost("key")]
public async Task PutKey([FromBody]UpdateKeyRequestModel model)
{ {
var user = await _userService.GetUserByPrincipalAsync(User); var user = await _userService.GetUserByPrincipalAsync(User);
// 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.Data.Ciphers.Select(c => c.ToCipher(user.Id)); var ciphers = model.Ciphers.Select(c => c.ToCipher(user.Id));
var folders = model.Data.Folders.Select(c => c.ToFolder(user.Id)); var folders = model.Folders.Select(c => c.ToFolder(user.Id));
var result = await _userService.ChangePasswordAsync( var result = await _userService.UpdateKeyAsync(
user, user,
model.MasterPasswordHash, model.MasterPasswordHash,
model.NewMasterPasswordHash, model.Key,
model.PrivateKey,
ciphers, ciphers,
folders, folders);
model.Data.PrivateKey);
if(result.Succeeded) if(result.Succeeded)
{ {

View File

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

View File

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

View File

@ -16,6 +16,7 @@ namespace Bit.Core.Models.Api
public string MasterPasswordHash { get; set; } public string MasterPasswordHash { get; set; }
[StringLength(50)] [StringLength(50)]
public string MasterPasswordHint { get; set; } public string MasterPasswordHint { get; set; }
public string Key { get; set; }
public KeysRequestModel Keys { get; set; } public KeysRequestModel Keys { get; set; }
public User ToUser() public User ToUser()
@ -27,6 +28,11 @@ namespace Bit.Core.Models.Api
MasterPasswordHint = MasterPasswordHint MasterPasswordHint = MasterPasswordHint
}; };
if(Key != null)
{
user.Key = Key;
}
if(Keys != null) if(Keys != null)
{ {
Keys.ToUser(user); Keys.ToUser(user);

View File

@ -3,13 +3,18 @@ using System.ComponentModel.DataAnnotations;
namespace Bit.Core.Models.Api namespace Bit.Core.Models.Api
{ {
public class DataReloadRequestModel public class UpdateKeyRequestModel
{ {
[Required]
[StringLength(300)]
public string MasterPasswordHash { get; set; }
[Required] [Required]
public IEnumerable<LoginWithIdRequestModel> Ciphers { get; set; } public IEnumerable<LoginWithIdRequestModel> Ciphers { get; set; }
[Required] [Required]
public IEnumerable<FolderWithIdRequestModel> Folders { get; set; } public IEnumerable<FolderWithIdRequestModel> Folders { get; set; }
[Required] [Required]
public string PrivateKey { get; set; } public string PrivateKey { get; set; }
[Required]
public string Key { get; set; }
} }
} }

View File

@ -13,10 +13,12 @@ namespace Bit.Core.Models.Api
throw new ArgumentNullException(nameof(user)); throw new ArgumentNullException(nameof(user));
} }
Key = user.Key;
PublicKey = user.PublicKey; PublicKey = user.PublicKey;
PrivateKey = user.PrivateKey; PrivateKey = user.PrivateKey;
} }
public string Key { get; set; }
public string PublicKey { get; set; } public string PublicKey { get; set; }
public string PrivateKey { get; set; } public string PrivateKey { get; set; }
} }

View File

@ -21,6 +21,7 @@ namespace Bit.Core.Models.Table
public string EquivalentDomains { get; set; } public string EquivalentDomains { get; set; }
public string ExcludedGlobalEquivalentDomains { get; set; } public string ExcludedGlobalEquivalentDomains { get; set; }
public DateTime AccountRevisionDate { get; internal set; } = DateTime.UtcNow; public DateTime AccountRevisionDate { get; internal set; } = DateTime.UtcNow;
public string Key { get; set; }
public string PublicKey { get; set; } public string PublicKey { get; set; }
public string PrivateKey { get; set; } public string PrivateKey { get; set; }
public DateTime CreationDate { get; internal set; } = DateTime.UtcNow; public DateTime CreationDate { get; internal set; } = DateTime.UtcNow;

View File

@ -19,7 +19,7 @@ namespace Bit.Core.Repositories
Task UpsertAsync(CipherDetails cipher); Task UpsertAsync(CipherDetails cipher);
Task ReplaceAsync(Cipher obj, IEnumerable<Guid> collectionIds); Task ReplaceAsync(Cipher obj, IEnumerable<Guid> collectionIds);
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, IEnumerable<Folder> folders); Task UpdateUserKeysAndCiphersAsync(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

@ -176,7 +176,7 @@ namespace Bit.Core.Repositories.SqlServer
} }
} }
public Task UpdateUserEmailPasswordAndCiphersAsync(User user, IEnumerable<Cipher> ciphers, IEnumerable<Folder> folders) public Task UpdateUserKeysAndCiphersAsync(User user, IEnumerable<Cipher> ciphers, IEnumerable<Folder> folders)
{ {
using(var connection = new SqlConnection(ConnectionString)) using(var connection = new SqlConnection(ConnectionString))
{ {
@ -188,14 +188,13 @@ namespace Bit.Core.Repositories.SqlServer
{ {
// 1. Update user. // 1. Update user.
using(var cmd = new SqlCommand("[dbo].[User_UpdateEmailPassword]", connection, transaction)) using(var cmd = new SqlCommand("[dbo].[User_UpdateKeys]", connection, transaction))
{ {
cmd.CommandType = CommandType.StoredProcedure; cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Id", SqlDbType.UniqueIdentifier).Value = user.Id; cmd.Parameters.Add("@Id", SqlDbType.UniqueIdentifier).Value = user.Id;
cmd.Parameters.Add("@Email", SqlDbType.NVarChar).Value = user.Email;
cmd.Parameters.Add("@EmailVerified", SqlDbType.NVarChar).Value = user.EmailVerified;
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("@Key", SqlDbType.VarChar).Value = user.Key;
if(string.IsNullOrWhiteSpace(user.PrivateKey)) if(string.IsNullOrWhiteSpace(user.PrivateKey))
{ {
cmd.Parameters.Add("@PrivateKey", SqlDbType.VarChar).Value = DBNull.Value; cmd.Parameters.Add("@PrivateKey", SqlDbType.VarChar).Value = DBNull.Value;
@ -204,6 +203,7 @@ namespace Bit.Core.Repositories.SqlServer
{ {
cmd.Parameters.Add("@PrivateKey", SqlDbType.VarChar).Value = user.PrivateKey; 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();
} }

View File

@ -18,14 +18,15 @@ 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, Task<IdentityResult> ChangeEmailAsync(User user, string masterPassword, string newEmail, string newMasterPassword,
string token, IEnumerable<Cipher> ciphers, IEnumerable<Folder> folders, string privateKey); string token, string key);
Task<IdentityResult> ChangePasswordAsync(User user, string currentMasterPasswordHash, string newMasterPasswordHash, Task<IdentityResult> ChangePasswordAsync(User user, string masterPassword, string newMasterPassword, string key);
IEnumerable<Cipher> ciphers, IEnumerable<Folder> folders, string privateKey); Task<IdentityResult> UpdateKeyAsync(User user, string masterPassword, string key, string privateKey,
IEnumerable<Cipher> ciphers, IEnumerable<Folder> folders);
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);
Task<string> GenerateUserTokenAsync(User user, string tokenProvider, string purpose); Task<string> GenerateUserTokenAsync(User user, string tokenProvider, string purpose);
Task<IdentityResult> DeleteAsync(User user); Task<IdentityResult> DeleteAsync(User user);
} }
} }

View File

@ -195,7 +195,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, IEnumerable<Folder> folders, string privateKey) string newMasterPassword, string token, string key)
{ {
var verifyPasswordResult = _passwordHasher.VerifyHashedPassword(user, user.MasterPassword, masterPassword); var verifyPasswordResult = _passwordHasher.VerifyHashedPassword(user, user.MasterPassword, masterPassword);
if(verifyPasswordResult == PasswordVerificationResult.Failed) if(verifyPasswordResult == PasswordVerificationResult.Failed)
@ -221,19 +221,11 @@ namespace Bit.Core.Services
return result; return result;
} }
user.Key = key;
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; await _userRepository.ReplaceAsync(user);
if(ciphers.Any() || folders.Any())
{
await _cipherRepository.UpdateUserEmailPasswordAndCiphersAsync(user, ciphers, folders);
}
else
{
await _userRepository.ReplaceAsync(user);
}
return IdentityResult.Success; return IdentityResult.Success;
} }
@ -244,7 +236,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<Folder> folders, string privateKey) string key)
{ {
if(user == null) if(user == null)
{ {
@ -260,10 +252,33 @@ namespace Bit.Core.Services
} }
user.RevisionDate = user.AccountRevisionDate = DateTime.UtcNow; user.RevisionDate = user.AccountRevisionDate = DateTime.UtcNow;
user.Key = key;
await _userRepository.ReplaceAsync(user);
return IdentityResult.Success;
}
Logger.LogWarning("Change password failed for user {userId}.", user.Id);
return IdentityResult.Failed(_identityErrorDescriber.PasswordMismatch());
}
public async Task<IdentityResult> UpdateKeyAsync(User user, string masterPassword, string key, string privateKey,
IEnumerable<Cipher> ciphers, IEnumerable<Folder> folders)
{
if(user == null)
{
throw new ArgumentNullException(nameof(user));
}
if(await base.CheckPasswordAsync(user, masterPassword))
{
user.RevisionDate = user.AccountRevisionDate = DateTime.UtcNow;
user.SecurityStamp = Guid.NewGuid().ToString();
user.Key = key;
user.PrivateKey = privateKey; user.PrivateKey = privateKey;
if(ciphers.Any() || folders.Any()) if(ciphers.Any() || folders.Any())
{ {
await _cipherRepository.UpdateUserEmailPasswordAndCiphersAsync(user, ciphers, folders); await _cipherRepository.UpdateUserKeysAndCiphersAsync(user, ciphers, folders);
} }
else else
{ {
@ -273,7 +288,7 @@ namespace Bit.Core.Services
return IdentityResult.Success; return IdentityResult.Success;
} }
Logger.LogWarning("Change password failed for user {userId}.", user.Id); Logger.LogWarning("Update key for user {userId}.", user.Id);
return IdentityResult.Failed(_identityErrorDescriber.PasswordMismatch()); return IdentityResult.Failed(_identityErrorDescriber.PasswordMismatch());
} }

View File

@ -148,7 +148,7 @@
<Build Include="dbo\Stored Procedures\User_ReadByEmail.sql" /> <Build Include="dbo\Stored Procedures\User_ReadByEmail.sql" />
<Build Include="dbo\Stored Procedures\User_ReadById.sql" /> <Build Include="dbo\Stored Procedures\User_ReadById.sql" />
<Build Include="dbo\Stored Procedures\User_Update.sql" /> <Build Include="dbo\Stored Procedures\User_Update.sql" />
<Build Include="dbo\Stored Procedures\User_UpdateEmailPassword.sql" /> <Build Include="dbo\Stored Procedures\User_UpdateKeys.sql" />
<Build Include="dbo\Stored Procedures\Device_ClearPushTokenByIdentifier.sql" /> <Build Include="dbo\Stored Procedures\Device_ClearPushTokenByIdentifier.sql" />
<Build Include="dbo\Stored Procedures\Collection_CreateWithGroups.sql" /> <Build Include="dbo\Stored Procedures\Collection_CreateWithGroups.sql" />
<Build Include="dbo\Stored Procedures\Collection_ReadWithGroupsById.sql" /> <Build Include="dbo\Stored Procedures\Collection_ReadWithGroupsById.sql" />

View File

@ -14,6 +14,7 @@
@EquivalentDomains NVARCHAR(MAX), @EquivalentDomains NVARCHAR(MAX),
@ExcludedGlobalEquivalentDomains NVARCHAR(MAX), @ExcludedGlobalEquivalentDomains NVARCHAR(MAX),
@AccountRevisionDate DATETIME2(7), @AccountRevisionDate DATETIME2(7),
@Key NVARCHAR(MAX),
@PublicKey NVARCHAR(MAX), @PublicKey NVARCHAR(MAX),
@PrivateKey NVARCHAR(MAX), @PrivateKey NVARCHAR(MAX),
@CreationDate DATETIME2(7), @CreationDate DATETIME2(7),
@ -39,6 +40,7 @@ BEGIN
[EquivalentDomains], [EquivalentDomains],
[ExcludedGlobalEquivalentDomains], [ExcludedGlobalEquivalentDomains],
[AccountRevisionDate], [AccountRevisionDate],
[Key],
[PublicKey], [PublicKey],
[PrivateKey], [PrivateKey],
[CreationDate], [CreationDate],
@ -61,6 +63,7 @@ BEGIN
@EquivalentDomains, @EquivalentDomains,
@ExcludedGlobalEquivalentDomains, @ExcludedGlobalEquivalentDomains,
@AccountRevisionDate, @AccountRevisionDate,
@Key,
@PublicKey, @PublicKey,
@PrivateKey, @PrivateKey,
@CreationDate, @CreationDate,

View File

@ -14,6 +14,7 @@
@EquivalentDomains NVARCHAR(MAX), @EquivalentDomains NVARCHAR(MAX),
@ExcludedGlobalEquivalentDomains NVARCHAR(MAX), @ExcludedGlobalEquivalentDomains NVARCHAR(MAX),
@AccountRevisionDate DATETIME2(7), @AccountRevisionDate DATETIME2(7),
@Key NVARCHAR(MAX),
@PublicKey NVARCHAR(MAX), @PublicKey NVARCHAR(MAX),
@PrivateKey NVARCHAR(MAX), @PrivateKey NVARCHAR(MAX),
@CreationDate DATETIME2(7), @CreationDate DATETIME2(7),
@ -39,6 +40,7 @@ BEGIN
[EquivalentDomains] = @EquivalentDomains, [EquivalentDomains] = @EquivalentDomains,
[ExcludedGlobalEquivalentDomains] = @ExcludedGlobalEquivalentDomains, [ExcludedGlobalEquivalentDomains] = @ExcludedGlobalEquivalentDomains,
[AccountRevisionDate] = @AccountRevisionDate, [AccountRevisionDate] = @AccountRevisionDate,
[Key] = @Key,
[PublicKey] = @PublicKey, [PublicKey] = @PublicKey,
[PrivateKey] = @PrivateKey, [PrivateKey] = @PrivateKey,
[CreationDate] = @CreationDate, [CreationDate] = @CreationDate,

View File

@ -1,9 +1,7 @@
CREATE PROCEDURE [dbo].[User_UpdateEmailPassword] CREATE PROCEDURE [dbo].[User_UpdateKeys]
@Id UNIQUEIDENTIFIER, @Id UNIQUEIDENTIFIER,
@Email NVARCHAR(50),
@EmailVerified BIT,
@MasterPassword NVARCHAR(300),
@SecurityStamp NVARCHAR(50), @SecurityStamp NVARCHAR(50),
@Key NVARCHAR(MAX),
@PrivateKey VARCHAR(MAX), @PrivateKey VARCHAR(MAX),
@RevisionDate DATETIME2(7) @RevisionDate DATETIME2(7)
AS AS
@ -13,10 +11,8 @@ BEGIN
UPDATE UPDATE
[dbo].[User] [dbo].[User]
SET SET
[Email] = @Email,
[EmailVerified] = @EmailVerified,
[MasterPassword] = @MasterPassword,
[SecurityStamp] = @SecurityStamp, [SecurityStamp] = @SecurityStamp,
[Key] = @Key,
[PrivateKey] = @PrivateKey, [PrivateKey] = @PrivateKey,
[RevisionDate] = @RevisionDate, [RevisionDate] = @RevisionDate,
[AccountRevisionDate] = @RevisionDate [AccountRevisionDate] = @RevisionDate

View File

@ -14,6 +14,7 @@
[EquivalentDomains] NVARCHAR (MAX) NULL, [EquivalentDomains] NVARCHAR (MAX) NULL,
[ExcludedGlobalEquivalentDomains] NVARCHAR (MAX) NULL, [ExcludedGlobalEquivalentDomains] NVARCHAR (MAX) NULL,
[AccountRevisionDate] DATETIME2 (7) NOT NULL, [AccountRevisionDate] DATETIME2 (7) NOT NULL,
[Key] VARCHAR (MAX) NULL,
[PublicKey] VARCHAR (MAX) NULL, [PublicKey] VARCHAR (MAX) NULL,
[PrivateKey] VARCHAR (MAX) NULL, [PrivateKey] VARCHAR (MAX) NULL,
[CreationDate] DATETIME2 (7) NOT NULL, [CreationDate] DATETIME2 (7) NOT NULL,