-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathSetInitialMasterPasswordData.cs
More file actions
53 lines (47 loc) · 2.01 KB
/
SetInitialMasterPasswordData.cs
File metadata and controls
53 lines (47 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using Bit.Core.Entities;
namespace Bit.Core.KeyManagement.Models.Data;
/// <summary>
/// Data for setting an initial master password on a user account that has no existing master password.
/// See <see cref="UpdateMasterPasswordData"/> for updating an existing master password.
/// </summary>
public class SetInitialMasterPasswordData
{
public required MasterPasswordAuthenticationData MasterPasswordAuthentication { get; init; }
public required MasterPasswordUnlockData MasterPasswordUnlock { get; init; }
public string? MasterPasswordHint { get; init; }
/// <summary>
/// Validates that the provided data is consistent with a user account that has no existing master password.
/// </summary>
/// <remarks>
/// Verifies that the user has no existing master password, user key, or master password salt,
/// and that the provided salts match the user's current salt (email fallback).
/// </remarks>
/// <exception cref="InvalidOperationException">
/// Thrown when the user already has a master password, user key, or master password salt set,
/// or when the provided salt does not match the user's current salt.
/// </exception>
public void ValidateForUser(User user)
{
try
{
if (user.MasterPassword != null)
{
throw new InvalidOperationException("User already has a master password.");
}
if (user.Key != null)
{
throw new InvalidOperationException("User already has a user key.");
}
if (user.MasterPasswordSalt != null)
{
throw new InvalidOperationException("User already has a master password salt.");
}
MasterPasswordAuthentication.ValidateSaltUnchangedForUser(user);
MasterPasswordUnlock.ValidateSaltUnchangedForUser(user);
}
catch
{
throw new InvalidOperationException("The provided master password data is not valid for this user.");
}
}
}