diff --git a/src/HaveIBeenPwned.Client.Abstractions/Models/PwnedPassword.cs b/src/HaveIBeenPwned.Client.Abstractions/Models/PwnedPassword.cs index 95b9582..6eafbec 100644 --- a/src/HaveIBeenPwned.Client.Abstractions/Models/PwnedPassword.cs +++ b/src/HaveIBeenPwned.Client.Abstractions/Models/PwnedPassword.cs @@ -3,13 +3,36 @@ namespace HaveIBeenPwned.Client.Abstractions; -/// -public record PwnedPassword( - string? PlainTextPassword, - bool? IsPwned = default, - long PwnedCount = -1, - string? HashedPassword = default) +/// +/// 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. +/// +public record class PwnedPassword { + /// + /// The plain text password used for the lookup. + /// + public string? PlainTextPassword { get; set; } + + /// + /// Whether or not the current + /// instance is considered to be "pwned". + /// + public bool? IsPwned { get; set; } + + /// + /// When is true, this will be a non-zero number. + /// It represents the number of times the given + /// has been found in the "have i been pwned" passwords database. + /// + public long PwnedCount { get; set; } + + /// + /// The hashed representation of the given . + /// + public string? HashedPassword { get; set; } + internal bool IsInvalid() => PlainTextPassword is null or { Length: 0 }; } diff --git a/src/HaveIBeenPwned.Client/DefaultPwnedClient.Passwords.cs b/src/HaveIBeenPwned.Client/DefaultPwnedClient.Passwords.cs index 03b3a47..20c6c02 100644 --- a/src/HaveIBeenPwned.Client/DefaultPwnedClient.Passwords.cs +++ b/src/HaveIBeenPwned.Client/DefaultPwnedClient.Passwords.cs @@ -14,7 +14,11 @@ async Task 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; diff --git a/tests/HaveIBeenPwned.Client.AbstractionsTests/Models/PwnedPasswordTests.cs b/tests/HaveIBeenPwned.Client.AbstractionsTests/Models/PwnedPasswordTests.cs index 2a83e05..48f6cf5 100644 --- a/tests/HaveIBeenPwned.Client.AbstractionsTests/Models/PwnedPasswordTests.cs +++ b/tests/HaveIBeenPwned.Client.AbstractionsTests/Models/PwnedPasswordTests.cs @@ -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()); } } diff --git a/tests/HaveIBeenPwned.ClientTests/DefaultPwnedClientTests.cs b/tests/HaveIBeenPwned.ClientTests/DefaultPwnedClientTests.cs index 0298d8b..e6afc3d 100644 --- a/tests/HaveIBeenPwned.ClientTests/DefaultPwnedClientTests.cs +++ b/tests/HaveIBeenPwned.ClientTests/DefaultPwnedClientTests.cs @@ -98,8 +98,9 @@ public static IEnumerable ParsePasswordRangeResponseTextInput { yield return new object[] { - new PwnedPassword("f@k3PA55w0d!") + new PwnedPassword() { + PlainTextPassword = "f@k3PA55w0d!", HashedPassword = "a841ab792c6b438c71a97e05e6197ceba54f8e96", PwnedCount = 77, IsPwned = true @@ -115,8 +116,9 @@ public static IEnumerable ParsePasswordRangeResponseTextInput yield return new object[] { - new PwnedPassword("Does this even work?") + new PwnedPassword() { + PlainTextPassword = "Does this even work?", HashedPassword = "5ec3e89686893f2ba76bdf1bcf1070568823e400" }, "Does this even work?", @@ -134,8 +136,9 @@ public static IEnumerable ParsePasswordRangeResponseTextInput yield return new object[] { - new PwnedPassword("f@k3PA55w0d!") + new PwnedPassword() { + PlainTextPassword = "f@k3PA55w0d!", HashedPassword = "a841ab792c6b438c71a97e05e6197ceba54f8e96", PwnedCount = 4001, IsPwned = true @@ -152,8 +155,9 @@ public static IEnumerable ParsePasswordRangeResponseTextInput yield return new object[] { - new PwnedPassword("f@k3PA55w0d!") + new PwnedPassword() { + PlainTextPassword ="f@k3PA55w0d!", HashedPassword = "a841ab792c6b438c71a97e05e6197ceba54f8e96" }, "f@k3PA55w0d!", @@ -169,7 +173,11 @@ public static IEnumerable 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(