-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
383f4b0
commit f52e1b0
Showing
14 changed files
with
301 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,99 @@ | ||
# Have I Been Pwned? — .NET Client | ||
# ![';-- have i been pwned? — .NET HTTP Client logo.](https://raw.githubusercontent.com/IEvangelist/pwned-client/main/assets/pwned-header.png) | ||
|
||
[![build](https://github.com/IEvangelist/pwned-client/actions/workflows/build-validation.yml/badge.svg)](https://github.com/IEvangelist/pwned-client/actions/workflows/build-validation.yml) [![code analysis](https://github.com/IEvangelist/pwned-client/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/IEvangelist/pwned-client/actions/workflows/codeql-analysis.yml) | ||
[![build](https://github.com/IEvangelist/pwned-client/actions/workflows/build-validation.yml/badge.svg)](https://github.com/IEvangelist/pwned-client/actions/workflows/build-validation.yml) [![code analysis](https://github.com/IEvangelist/pwned-client/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/IEvangelist/pwned-client/actions/workflows/codeql-analysis.yml) [![NuGet](https://img.shields.io/nuget/v/HaveIBeenPwned.Client.svg?style=flat)](https://www.nuget.org/packages/HaveIBeenPwned.Client) | ||
|
||
A .NET HTTP client for the "Have I Been Pwned" API | ||
A .NET HTTP client for the "Have I Been Pwned" API. | ||
|
||
## Get started | ||
|
||
Install from the .NET CLI: | ||
|
||
```shell | ||
dotnet add package HaveIBeenPwned.Client | ||
``` | ||
|
||
Alternatively add manually to your consuming _.csproj_: | ||
|
||
```xml | ||
<PackageReference Include="HaveIBeenPwned.Client" Version="{VersionNumber}" /> | ||
``` | ||
|
||
Or, install using the NuGet Package Manager: | ||
|
||
```powershell | ||
Install-Package HaveIBeenPwned.Client | ||
``` | ||
|
||
### Dependency injection | ||
|
||
To add all of the services to the dependency injection container, call one of the `AddPwnedServices` overloads. From Minimal APIs for example, with using a named configuration section: | ||
|
||
```csharp | ||
builder.Services.AddPwnedServices( | ||
builder.Configuration.GetSection(nameof(HibpOptions))); | ||
``` | ||
|
||
From a `ConfigureServices` method, with an `IConfiguration` instance: | ||
|
||
```csharp | ||
services.AddPwnedServices(options => | ||
{ | ||
options.ApiKey = _configuration["HibpOptions:ApiKey"]; | ||
options.UserAgent = _configuration["HibpOptions:UserAgent"]; | ||
}); | ||
``` | ||
|
||
Then you can require any of the available DI-ready types: | ||
|
||
- `IPwnedBreachesClient`: [Breaches API](https://haveibeenpwned.com/API/v3#BreachesForAccount). | ||
- `IPwnedPastesClient`: [Pastes API](https://haveibeenpwned.com/API/v3#PastesForAccount). | ||
- `IPwnedPasswordsClient`: [Pwned Passwords API](https://haveibeenpwned.com/API/v3#PwnedPasswords). | ||
- `IPwnedClient`: Marker interface, for conveniently injecting all of the above clients into a single client. | ||
|
||
### Example Minimal APIs | ||
|
||
![Minimal APIs example code.](https://raw.githubusercontent.com/IEvangelist/pwned-client/main/assets/minimal-api.svg) | ||
|
||
## Configuration | ||
|
||
To configure the `HaveIBeenPwned.Client`, the following table identifies the well-known configuration object: | ||
|
||
### Well-known keys | ||
|
||
Depending on the [.NET configuration provider](https://docs.microsoft.com/dotnet/core/extensions/configuration-providers?WC.m_id=dapine) your app is using, there are several well-known keys that map to the `HibpOptions` that configure your usage of the HTTP client. When using environment variables, such as those in Azure App Service configuration or Azure Key Vault secrets, the following keys map to the `HibpOption` instance: | ||
|
||
| Key | Data type | Default value | | ||
|--------------------------|-----------|--------------------------| | ||
| `HibpOptions__ApiKey` | `string` | `null` | | ||
| `HibpOptions__UserAgent` | `string` | `".NET HIBP Client/1.0"` | | ||
|
||
The `ApiKey` is required, to get one — sign up here: <https://haveibeenpwned.com/api/key> | ||
|
||
### Example `appsettings.json` | ||
|
||
```json | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft": "Warning", | ||
"Microsoft.Hosting.Lifetime": "Information" | ||
} | ||
}, | ||
"AllowedHosts": "*", | ||
"HibpOptions": { | ||
"ApiKey": "<YourApiKey>", | ||
"UserAgent": "<YourUserAgent>" | ||
} | ||
} | ||
``` | ||
|
||
For more information, see [JSON configuration provider](https://docs.microsoft.com/dotnet/core/extensions/configuration-providers?WC.m_id=dapine#json-configuration-provider). | ||
|
||
<!-- | ||
Notes for tagging releases: | ||
https://rehansaeed.com/the-easiest-way-to-version-nuget-packages/#minver | ||
git tag -a 0.0.3 -m "Build version 0.0.3" | ||
git push upstream --tags | ||
--> | ||
--> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/HaveIBeenPwned.Client/Extensions/PwnedPasswordsClientExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright (c) David Pine. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace HaveIBeenPwned.Client.Extensions | ||
{ | ||
/// <summary></summary> | ||
public static class PwnedPasswordsClientExtensions | ||
{ | ||
/// <summary> | ||
/// An extension method that evaluates whether the <paramref name="plainTextPassword"/> is "pwned". | ||
/// When <c>true</c>, the <c>Count</c> is at least <c>1</c>. | ||
/// </summary> | ||
/// <param name="pwnedPasswordsClient"></param> | ||
/// <param name="plainTextPassword"></param> | ||
/// <returns> | ||
/// <list type="bullet"> | ||
/// <item> | ||
/// When the given <paramref name="plainTextPassword"/> is "pwned", returns <c>(true, 3)</c> when "pwned" three times. | ||
/// </item> | ||
/// <item> | ||
/// When the given <paramref name="plainTextPassword"/> <strong>isn't</strong> "pwned", this could return <c>(false, 0)</c>. | ||
/// </item> | ||
/// <item> | ||
/// When unable to determine, returns <c>(null, null)</c>. | ||
/// </item> | ||
/// </list> | ||
/// </returns> | ||
public static async ValueTask<(bool? IsPwned, long? Count)> IsPasswordPwnedAsync( | ||
this IPwnedPasswordsClient pwnedPasswordsClient, string plainTextPassword) | ||
{ | ||
var pwnedPassword = await pwnedPasswordsClient.GetPwnedPasswordAsync(plainTextPassword); | ||
|
||
return | ||
( | ||
IsPwned: pwnedPassword.IsPwned ?? false, | ||
Count: pwnedPassword.PwnedCount | ||
); | ||
} | ||
|
||
/// <summary> | ||
/// An extension method that evaluates whether the <paramref name="account"/> is part of a breach. | ||
/// When <c>true</c>, the <c>Breaches</c> has at least one breach name. | ||
/// </summary> | ||
/// <param name="pwnedBreachesClient"></param> | ||
/// <param name="account"></param> | ||
/// <returns> | ||
/// <list type="bullet"> | ||
/// <item> | ||
/// When the given <paramref name="account"/> is part of a breach, returns | ||
/// <c>(true, ["Adobe", "LinkedIn"])</c> when the found in the Adobe and LinkedIn breaches. | ||
/// </item> | ||
/// <item> | ||
/// When the given <paramref name="account"/> <strong>isn't</strong> part of a breach, returns <c>(false, [])</c>. | ||
/// </item> | ||
/// <item> | ||
/// When unable to determine, returns <c>(null, null)</c>. | ||
/// </item> | ||
/// </list> | ||
/// </returns> | ||
public static async ValueTask<(bool? IsBreached, string[]? Breaches)> IsBreachedAccountAsync( | ||
this IPwnedBreachesClient pwnedBreachesClient, string account) | ||
{ | ||
if (string.IsNullOrWhiteSpace(account)) | ||
{ | ||
return (null, null); | ||
} | ||
|
||
var breaches = await pwnedBreachesClient.GetBreachHeadersForAccountAsync(account); | ||
|
||
return | ||
( | ||
IsBreached: breaches is { Length: > 0 }, | ||
Breaches: breaches?.Select(breach => breach.Name)?.ToArray() ?? Array.Empty<string>() | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.