mirror of
https://github.com/bitwarden/mobile
synced 2025-12-23 03:33:59 +00:00
[PM-1402] Refactor PasswordGenerationService alongside PolicyService (#2443)
* PM-1402 Refactor pass generation service alongside policyservice * PM-1402 Refactor PasswordGenerationService and PolicyService to have a simpler code and more specific to each class * PM-1402 Fix format * PM-1402 Moved policy consts from PolicyService to Policy * PM-1402 fix crash due to lack of null checking * PM-1402 fix format * PM-1402 removed GetValueOrDefault() given that it was not needed and was changing the behavior
This commit is contained in:
committed by
GitHub
parent
f24b82f345
commit
1c8328f62d
@@ -2,29 +2,29 @@
|
||||
{
|
||||
public class PasswordGenerationOptions
|
||||
{
|
||||
public PasswordGenerationOptions() { }
|
||||
public const string TYPE_PASSWORD = "password";
|
||||
public const string TYPE_PASSPHRASE = "passphrase";
|
||||
|
||||
public PasswordGenerationOptions(bool defaultOptions)
|
||||
public static PasswordGenerationOptions CreateDefault => new PasswordGenerationOptions
|
||||
{
|
||||
if (defaultOptions)
|
||||
{
|
||||
Length = 14;
|
||||
AllowAmbiguousChar = true;
|
||||
Number = true;
|
||||
MinNumber = 1;
|
||||
Uppercase = true;
|
||||
MinUppercase = 0;
|
||||
Lowercase = true;
|
||||
MinLowercase = 0;
|
||||
Special = false;
|
||||
MinSpecial = 1;
|
||||
Type = "password";
|
||||
NumWords = 3;
|
||||
WordSeparator = "-";
|
||||
Capitalize = false;
|
||||
IncludeNumber = false;
|
||||
}
|
||||
}
|
||||
Length = 14,
|
||||
AllowAmbiguousChar = true,
|
||||
Number = true,
|
||||
MinNumber = 1,
|
||||
Uppercase = true,
|
||||
MinUppercase = 0,
|
||||
Lowercase = true,
|
||||
MinLowercase = 0,
|
||||
Special = false,
|
||||
MinSpecial = 1,
|
||||
Type = TYPE_PASSWORD,
|
||||
NumWords = 3,
|
||||
WordSeparator = "-",
|
||||
Capitalize = false,
|
||||
IncludeNumber = false
|
||||
};
|
||||
|
||||
public PasswordGenerationOptions() { }
|
||||
|
||||
public int? Length { get; set; }
|
||||
public bool? AllowAmbiguousChar { get; set; }
|
||||
@@ -42,6 +42,12 @@
|
||||
public bool? Capitalize { get; set; }
|
||||
public bool? IncludeNumber { get; set; }
|
||||
|
||||
public PasswordGenerationOptions WithLength(int? length)
|
||||
{
|
||||
Length = length;
|
||||
return this;
|
||||
}
|
||||
|
||||
public void Merge(PasswordGenerationOptions defaults)
|
||||
{
|
||||
Length = Length ?? defaults.Length;
|
||||
@@ -60,5 +66,75 @@
|
||||
Capitalize = Capitalize ?? defaults.Capitalize;
|
||||
IncludeNumber = IncludeNumber ?? defaults.IncludeNumber;
|
||||
}
|
||||
|
||||
public void EnforcePolicy(PasswordGeneratorPolicyOptions enforcedPolicyOptions)
|
||||
{
|
||||
if (enforcedPolicyOptions is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (Length < enforcedPolicyOptions.MinLength)
|
||||
{
|
||||
Length = enforcedPolicyOptions.MinLength;
|
||||
}
|
||||
|
||||
if (enforcedPolicyOptions.UseUppercase)
|
||||
{
|
||||
Uppercase = true;
|
||||
}
|
||||
|
||||
if (enforcedPolicyOptions.UseLowercase)
|
||||
{
|
||||
Lowercase = true;
|
||||
}
|
||||
|
||||
if (enforcedPolicyOptions.UseNumbers)
|
||||
{
|
||||
Number = true;
|
||||
}
|
||||
|
||||
if (MinNumber < enforcedPolicyOptions.NumberCount)
|
||||
{
|
||||
MinNumber = enforcedPolicyOptions.NumberCount;
|
||||
}
|
||||
|
||||
if (enforcedPolicyOptions.UseSpecial)
|
||||
{
|
||||
Special = true;
|
||||
}
|
||||
|
||||
if (MinSpecial < enforcedPolicyOptions.SpecialCount)
|
||||
{
|
||||
MinSpecial = enforcedPolicyOptions.SpecialCount;
|
||||
}
|
||||
|
||||
// Must normalize these fields because the receiving call expects all options to pass the current rules
|
||||
if (MinSpecial + MinNumber > Length)
|
||||
{
|
||||
MinSpecial = Length - MinNumber;
|
||||
}
|
||||
|
||||
if (NumWords < enforcedPolicyOptions.MinNumberOfWords)
|
||||
{
|
||||
NumWords = enforcedPolicyOptions.MinNumberOfWords;
|
||||
}
|
||||
|
||||
if (enforcedPolicyOptions.Capitalize)
|
||||
{
|
||||
Capitalize = true;
|
||||
}
|
||||
|
||||
if (enforcedPolicyOptions.IncludeNumber)
|
||||
{
|
||||
IncludeNumber = true;
|
||||
}
|
||||
|
||||
// Force default type if password/passphrase selected via policy
|
||||
if (enforcedPolicyOptions.DefaultType == TYPE_PASSWORD || enforcedPolicyOptions.DefaultType == TYPE_PASSPHRASE)
|
||||
{
|
||||
Type = enforcedPolicyOptions.DefaultType;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,11 @@ namespace Bit.Core.Models.Domain
|
||||
{
|
||||
public class Policy : Domain
|
||||
{
|
||||
public const string MINUTES_KEY = "minutes";
|
||||
public const string ACTION_KEY = "action";
|
||||
public const string ACTION_LOCK = "lock";
|
||||
public const string ACTION_LOGOUT = "logOut";
|
||||
|
||||
public Policy() { }
|
||||
|
||||
public Policy(PolicyData obj)
|
||||
@@ -22,5 +27,32 @@ namespace Bit.Core.Models.Domain
|
||||
public PolicyType Type { get; set; }
|
||||
public Dictionary<string, object> Data { get; set; }
|
||||
public bool Enabled { get; set; }
|
||||
|
||||
public int? GetInt(string key)
|
||||
{
|
||||
if (Data.TryGetValue(key, out var val) && val != null)
|
||||
{
|
||||
return (int)(long)val;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public bool? GetBool(string key)
|
||||
{
|
||||
if (Data.TryGetValue(key, out var val) && val != null)
|
||||
{
|
||||
return (bool)val;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public string GetString(string key)
|
||||
{
|
||||
if (Data.TryGetValue(key, out var val))
|
||||
{
|
||||
return (string)val;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user