Skip to content

Commit

Permalink
Merge pull request #13 from nodew/refactor-policy-management
Browse files Browse the repository at this point in the history
Refactor policy management
  • Loading branch information
nodew authored Feb 5, 2024
2 parents d5d1d86 + 74f571a commit 53caed8
Show file tree
Hide file tree
Showing 33 changed files with 553 additions and 233 deletions.
2 changes: 1 addition & 1 deletion .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"isRoot": true,
"tools": {
"dotnet-format": {
"version": "5.1.250801",
"version": "8.0.453106",
"commands": [
"dotnet-format"
]
Expand Down
2 changes: 1 addition & 1 deletion .husky/task-runner.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "dotnet-format",
"command": "dotnet",
"group": "pre-commit",
"args": ["format", "--include" , "${staged}"],
"args": ["dotnet-format", "--include" , "${staged}"],
"include": ["**/*.cs"]
}
]
Expand Down
8 changes: 8 additions & 0 deletions NuGet.Config
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
<add key="dotnet8" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet8/nuget/v3/index.json" protocolVersion="3" />
</packageSources>
</configuration>
2 changes: 1 addition & 1 deletion src/PwSafeClient.CLI/Commands/CommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class CommandHandler : ICommandHandler
{
public int Invoke(InvocationContext context)
{
int result = 0;
var result = 0;
Task.Run(async () => result = await InvokeAsync(context)).Wait();
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public InitConfigCommandHandler(IConfigManager configManager, IConsoleService co

public override async Task<int> InvokeAsync(InvocationContext context)
{
string filepath = configManager.GetConfigPath();
var filepath = configManager.GetConfigPath();

if (configManager.ConfigExists())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public override async Task<int> InvokeAsync(InvocationContext context)

try
{
Document document = new Document(password);
var document = new Document(password);
document.Save(File.FullName);

await configManager.AddDatabaseAsync(Alias, File.FullName, Default);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ public ShowDbCommandHandler(IDocumentHelper documentHelper, IConsoleService cons

public override async Task<int> InvokeAsync(InvocationContext context)
{
Document? doc = await documentHelper.TryLoadDocumentAsync(Alias, File, true);
var doc = await documentHelper.TryLoadDocumentAsync(Alias, File, true);

if (doc == null)
{
return 1;
}

string format = "{0, -30}{1}";
var format = "{0, -30}{1}";
Console.WriteLine(format, "Database UUID:", doc.Uuid);
Console.WriteLine(format, "Name:", doc.Name ?? "-");
Console.WriteLine(format, "Description:", doc.Description ?? "-");
Expand Down
2 changes: 1 addition & 1 deletion src/PwSafeClient.CLI/Commands/GetPasswordCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public GetPasswordCommandHandler(IConsoleService consoleService, IDocumentHelper

public override async Task<int> InvokeAsync(InvocationContext context)
{
Document? document = await documentHelper.TryLoadDocumentAsync(Alias, File, true);
var document = await documentHelper.TryLoadDocumentAsync(Alias, File, true);
if (document == null)
{
return 1;
Expand Down
14 changes: 7 additions & 7 deletions src/PwSafeClient.CLI/Commands/ListEntriesCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ public ListEntriesCommandHandler(IDocumentHelper documentHelper, IConsoleService

public override async Task<int> InvokeAsync(InvocationContext context)
{
Document? document = await documentHelper.TryLoadDocumentAsync(Alias, File, true);
var document = await documentHelper.TryLoadDocumentAsync(Alias, File, true);

if (document == null)
{
return 1;
}

List<Entry> entries = document.Entries.ToList();
var entries = document.Entries.ToList();

if (!string.IsNullOrEmpty(Filter))
{
Expand Down Expand Up @@ -102,7 +102,7 @@ private static void PrintListView(List<Entry> entries)
Console.OutputEncoding = System.Text.Encoding.UTF8;
Console.WriteLine(fmt, "Uuid", "Title", "Username", "Group");

foreach (Entry entry in oderedEntries)
foreach (var entry in oderedEntries)
{
Console.WriteLine(fmt, entry.Uuid, entry.Title, entry.UserName, entry.Group);
}
Expand All @@ -111,13 +111,13 @@ private static void PrintListView(List<Entry> entries)
private static void PrintTreeView(List<Entry> entries)
{
Console.OutputEncoding = System.Text.Encoding.UTF8;
Group root = new GroupBuilder(entries).Build();
var root = new GroupBuilder(entries).Build();
PrintTreeView(entries, root, 0);
}

private static void PrintTreeView(List<Entry> entries, Group group, int depth)
{
GroupPath groupPath = group.GetGroupPath();
var groupPath = group.GetGroupPath();

IEnumerable<Entry> subEntries = entries
.Where(entry => entry.Group.Equals(groupPath))
Expand All @@ -128,13 +128,13 @@ private static void PrintTreeView(List<Entry> entries, Group group, int depth)
Console.WriteLine("{0}|- {1}", new string(' ', (depth - 1) * 2), group.Name);
}

foreach (Entry entry in subEntries)
foreach (var entry in subEntries)
{
var fmt = "{0}|- {1}({2}) [{3}]";
Console.WriteLine(fmt, new string(' ', depth * 2), entry.Title, entry.UserName, entry.Uuid);
}

foreach (Group child in group.Children)
foreach (var child in group.Children)
{
PrintTreeView(entries, child, depth + 1);
}
Expand Down
8 changes: 4 additions & 4 deletions src/PwSafeClient.CLI/Commands/NewEntryCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public NewEntryCommandHandler(IConsoleService consoleService, IDocumentHelper do

public override async Task<int> InvokeAsync(InvocationContext context)
{
Document? document = await documentHelper.TryLoadDocumentAsync(Alias, File, false);
var document = await documentHelper.TryLoadDocumentAsync(Alias, File, false);
if (document == null)
{
return 1;
Expand All @@ -126,7 +126,7 @@ public override async Task<int> InvokeAsync(InvocationContext context)
if (!string.IsNullOrWhiteSpace(Group))
{
groupSegments = Group.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < groupSegments.Length; i++)
for (var i = 0; i < groupSegments.Length; i++)
{
groupSegments[i] = groupSegments[i].Trim();
}
Expand All @@ -136,7 +136,7 @@ public override async Task<int> InvokeAsync(InvocationContext context)
if (document.Entries.Any(e => e.Title == Title && e.Group.Equals(targetGroupPath)))
{
consoleService.LogError($"The entry {Title} already exists under the group {Group}.");
bool shouldContinue = consoleService.DoConfirm("Do you want to continue?");
var shouldContinue = consoleService.DoConfirm("Do you want to continue?");

if (!shouldContinue)
{
Expand All @@ -156,7 +156,7 @@ public override async Task<int> InvokeAsync(InvocationContext context)
Notes = Notes
};

string newPassword = string.Empty;
var newPassword = string.Empty;

if (context.ParseResult.HasOption(PasswordOption))
{
Expand Down
99 changes: 65 additions & 34 deletions src/PwSafeClient.CLI/Commands/PolicyManagement/AddPolicyCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,15 @@ public override async Task<int> InvokeAsync(InvocationContext context)
return 1;
}

Document? document = await documentHelper.TryLoadDocumentAsync(Alias, File, false);
if (HexOnly && (Digits > 0 || Uppercase > 0 || Lowercase > 0 || Symbols > 0))
{
if (!consoleService.DoConfirm("When --hex-ony is set, all other options will be ignored, continue?"))
{
return 0;
}
}

var document = await documentHelper.TryLoadDocumentAsync(Alias, File, false);
if (document == null)
{
return 1;
Expand All @@ -101,61 +109,78 @@ public override async Task<int> InvokeAsync(InvocationContext context)
var policy = new NamedPasswordPolicy(Name, Length);

PasswordPolicyStyle style = 0;
var minimumDigitCount = 0;
var minimumUppercaseCount = 0;
var minimumLowercaseCount = 0;
var minimumSymbolCount = 0;

if (HexOnly)
{
style |= PasswordPolicyStyle.UseHexDigits;
}

if (EasyVision)
else
{
style |= PasswordPolicyStyle.UseEasyVision;
}
if (Pronounceable)
{
style |= PasswordPolicyStyle.MakePronounceable;
}

if (Pronounceable)
{
style |= PasswordPolicyStyle.MakePronounceable;
}
if (EasyVision)
{
style |= PasswordPolicyStyle.UseEasyVision;
}

if (Digits >= 0)
{
style |= PasswordPolicyStyle.UseDigits;
}
if (Digits >= 0)
{
style |= PasswordPolicyStyle.UseDigits;
minimumDigitCount = Pronounceable ? 0 : Digits;
}

if (Uppercase >= 0)
{
style |= PasswordPolicyStyle.UseUppercase;
}
if (Uppercase >= 0)
{
style |= PasswordPolicyStyle.UseUppercase;
minimumUppercaseCount = Pronounceable ? 0 : Uppercase;
}

if (Lowercase >= 0)
{
style |= PasswordPolicyStyle.UseLowercase;
}
if (Lowercase >= 0)
{
style |= PasswordPolicyStyle.UseLowercase;
minimumLowercaseCount = Pronounceable ? 0 : Lowercase;
}

if (Symbols >= 0)
{
style |= PasswordPolicyStyle.UseSymbols;
if (Symbols >= 0)
{
style |= PasswordPolicyStyle.UseSymbols;
minimumSymbolCount = Pronounceable ? 0 : Symbols;
}
}

policy.Style = style;
policy.MinimumDigitCount = FilterNegativeValue(Digits);
policy.MinimumUppercaseCount = FilterNegativeValue(Uppercase);
policy.MinimumLowercaseCount = FilterNegativeValue(Lowercase);
policy.MinimumSymbolCount = FilterNegativeValue(Symbols);
policy.MinimumDigitCount = minimumDigitCount;
policy.MinimumUppercaseCount = minimumUppercaseCount;
policy.MinimumLowercaseCount = minimumLowercaseCount;
policy.MinimumSymbolCount = minimumSymbolCount;

if (!string.IsNullOrWhiteSpace(SymbolChars))
if (Pronounceable)
{
policy.SetSpecialSymbolSet(SymbolChars.ToArray());
policy.SetSpecialSymbolSet(PwCharPool.PronounceableSymbolChars);
}
else
{
if (policy.Style.HasFlag(PasswordPolicyStyle.UseEasyVision))
if (!string.IsNullOrWhiteSpace(SymbolChars))
{
policy.SetSpecialSymbolSet(PwCharPool.EasyVisionSymbolChars);
policy.SetSpecialSymbolSet(SymbolChars.ToArray());
}
else
{
policy.SetSpecialSymbolSet(PwCharPool.StdSymbolChars);
if (EasyVision)
{
policy.SetSpecialSymbolSet(PwCharPool.EasyVisionSymbolChars);
}
else
{
policy.SetSpecialSymbolSet(PwCharPool.StdSymbolChars);
}
}
}

Expand Down Expand Up @@ -186,14 +211,20 @@ private bool HasValidatedInput()
return false;
}

int constraintsLength = FilterNegativeValue(Digits) + FilterNegativeValue(Uppercase) + FilterNegativeValue(Lowercase) + FilterNegativeValue(Symbols);
var constraintsLength = FilterNegativeValue(Digits) + FilterNegativeValue(Uppercase) + FilterNegativeValue(Lowercase) + FilterNegativeValue(Symbols);

if (constraintsLength > Length)
{
consoleService.LogError("The password length is less than the sum of 'at least' constraints.");
return false;
}

if (EasyVision && Pronounceable)
{
Console.WriteLine("The options '--easy-vision' and '--pronounceable' cannot be used together.");
return false;
}

if (!string.IsNullOrWhiteSpace(SymbolChars))
{
if (PwCharPool.HasDuplicatedCharacters(SymbolChars))
Expand Down
Loading

0 comments on commit 53caed8

Please sign in to comment.