/// <summary>
        /// Retrieves information as to whether <paramref name="password" /> has been pwned.
        /// </summary>
        /// <param name="password">Password to check.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <returns>The task object representing the asynchronous operation.</returns>
        /// <exception cref="T:System.ArgumentException"><paramref name="password" />Password is null or empty.</exception>
        /// <exception cref="T:ByteDev.PwnedPasswords.PwnedPasswordsClientException">Error occured while calling the pwned passwords API.</exception>
        public async Task <PwnedPasswordResponse> GetHasBeenPwnedAsync(string password, CancellationToken cancellationToken)
        {
            var hashedPassword = new HashedPassword(password);

            try
            {
                var response = await _httpClient.GetAsync(UriFactory.GetHashPasswordUri(hashedPassword), cancellationToken);

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    var body = await response.Content.ReadAsStringAsync();

                    var fiveCharResponse = new FiveCharOnlyResponse(body);

                    return(PwnedPasswordResponse.CreatePwned(fiveCharResponse.GetCount(hashedPassword)));
                }

                if (response.StatusCode == HttpStatusCode.NotFound)
                {
                    return(PwnedPasswordResponse.CreateNotPwned());
                }

                throw new PwnedPasswordsClientException(CreateUnhandledStatusCodeMessage(response));
            }
            catch (Exception ex)
            {
                throw new PwnedPasswordsClientException("Error occured while calling the pwned passwords API.", ex);
            }
        }
Ejemplo n.º 2
0
            public void WhenValidContent_ThenSetProperties()
            {
                var sut = new FiveCharOnlyResponse(ValidContent);

                Assert.That(sut.RawContent, Is.EqualTo(ValidContent));
                Assert.That(sut.RawContentLines.Length, Is.EqualTo(3));
            }
Ejemplo n.º 3
0
            public void WhenHashExists_AndResponseIsOneRecord_ThenReturnCount()
            {
                var sut = new FiveCharOnlyResponse("E5D64B0E216796E834F52D61FD0B70332FC:2");

                var result = sut.GetCount(new HashedPassword("1234567"));

                Assert.That(result, Is.EqualTo(2));
            }
Ejemplo n.º 4
0
 public void SetUp()
 {
     _sut = new FiveCharOnlyResponse(ValidContent);
 }
Ejemplo n.º 5
0
 public void WhenContentIsEmpty_ThenThrowException()
 {
     Assert.Throws <ArgumentException>(() => _ = new FiveCharOnlyResponse(string.Empty));
 }
Ejemplo n.º 6
0
 public void WhenContentIsNull_ThenThrowException()
 {
     Assert.Throws <ArgumentException>(() => _ = new FiveCharOnlyResponse(null));
 }