API key protection

This commit is contained in:
Paul Schneider 2026-07-05 21:18:37 +01:00
commit 38bb7e40ef
7 changed files with 143 additions and 19 deletions

View file

@ -19,6 +19,9 @@ using System.Threading.Tasks;
using NuGet.Protocol.Core.Types;
using NuGet.Common;
using Isn.Abstract;
using isnd.Helpers;
using Microsoft.AspNetCore.DataProtection;
using System.Security.Cryptography;
namespace isnd.host.tests
{
@ -49,6 +52,17 @@ namespace isnd.host.tests
}
}
[Fact]
public void LegacyApiKeyProtectorCanUnprotectValuesFromOlderClients()
{
const string clearValue = "legacy-api-key";
var legacyProtectedValue = new Isn.DefaultDataProtector().Protect(clearValue);
var unprotected = ApiKeyProtector.UnprotectWithFallback(new ThrowingProtector(), legacyProtectedValue);
Assert.Equal(clearValue, unprotected);
}
[Fact]
void TestDropUser()
{
@ -117,5 +131,14 @@ namespace isnd.host.tests
{
throw new NotImplementedException();
}
private sealed class ThrowingProtector : IDataProtector
{
public byte[] Protect(byte[] plaintext) => throw new CryptographicException("unexpected protect call");
public byte[] Unprotect(byte[] protectedData) => throw new CryptographicException("unexpected unprotect call");
public IDataProtector CreateProtector(string purpose) => this;
}
}
}