You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.6 KiB

  1. package model
  2. import (
  3. "testing"
  4. "github.com/go-webauthn/webauthn/protocol"
  5. webauthn "github.com/go-webauthn/webauthn/webauthn"
  6. "github.com/stretchr/testify/require"
  7. )
  8. func TestPasskeyCredentialTransportsRoundTripAndIgnoreMalformedJSON(t *testing.T) {
  9. credential := &PasskeyCredential{}
  10. credential.SetTransports([]protocol.AuthenticatorTransport{protocol.USB, protocol.Internal})
  11. require.Equal(t, []protocol.AuthenticatorTransport{protocol.USB, protocol.Internal}, credential.TransportList())
  12. credential.Transports = "not-json"
  13. require.Nil(t, credential.TransportList())
  14. credential.SetTransports(nil)
  15. require.Empty(t, credential.Transports)
  16. }
  17. func TestPasskeyCredentialConvertsWebAuthnFieldsWithoutLosingFlags(t *testing.T) {
  18. webCredential := &webauthn.Credential{
  19. ID: []byte("credential"), PublicKey: []byte("public-key"), AttestationType: "none",
  20. Transport: []protocol.AuthenticatorTransport{protocol.Internal},
  21. Flags: webauthn.CredentialFlags{UserPresent: true, UserVerified: true, BackupEligible: true},
  22. Authenticator: webauthn.Authenticator{AAGUID: []byte("aaguid"), SignCount: 8, Attachment: protocol.Platform},
  23. }
  24. stored := NewPasskeyCredentialFromWebAuthn(9, webCredential)
  25. require.NotNil(t, stored)
  26. require.Equal(t, 9, stored.UserID)
  27. require.True(t, stored.UserVerified)
  28. require.EqualValues(t, 8, stored.SignCount)
  29. roundTripped := stored.ToWebAuthnCredential()
  30. require.Equal(t, webCredential.ID, roundTripped.ID)
  31. require.Equal(t, webCredential.PublicKey, roundTripped.PublicKey)
  32. require.True(t, roundTripped.Flags.UserVerified)
  33. require.Equal(t, webCredential.Transport, roundTripped.Transport)
  34. }