Skip to content

Commit

Permalink
Adde Password Builder
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexMi-Ha committed Apr 10, 2024
1 parent 4f12a77 commit 17946e4
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 4 deletions.
4 changes: 4 additions & 0 deletions PWManager.Application/Exceptions/MessageStrings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,8 @@ public static class MessageStrings {
public const string PATH_ERROR = "An unknown error occured! Could not determine execution path!";
public const string DIRECTORY_ERROR = "An unknown error occured! Execution path is not a directory!";
// ----------------------------------------

// PasswordBuilder
public const string MIN_LENGTH_TO_SMALL = "MinLength cannot be smaller than 0";
public const string MAX_LENGTH_TO_SMALL = "MaxLength cannot be smaller than MinLength";
}
79 changes: 79 additions & 0 deletions PWManager.Application/Services/PasswordBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using PWManager.Application.Exceptions;
using PWManager.Domain.ValueObjects;

namespace PWManager.Application.Services;

public class PasswordBuilder {

private PasswordGeneratorService _generatorService;

private bool _includeLowerCase = false;
private bool _includeUpperCase = false;
private bool _includeSpecial = false;
private bool _includeSpaces = false;
private bool _includeNumeric = false;
private bool _includeBrackets = false;
private int _minLength = 5;
private int _maxLength = 8;

private PasswordBuilder() : this(new PasswordGeneratorService(null)) {}

private PasswordBuilder(PasswordGeneratorService generatorService) {
_generatorService = generatorService;
}

public PasswordBuilder SetMinLength(int minLength) {
if (minLength <= 0) {
throw new PasswordGenerationException(MessageStrings.MIN_LENGTH_TO_SMALL);
}
_minLength = minLength;
return this;
}

public PasswordBuilder SetMaxLength(int maxLength) {
if (maxLength < _minLength) {
throw new PasswordGenerationException(MessageStrings.MAX_LENGTH_TO_SMALL);
}
_maxLength = maxLength;
return this;
}

public PasswordBuilder IncludeUppercase() {
_includeUpperCase = true;
return this;
}
public PasswordBuilder IncludeLowercase() {
_includeLowerCase = true;
return this;
}
public PasswordBuilder IncludeSpecialChars() {
_includeSpecial = true;
return this;
}
public PasswordBuilder IncludeSpaces() {
_includeSpaces = true;
return this;
}
public PasswordBuilder IncludeNumeric() {
_includeNumeric = true;
return this;
}
public PasswordBuilder IncludeBrackets() {
_includeBrackets = true;
return this;
}

public string BuildPassword() {
var criteria = BuildCriteria();
return _generatorService.GeneratePasswordWith(criteria);
}

internal PasswordGeneratorCriteria BuildCriteria() {
return new PasswordGeneratorCriteria(_includeLowerCase, _includeUpperCase, _includeNumeric,
_includeSpecial, _includeBrackets, _includeSpaces, _minLength, _maxLength);
}

public static PasswordBuilder Create() {
return new PasswordBuilder();
}
}
6 changes: 3 additions & 3 deletions PWManager.Application/Services/PasswordGeneratorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace PWManager.Application.Services;

public class PasswordGeneratorService : IPasswordGeneratorService {

private readonly PasswordGeneratorCriteria _userSettings;
private readonly ISettingsRepository _settingsRepo;

private const string Lowercase = "abcdefghijklmnopqrstuvwxyz";
private const string Uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Expand All @@ -23,7 +23,7 @@ public class PasswordGeneratorService : IPasswordGeneratorService {
}

public PasswordGeneratorService(ISettingsRepository settingsRepository, Random rng) {
_userSettings = settingsRepository.GetSettings().PwGenCriteria;
_settingsRepo = settingsRepository;
_rng = rng;
}

Expand All @@ -44,7 +44,7 @@ public string GeneratePasswordWith(PasswordGeneratorCriteria criteria) {
}

public string GeneratePassword() {
return GeneratePasswordWith(_userSettings);
return GeneratePasswordWith(_settingsRepo.GetSettings().PwGenCriteria);
}

private static char[] BuildPossibleChars(PasswordGeneratorCriteria criteria) {
Expand Down
2 changes: 1 addition & 1 deletion PWManager.Domain/ValueObjects/PasswordGeneratorCriteria.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class PasswordGeneratorCriteria : ValueObject {
public bool IncludeBrackets { get; }
public int MinLength { get; }
public int MaxLength { get; }

public PasswordGeneratorCriteria(bool includeLowerCase, bool includeUpperCase, bool includeNumeric, bool includeSpecial, bool includeBrackets, bool includeSpaces, int minLength, int maxLength) {
if (minLength <= 0) {
throw new ArgumentException("MinLength cannot be less than or equal to 0");
Expand Down

0 comments on commit 17946e4

Please sign in to comment.