using System.Security.Cryptography;
using System.Text;
using Fleet.Identity;
namespace MoShiro.Backend.Application;
/// 注册成功后要保存的凭证。
public sealed record RegisteredCredential(byte[] CredentialId, byte[] PublicKeySpki, uint SignCount);
///
/// 解析并校验 WebAuthn 注册(navigator.credentials.create 的结果)。只接受 ES256;attestation 一律当 none 处理:
/// 不验设备证明,信任浏览器的实现(docs/access.md)。CBOR 解析只在 Backend 里,节点不带。
///
public static class WebAuthnRegistration
{
public static RegisteredCredential? Verify(
ReadOnlySpan attestationObject, ReadOnlySpan clientDataJson, string rpId, ReadOnlySpan expectedChallenge,
Func originAllowed, bool requireUserVerification)
{
if (!WebAuthnAssertion.ClientDataValid(clientDataJson, "webauthn.create", expectedChallenge, originAllowed))
{
return null;
}
try
{
if (new MiniCbor(attestationObject.ToArray()).ReadValue() is not Dictionary