Skip to content

Commit

Permalink
JSON serialization woes from init. Seriously not a fun time. Let's …
Browse files Browse the repository at this point in the history
…use `set` instead. :(
  • Loading branch information
IEvangelist committed Mar 11, 2022
1 parent 510f253 commit 2707571
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 13 deletions.
35 changes: 29 additions & 6 deletions src/HaveIBeenPwned.Client.Abstractions/Models/PwnedPassword.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,36 @@

namespace HaveIBeenPwned.Client.Abstractions;

/// <summary></summary>
public record PwnedPassword(
string? PlainTextPassword,
bool? IsPwned = default,
long PwnedCount = -1,
string? HashedPassword = default)
/// <summary>
/// An object used to represent the plain-text password, and corresponding
/// hashed password. As well as whether the password is considered "pwned",
/// and if so, how many times.
/// </summary>
public record class PwnedPassword
{
/// <summary>
/// The plain text password used for the lookup.
/// </summary>
public string? PlainTextPassword { get; set; }

/// <summary>
/// Whether or not the current <see cref="PwnedPassword"/>
/// instance is considered to be "pwned".
/// </summary>
public bool? IsPwned { get; set; }

/// <summary>
/// When <see cref="IsPwned"/> is <c>true</c>, this will be a non-zero number.
/// It represents the number of times the given <see cref="PlainTextPassword"/>
/// has been found in the "have i been pwned" passwords database.
/// </summary>
public long PwnedCount { get; set; }

/// <summary>
/// The hashed representation of the given <see cref="PlainTextPassword"/>.
/// </summary>
public string? HashedPassword { get; set; }

internal bool IsInvalid() =>
PlainTextPassword is null or { Length: 0 };
}
6 changes: 5 additions & 1 deletion src/HaveIBeenPwned.Client/DefaultPwnedClient.Passwords.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ async Task<PwnedPassword> IPwnedPasswordsClient.GetPwnedPasswordAsync(string pla
"The plainTextPassword cannot be either null, or empty.", nameof(plainTextPassword));
}

var pwnedPassword = new PwnedPassword(plainTextPassword);
var pwnedPassword = new PwnedPassword()
{
PlainTextPassword = plainTextPassword
};

if (pwnedPassword.IsInvalid())
{
return pwnedPassword;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class PwnedPasswordTests
public void PwnedPassword_Returns_CorrectValidityState_WhenConstructed(
string value, bool expected)
{
PwnedPassword pwnedPassword = new(value);
PwnedPassword pwnedPassword = new() { PlainTextPassword = value };
Assert.Equal(expected, pwnedPassword.IsInvalid());
}
}
18 changes: 13 additions & 5 deletions tests/HaveIBeenPwned.ClientTests/DefaultPwnedClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ public static IEnumerable<object[]> ParsePasswordRangeResponseTextInput
{
yield return new object[]
{
new PwnedPassword("f@k3PA55w0d!")
new PwnedPassword()
{
PlainTextPassword = "f@k3PA55w0d!",
HashedPassword = "a841ab792c6b438c71a97e05e6197ceba54f8e96",
PwnedCount = 77,
IsPwned = true
Expand All @@ -115,8 +116,9 @@ public static IEnumerable<object[]> ParsePasswordRangeResponseTextInput

yield return new object[]
{
new PwnedPassword("Does this even work?")
new PwnedPassword()
{
PlainTextPassword = "Does this even work?",
HashedPassword = "5ec3e89686893f2ba76bdf1bcf1070568823e400"
},
"Does this even work?",
Expand All @@ -134,8 +136,9 @@ public static IEnumerable<object[]> ParsePasswordRangeResponseTextInput

yield return new object[]
{
new PwnedPassword("f@k3PA55w0d!")
new PwnedPassword()
{
PlainTextPassword = "f@k3PA55w0d!",
HashedPassword = "a841ab792c6b438c71a97e05e6197ceba54f8e96",
PwnedCount = 4001,
IsPwned = true
Expand All @@ -152,8 +155,9 @@ public static IEnumerable<object[]> ParsePasswordRangeResponseTextInput

yield return new object[]
{
new PwnedPassword("f@k3PA55w0d!")
new PwnedPassword()
{
PlainTextPassword ="f@k3PA55w0d!",
HashedPassword = "a841ab792c6b438c71a97e05e6197ceba54f8e96"
},
"f@k3PA55w0d!",
Expand All @@ -169,7 +173,11 @@ public static IEnumerable<object[]> ParsePasswordRangeResponseTextInput
public void ParsePasswordRangeResponseText_CorrectlyHandlesText(
PwnedPassword expected, string plainTextPassword, string passwordRangeResponseText)
{
PwnedPassword actual = new(plainTextPassword);
PwnedPassword actual = new()
{
PlainTextPassword = plainTextPassword
};

var passwordHash = plainTextPassword.ToSha1Hash();

var mutatedActual = DefaultPwnedClient.ParsePasswordRangeResponseText(
Expand Down

0 comments on commit 2707571

Please sign in to comment.