|
| 1 | +package test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/descope/virtualwebauthn" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +// TestAttestationClientExtensionResults verifies that attestation responses include |
| 12 | +// an empty clientExtensionResults map |
| 13 | +func TestAttestationClientExtensionResults(t *testing.T) { |
| 14 | + // Create a mock relying party, mock authenticator and a mock credential |
| 15 | + rp := virtualwebauthn.RelyingParty{Name: WebauthnDisplayName, ID: WebauthnDomain, Origin: WebauthnOrigin} |
| 16 | + authenticator := virtualwebauthn.NewAuthenticator() |
| 17 | + cred := virtualwebauthn.NewCredential(virtualwebauthn.KeyTypeEC2) |
| 18 | + |
| 19 | + // Start an attestation request |
| 20 | + attestation := startWebauthnRegister(t) |
| 21 | + |
| 22 | + // Parse the attestation options |
| 23 | + attestationOptions, err := virtualwebauthn.ParseAttestationOptions(attestation.Options) |
| 24 | + require.NoError(t, err) |
| 25 | + require.NotNil(t, attestationOptions) |
| 26 | + |
| 27 | + // Create an attestation response |
| 28 | + attestationResponse := virtualwebauthn.CreateAttestationResponse(rp, authenticator, cred, *attestationOptions) |
| 29 | + require.NotEmpty(t, attestationResponse) |
| 30 | + |
| 31 | + // Parse the response to verify clientExtensionResults |
| 32 | + var response map[string]interface{} |
| 33 | + err = json.Unmarshal([]byte(attestationResponse), &response) |
| 34 | + require.NoError(t, err) |
| 35 | + |
| 36 | + // Verify clientExtensionResults exists |
| 37 | + clientExtensionResults, exists := response["clientExtensionResults"] |
| 38 | + require.True(t, exists, "clientExtensionResults should exist in attestation response") |
| 39 | + require.NotNil(t, clientExtensionResults, "clientExtensionResults should not be nil") |
| 40 | + |
| 41 | + // Verify clientExtensionResults is an empty map |
| 42 | + clientExtensionResultsMap, ok := clientExtensionResults.(map[string]interface{}) |
| 43 | + require.True(t, ok, "clientExtensionResults should be a map") |
| 44 | + require.Empty(t, clientExtensionResultsMap, "clientExtensionResults should be an empty map") |
| 45 | +} |
| 46 | + |
| 47 | +// TestAssertionClientExtensionResults verifies that assertion responses include |
| 48 | +// the empty clientExtensionResults map |
| 49 | +func TestAssertionClientExtensionResults(t *testing.T) { |
| 50 | + // Create a mock relying party, mock authenticator and a mock credential |
| 51 | + rp := virtualwebauthn.RelyingParty{Name: WebauthnDisplayName, ID: WebauthnDomain, Origin: WebauthnOrigin} |
| 52 | + authenticator := virtualwebauthn.NewAuthenticator() |
| 53 | + cred := virtualwebauthn.NewCredential(virtualwebauthn.KeyTypeEC2) |
| 54 | + |
| 55 | + // Register the credential first |
| 56 | + attestation := startWebauthnRegister(t) |
| 57 | + attestationOptions, err := virtualwebauthn.ParseAttestationOptions(attestation.Options) |
| 58 | + require.NoError(t, err) |
| 59 | + |
| 60 | + attestationResponse := virtualwebauthn.CreateAttestationResponse(rp, authenticator, cred, *attestationOptions) |
| 61 | + webauthnCredential := finishWebauthnRegister(t, attestation, attestationResponse) |
| 62 | + |
| 63 | + authenticator.Options.UserHandle = []byte(UserID) |
| 64 | + authenticator.AddCredential(cred) |
| 65 | + |
| 66 | + // Start an assertion request |
| 67 | + assertion := startWebauthnLogin(t, webauthnCredential, cred.ID) |
| 68 | + |
| 69 | + // Parse the assertion options |
| 70 | + assertionOptions, err := virtualwebauthn.ParseAssertionOptions(assertion.Options) |
| 71 | + require.NoError(t, err) |
| 72 | + require.NotNil(t, assertionOptions) |
| 73 | + |
| 74 | + // Create an assertion response |
| 75 | + assertionResponse := virtualwebauthn.CreateAssertionResponse(rp, authenticator, cred, *assertionOptions) |
| 76 | + require.NotEmpty(t, assertionResponse) |
| 77 | + |
| 78 | + // Parse the response to verify clientExtensionResults |
| 79 | + var response map[string]interface{} |
| 80 | + err = json.Unmarshal([]byte(assertionResponse), &response) |
| 81 | + require.NoError(t, err) |
| 82 | + |
| 83 | + // Verify clientExtensionResults exists |
| 84 | + clientExtensionResults, exists := response["clientExtensionResults"] |
| 85 | + require.True(t, exists, "clientExtensionResults should exist in assertion response") |
| 86 | + require.NotNil(t, clientExtensionResults, "clientExtensionResults should not be nil") |
| 87 | + |
| 88 | + // Verify clientExtensionResults is an empty map |
| 89 | + clientExtensionResultsMap, ok := clientExtensionResults.(map[string]interface{}) |
| 90 | + require.True(t, ok, "clientExtensionResults should be a map") |
| 91 | + require.Empty(t, clientExtensionResultsMap, "clientExtensionResults should be an empty map") |
| 92 | +} |
| 93 | + |
| 94 | +// TestBothKeyTypesWithClientExtensionResults verifies that both EC2 and RSA key types |
| 95 | +// work correctly with clientExtensionResults |
| 96 | +func TestBothKeyTypesWithClientExtensionResults(t *testing.T) { |
| 97 | + // Test EC2 key |
| 98 | + t.Run("EC2 Key", func(t *testing.T) { |
| 99 | + testKeyTypeWithClientExtensionResults(t, virtualwebauthn.KeyTypeEC2) |
| 100 | + }) |
| 101 | + |
| 102 | + // Test RSA key |
| 103 | + t.Run("RSA Key", func(t *testing.T) { |
| 104 | + testKeyTypeWithClientExtensionResults(t, virtualwebauthn.KeyTypeRSA) |
| 105 | + }) |
| 106 | +} |
| 107 | + |
| 108 | +func testKeyTypeWithClientExtensionResults(t *testing.T, keyType virtualwebauthn.KeyType) { |
| 109 | + // Create a mock relying party, mock authenticator and a mock credential |
| 110 | + rp := virtualwebauthn.RelyingParty{Name: WebauthnDisplayName, ID: WebauthnDomain, Origin: WebauthnOrigin} |
| 111 | + authenticator := virtualwebauthn.NewAuthenticator() |
| 112 | + cred := virtualwebauthn.NewCredential(keyType) |
| 113 | + |
| 114 | + // Test attestation |
| 115 | + attestation := startWebauthnRegister(t) |
| 116 | + attestationOptions, err := virtualwebauthn.ParseAttestationOptions(attestation.Options) |
| 117 | + require.NoError(t, err) |
| 118 | + |
| 119 | + attestationResponse := virtualwebauthn.CreateAttestationResponse(rp, authenticator, cred, *attestationOptions) |
| 120 | + webauthnCredential := finishWebauthnRegister(t, attestation, attestationResponse) |
| 121 | + |
| 122 | + // Verify attestation response has clientExtensionResults |
| 123 | + var attestationResponseMap map[string]interface{} |
| 124 | + err = json.Unmarshal([]byte(attestationResponse), &attestationResponseMap) |
| 125 | + require.NoError(t, err) |
| 126 | + require.Contains(t, attestationResponseMap, "clientExtensionResults") |
| 127 | + |
| 128 | + // Test assertion |
| 129 | + authenticator.Options.UserHandle = []byte(UserID) |
| 130 | + authenticator.AddCredential(cred) |
| 131 | + |
| 132 | + assertion := startWebauthnLogin(t, webauthnCredential, cred.ID) |
| 133 | + assertionOptions, err := virtualwebauthn.ParseAssertionOptions(assertion.Options) |
| 134 | + require.NoError(t, err) |
| 135 | + |
| 136 | + assertionResponse := virtualwebauthn.CreateAssertionResponse(rp, authenticator, cred, *assertionOptions) |
| 137 | + finishWebauthnLogin(t, assertion, assertionResponse) |
| 138 | + |
| 139 | + // Verify assertion response has clientExtensionResults |
| 140 | + var assertionResponseMap map[string]interface{} |
| 141 | + err = json.Unmarshal([]byte(assertionResponse), &assertionResponseMap) |
| 142 | + require.NoError(t, err) |
| 143 | + require.Contains(t, assertionResponseMap, "clientExtensionResults") |
| 144 | +} |
0 commit comments