Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
root = true

[*]
charset = utf-8
end_of_line = crlf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

[*.{cs,csproj,props,targets,sln}]
charset = utf-8-bom

[*.{json,yml,yaml}]
indent_size = 2

[*.md]
trim_trailing_whitespace = false
23 changes: 23 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
version: 2
updates:
- package-ecosystem: nuget
directory: /
schedule:
interval: weekly
open-pull-requests-limit: 10
commit-message:
prefix: chore
include: scope
groups:
tests:
patterns: ["xunit*", "AwesomeAssertions"]
ignore:
# Never reintroduce FluentAssertions (commercial license since v8)
- dependency-name: "FluentAssertions"

- package-ecosystem: github-actions
directory: /
schedule:
interval: weekly
commit-message:
prefix: ci
72 changes: 48 additions & 24 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,79 @@ name: NetDevPack - MAIN Publish
on:
push:
branches: [ main ]
workflow_dispatch:
inputs:
version:
description: Existing release to publish again (e.g. 4.0.0). Builds tag v<version> and pushes it to nuget.org.
required: true

env:
REPOSITORY_URL: https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json
CURRENT_REPO_URL: https://github.com/${{ github.repository }}

permissions:
contents: write
issues: write
pull-requests: write
id-token: write # NuGet trusted publishing (OIDC)

jobs:
publish:
runs-on: ubuntu-latest
defaults:
run:
working-directory: src

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Setup .NET 8
uses: actions/setup-dotnet@v3
uses: actions/checkout@v7
with:
dotnet-version: 8.0.x
fetch-depth: 0
ref: ${{ inputs.version && format('v{0}', inputs.version) || github.sha }}

- name: Setup .NET 9
uses: actions/setup-dotnet@v3
- name: Setup .NET
uses: actions/setup-dotnet@v6
with:
dotnet-version: 9.0.x

- name: Setup .NET 10
uses: actions/setup-dotnet@v3
with:
dotnet-version: 10.0.x
global-json-file: global.json

- name: Restore dependencies
run: dotnet restore
run: dotnet restore src/NetDevPack.Fido2.sln

- name: Build
run: dotnet build --no-restore
run: dotnet build src/NetDevPack.Fido2.sln --no-restore -c Release

- name: Test
run: dotnet test --solution src/NetDevPack.Fido2.sln --no-build -c Release

- name: Semantic Release
id: semantic
if: github.event_name == 'push'
uses: cycjimmy/semantic-release-action@v4
with:
semantic_version: 24
extra_plugins: |
@semantic-release/changelog
@semantic-release/git
@semantic-release/changelog@6
@semantic-release/git@10
conventional-changelog-conventionalcommits@8
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Resolve version
id: version
if: github.event_name == 'workflow_dispatch' || steps.semantic.outputs.new_release_published == 'true'
env:
VERSION: ${{ inputs.version || steps.semantic.outputs.new_release_version }}
run: |
[[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]] || { echo "::error::Invalid version '$VERSION'"; exit 1; }
echo "value=$VERSION" >> "$GITHUB_OUTPUT"

- name: Generate Package
run: dotnet pack -c Release -o out -p:PackageVersion=${{ steps.semantic.outputs.new_release_version }} -p:RepositoryUrl=${{env.CURRENT_REPO_URL}}
if: steps.version.outputs.value != ''
run: dotnet pack src/NetDevPack.Fido2.EntityFramework.Store/NetDevPack.Fido2.EntityFramework.Store.csproj -c Release -o out -p:Version=${{ steps.version.outputs.value }} -p:RepositoryUrl=${{ env.CURRENT_REPO_URL }}

- name: NuGet login (trusted publishing)
id: nuget
if: steps.version.outputs.value != ''
uses: NuGet/login@v1
with:
user: ${{ vars.NUGET_USER }}

- name: Publish the package to nuget.org
run: dotnet nuget push ./out/*.nupkg -n -d -k ${{ secrets.NUGET_AUTH_TOKEN}} -s https://api.nuget.org/v3/index.json

if: steps.version.outputs.value != ''
run: dotnet nuget push ./out/*.nupkg --skip-duplicate -k ${{ steps.nuget.outputs.NUGET_API_KEY }} -s https://api.nuget.org/v3/index.json
38 changes: 18 additions & 20 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
@@ -1,39 +1,37 @@
name: NetDevPack - MASTER PR
name: NetDevPack - MAIN PR

on:
pull_request:
branches: [ main ]

permissions:
contents: read

jobs:
pull-request:
runs-on: ubuntu-latest
defaults:
run:
working-directory: src

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Setup .NET 8
uses: actions/setup-dotnet@v3
uses: actions/checkout@v7
with:
dotnet-version: 8.0.x
fetch-depth: 0

- name: Setup .NET 9
uses: actions/setup-dotnet@v3
- name: Setup .NET
uses: actions/setup-dotnet@v6
with:
dotnet-version: 9.0.x

- name: Setup .NET 10
uses: actions/setup-dotnet@v3
with:
dotnet-version: 10.0.x
global-json-file: global.json

- name: Restore dependencies
run: dotnet restore
run: dotnet restore src/NetDevPack.Fido2.sln

- name: Build
run: dotnet build --no-restore
run: dotnet build src/NetDevPack.Fido2.sln --no-restore -c Release

- name: Test
run: dotnet test --no-build
run: dotnet test --solution src/NetDevPack.Fido2.sln --no-build -c Release

- name: Check vulnerable packages
run: |
dotnet list src/NetDevPack.Fido2.sln package --vulnerable --include-transitive 2>&1 | tee vulnerable.txt
! grep -q "has the following vulnerable packages" vulnerable.txt
31 changes: 21 additions & 10 deletions .releaserc
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
{
"branches": ["main"],

"plugins": [
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
"@semantic-release/changelog",
"@semantic-release/github",
"@semantic-release/git"
]
}
"branches": [
"main"
],
"plugins": [
[
"@semantic-release/commit-analyzer",
{
"preset": "conventionalcommits"
}
],
[
"@semantic-release/release-notes-generator",
{
"preset": "conventionalcommits"
}
],
"@semantic-release/changelog",
"@semantic-release/github",
"@semantic-release/git"
]
}
11 changes: 11 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project>
<!-- Shared settings for every project under this repository -->
<PropertyGroup>
<LangVersion>latest</LangVersion>
<!-- Fail the build when a dependency has a known vulnerability (NuGet Audit) -->
<NuGetAudit>true</NuGetAudit>
<NuGetAuditMode>all</NuGetAuditMode>
<WarningsAsErrors>$(WarningsAsErrors);NU1901;NU1902;NU1903;NU1904</WarningsAsErrors>
<ContinuousIntegrationBuild Condition="'$(CI)' == 'true'">true</ContinuousIntegrationBuild>
</PropertyGroup>
</Project>
33 changes: 33 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<Project>
<!--
Central Package Management: every version lives here, csproj files only declare <PackageReference Include="..." />.
Microsoft.* packages that ship with .NET follow the target framework (net10.0 -> 10.0.x).
-->
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
<CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled>
</PropertyGroup>

<ItemGroup>
<!-- Library -->
<PackageVersion Include="Fido2" Version="4.3.3" />
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="10.0.15" />

<!-- Demo -->
<PackageVersion Include="Fido2.AspNet" Version="4.3.3" />
<PackageVersion Include="NetDevPack" Version="8.0.2" />
<PackageVersion Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="10.0.15" />
<PackageVersion Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="10.0.15" />
<PackageVersion Include="Microsoft.AspNetCore.Identity.UI" Version="10.0.15" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.InMemory" Version="10.0.15" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="10.0.15" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Tools" Version="10.0.15" />
<PackageVersion Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.23.0" />

<!-- Tests -->
<PackageVersion Include="AwesomeAssertions" Version="9.6.0" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.15" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="10.0.15" />
<PackageVersion Include="xunit.v3" Version="4.0.3" />
</ItemGroup>
</Project>
89 changes: 82 additions & 7 deletions README.MD
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
# Fido2 EntityFramework.Store

A thin wrapper around [Fido2](https://github.com/passwordless-lib/fido2-net-lib) adding store capabilities to it.
An Entity Framework Core store for [Fido2](https://github.com/passwordless-lib/fido2-net-lib) (WebAuthn / passkeys).

## Purpouse
## Purpose

Provide a better integration between [Fido2](https://github.com/passwordless-lib/fido2-net-lib) library and ASP.NET Identity
Provide a better integration between the [Fido2](https://github.com/passwordless-lib/fido2-net-lib) library and ASP.NET Core Identity.

## Compatibility

| NetDevPack.Fido2.EntityFramework.Store | Fido2 | Target framework |
| -------------------------------------- | ----- | ---------------- |
| 4.x | 4.x | .NET 10 (LTS) |
| 3.x | 3.x | .NET 8, .NET 9, .NET 10 |

## How to implement

1. Add at `Program.cs`
1. Add to `Program.cs`
```csharp
builder.Services.AddFido2(builder.Configuration.GetSection("Fido2"));
builder.Services.AddFido2Context<ApplicationDbContext>();
```

Expand All @@ -25,12 +33,79 @@ public class ApplicationDbContext : IdentityDbContext, IFido2Context
{
}

public DbSet<StoredCredential> Fido2StoredCredential { get; set; }
public DbSet<StoredCredentialDetail> Fido2StoredCredential { get; set; }
}
```

3. Now you can inject `IFido2Store` to save and recover `StoredCredential`
3. Inject `IFido2Store` to save and recover credentials:
```csharp
// Registration
var credential = await _fido2.MakeNewCredentialAsync(new MakeNewCredentialParams
{
AttestationResponse = attestationResponse,
OriginalOptions = options,
IsCredentialIdUniqueToUserCallback = async (args, ct) =>
!(await _fido2Store.ListCredentialsByPublicKeyIdAsync(args.CredentialId)).Any()
});
_fido2Store.Store("My security key", fidoUser, StoredCredential.FromRegisteredCredential(credential));

// Login
var stored = await _fido2Store.GetCredentialByPublicKeyIdAsync(assertionResponse.RawId);
var result = await _fido2.MakeAssertionAsync(new MakeAssertionParams
{
AssertionResponse = assertionResponse,
OriginalOptions = options,
StoredPublicKey = stored.PublicKey,
StoredSignatureCounter = stored.SignatureCounter,
IsUserHandleOwnerOfCredentialIdCallback = async (args, ct) =>
(await _fido2Store.GetCredentialsByUserHandleAsync(args.UserHandle)).Any(c => c.Descriptor.Id.SequenceEqual(args.CredentialId))
});
await _fido2Store.UpdateCounter(result.CredentialId, result.SignCount, result.IsBackedUp);
```

## Upgrading from 3.x to 4.x

4.x targets **.NET 10** and **Fido2 4**. Stay on 3.x for .NET 8/9 or Fido2 3.

### API changes
- `Fido2NetLib.Development.StoredCredential` does not exist in Fido2 4. The store now uses its own
`NetDevPack.Fido2.EntityFramework.Store.Model.StoredCredential`. It keeps the 3.x property names (`UserId`, `Descriptor`, `PublicKey`,
`UserHandle`, `SignatureCounter`, `CredType`, `RegDate`, `AaGuid`). Replace `using Fido2NetLib.Development;` with
`using NetDevPack.Fido2.EntityFramework.Store.Model;`.
- New `StoredCredential.FromRegisteredCredential(RegisteredPublicKeyCredential)` builds a credential from the result of
`IFido2.MakeNewCredentialAsync`, including transports and backup flags.
- `IFido2Store.GetCredentialByPublicKeyIdAsync` returns `StoredCredential?`.
- `IFido2Store.UpdateCounter(byte[] credentialId, uint counter, bool? isBackedUp = null)` also stores the Backup State flag.
- Fido2 4 itself changed its API: `MakeNewCredentialAsync`, `MakeAssertionAsync`, `RequestNewCredential` and `GetAssertionOptions` now take
`*Params` objects, `MakeAssertionAsync` throws `Fido2VerificationException` instead of returning a status, and options no longer carry
`Status`/`ErrorMessage`. Use `AuthenticatorAssertionRawResponse.RawId` (bytes) to look up credentials, since `Id` is now a string.
See the [Fido2 releases](https://github.com/passwordless-lib/fido2-net-lib/releases).

### Database schema
Two columns were added to `StoredCredentialDetail`. Nothing was renamed or removed, and existing rows keep working:

| Column | Type | Default for existing rows |
| ------------------ | ------------- | ------------------------- |
| `IsBackupEligible` | `bool` (not null) | `false` |
| `IsBackedUp` | `bool` (not null) | `false` |

Create a migration in your application:
```bash
dotnet ef migrations add Fido2StoreV4 -c ApplicationDbContext
dotnet ef database update -c ApplicationDbContext
```

The generated migration only adds the two columns with `defaultValue: false`. For SQL Server it is equivalent to:
```sql
ALTER TABLE [Fido2StoredCredential] ADD [IsBackupEligible] bit NOT NULL DEFAULT CAST(0 AS bit);
ALTER TABLE [Fido2StoredCredential] ADD [IsBackedUp] bit NOT NULL DEFAULT CAST(0 AS bit);
```

`IsBackedUp` is refreshed on the next successful login when you pass `result.IsBackedUp` to `UpdateCounter`. `IsBackupEligible` cannot be known for credentials registered with 3.x, so it stays `false` for them.

## Demo

Check demo at Source Code.
See the demo in `demo/Fido2.Passwordless`. Docker build context is the repository root:
```bash
docker build -f demo/Fido2.Passwordless/Dockerfile .
```
Binary file added assets/IconNuget.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading