Update as_json to return string keys

This commit is contained in:
Stanko K.R.
2026-03-25 17:14:20 +01:00
parent f5ae2f8076
commit 5d0f52b57c
3 changed files with 42 additions and 36 deletions
@@ -35,12 +35,14 @@ class ActionPack::WebAuthn::PublicKeyCredential::RequestOptions < ActionPack::We
# Returns a Hash suitable for JSON serialization and passing to the # Returns a Hash suitable for JSON serialization and passing to the
# WebAuthn JavaScript API. # WebAuthn JavaScript API.
def as_json(options = {}) def as_json(options = {})
{ json = {
challenge: challenge, challenge: challenge,
rpId: relying_party.id, rpId: relying_party.id,
allowCredentials: credentials.map { |credential| allow_credential_json(credential) }, allowCredentials: credentials.map { |credential| allow_credential_json(credential) },
userVerification: user_verification.to_s userVerification: user_verification.to_s
}.as_json(options) }
json.as_json(options)
end end
private private
@@ -31,24 +31,26 @@ class ActionPack::WebAuthn::PublicKeyCredential::CreationOptionsTest < ActiveSup
end end
test "as_json" do test "as_json" do
assert_equal @options.challenge, @options.as_json[:challenge] json = @options.as_json
assert_equal({ id: "example.com", name: "Example App" }, @options.as_json[:rp]) assert_equal @options.challenge, json["challenge"]
user = @options.as_json[:user] assert_equal({ "id" => "example.com", "name" => "Example App" }, json["rp"])
assert_equal Base64.urlsafe_encode64("user-123", padding: false), user[:id]
assert_equal "user@example.com", user[:name] user = json["user"]
assert_equal "Test User", user[:displayName] assert_equal Base64.urlsafe_encode64("user-123", padding: false), user["id"]
assert_equal "user@example.com", user["name"]
assert_equal "Test User", user["displayName"]
assert_equal [ assert_equal [
{ type: "public-key", alg: -7 }, { "type" => "public-key", "alg" => -7 },
{ type: "public-key", alg: -8 }, { "type" => "public-key", "alg" => -8 },
{ type: "public-key", alg: -257 } { "type" => "public-key", "alg" => -257 }
], @options.as_json[:pubKeyCredParams] ], json["pubKeyCredParams"]
assert_equal "required", @options.as_json[:authenticatorSelection][:residentKey] assert_equal "required", json["authenticatorSelection"]["residentKey"]
assert_equal true, @options.as_json[:authenticatorSelection][:requireResidentKey] assert_equal true, json["authenticatorSelection"]["requireResidentKey"]
assert_equal "preferred", @options.as_json[:authenticatorSelection][:userVerification] assert_equal "preferred", json["authenticatorSelection"]["userVerification"]
end end
test "as_json includes residentKey in authenticatorSelection" do test "as_json includes residentKey in authenticatorSelection" do
@@ -60,12 +62,12 @@ class ActionPack::WebAuthn::PublicKeyCredential::CreationOptionsTest < ActiveSup
relying_party: @relying_party relying_party: @relying_party
) )
assert_equal "required", options.as_json[:authenticatorSelection][:residentKey] assert_equal "required", options.as_json["authenticatorSelection"]["residentKey"]
assert_equal true, options.as_json[:authenticatorSelection][:requireResidentKey] assert_equal true, options.as_json["authenticatorSelection"]["requireResidentKey"]
end end
test "as_json excludes excludeCredentials when empty" do test "as_json excludes excludeCredentials when empty" do
assert_nil @options.as_json[:excludeCredentials] assert_nil @options.as_json["excludeCredentials"]
end end
test "as_json includes excludeCredentials" do test "as_json includes excludeCredentials" do
@@ -83,13 +85,13 @@ class ActionPack::WebAuthn::PublicKeyCredential::CreationOptionsTest < ActiveSup
) )
assert_equal [ assert_equal [
{ type: "public-key", id: "cred-1", transports: [ "usb", "nfc" ] }, { "type" => "public-key", "id" => "cred-1", "transports" => [ "usb", "nfc" ] },
{ type: "public-key", id: "cred-2", transports: [ "internal" ] } { "type" => "public-key", "id" => "cred-2", "transports" => [ "internal" ] }
], options.as_json[:excludeCredentials] ], options.as_json["excludeCredentials"]
end end
test "as_json excludes attestation when none" do test "as_json excludes attestation when none" do
assert_nil @options.as_json[:attestation] assert_nil @options.as_json["attestation"]
end end
test "as_json includes attestation when not none" do test "as_json includes attestation when not none" do
@@ -101,7 +103,7 @@ class ActionPack::WebAuthn::PublicKeyCredential::CreationOptionsTest < ActiveSup
relying_party: @relying_party relying_party: @relying_party
) )
assert_equal "direct", options.as_json[:attestation] assert_equal "direct", options.as_json["attestation"]
end end
test "raises with invalid attestation preference" do test "raises with invalid attestation preference" do
@@ -126,8 +128,8 @@ class ActionPack::WebAuthn::PublicKeyCredential::CreationOptionsTest < ActiveSup
) )
assert_equal [ assert_equal [
{ type: "public-key", id: "cred-1" } { "type" => "public-key", "id" => "cred-1" }
], options.as_json[:excludeCredentials] ], options.as_json["excludeCredentials"]
end end
private private
@@ -31,13 +31,15 @@ class ActionPack::WebAuthn::PublicKeyCredential::RequestOptionsTest < ActiveSupp
end end
test "as_json" do test "as_json" do
assert_equal @options.challenge, @options.as_json[:challenge] json = @options.as_json
assert_equal "example.com", @options.as_json[:rpId]
assert_equal @options.challenge, json["challenge"]
assert_equal "example.com", json["rpId"]
assert_equal [ assert_equal [
{ type: "public-key", id: "credential-1" }, { "type" => "public-key", "id" => "credential-1" },
{ type: "public-key", id: "credential-2" } { "type" => "public-key", "id" => "credential-2" }
], @options.as_json[:allowCredentials] ], json["allowCredentials"]
assert_equal "preferred", @options.as_json[:userVerification] assert_equal "preferred", json["userVerification"]
end end
test "as_json includes transports when present" do test "as_json includes transports when present" do
@@ -52,9 +54,9 @@ class ActionPack::WebAuthn::PublicKeyCredential::RequestOptionsTest < ActiveSupp
) )
assert_equal [ assert_equal [
{ type: "public-key", id: "cred-1", transports: [ "usb", "nfc" ] }, { "type" => "public-key", "id" => "cred-1", "transports" => [ "usb", "nfc" ] },
{ type: "public-key", id: "cred-2", transports: [ "internal" ] } { "type" => "public-key", "id" => "cred-2", "transports" => [ "internal" ] }
], options.as_json[:allowCredentials] ], options.as_json["allowCredentials"]
end end
test "as_json omits transports when empty" do test "as_json omits transports when empty" do
@@ -66,8 +68,8 @@ class ActionPack::WebAuthn::PublicKeyCredential::RequestOptionsTest < ActiveSupp
) )
assert_equal [ assert_equal [
{ type: "public-key", id: "cred-1" } { "type" => "public-key", "id" => "cred-1" }
], options.as_json[:allowCredentials] ], options.as_json["allowCredentials"]
end end
private private