Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/webauthn/authenticator_assertion_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class SignCountVerificationError < VerificationError; end

class AuthenticatorAssertionResponse < AuthenticatorResponse
def self.from_client(response, relying_party: WebAuthn.configuration.relying_party)
validate_client_response!(response, %w[authenticatorData clientDataJSON signature])
encoder = relying_party.encoder

user_handle =
Expand Down
1 change: 1 addition & 0 deletions lib/webauthn/authenticator_attestation_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class AuthenticatorAttestationResponse < AuthenticatorResponse
extend Forwardable

def self.from_client(response, relying_party: WebAuthn.configuration.relying_party)
validate_client_response!(response, %w[attestationObject clientDataJSON])
encoder = relying_party.encoder

new(
Expand Down
6 changes: 6 additions & 0 deletions lib/webauthn/authenticator_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ class UserPresenceVerificationError < VerificationError; end
class UserVerifiedVerificationError < VerificationError; end

class AuthenticatorResponse
def self.validate_client_response!(response, required_fields)
unless response.is_a?(Hash) && required_fields.all? { |field| response[field].respond_to?(:to_str) }
raise CredentialFormatError, "Credential response must contain string-like #{required_fields.join(', ')} fields"
end
end

def initialize(client_data_json:, relying_party: WebAuthn.configuration.relying_party)
@client_data_json = client_data_json
@relying_party = relying_party
Expand Down
1 change: 1 addition & 0 deletions lib/webauthn/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

module WebAuthn
class Error < StandardError; end
class CredentialFormatError < Error; end
end
6 changes: 6 additions & 0 deletions lib/webauthn/public_key_credential.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "webauthn/encoder"
require "webauthn/error"

module WebAuthn
class PublicKeyCredential
Expand All @@ -9,6 +10,11 @@ class InvalidChallengeError < Error; end
attr_reader :type, :id, :raw_id, :client_extension_outputs, :authenticator_attachment, :response

def self.from_client(credential, relying_party: WebAuthn.configuration.relying_party)
unless credential.is_a?(Hash) && %w[type id rawId].all? { |field| credential[field].respond_to?(:to_str) } &&
credential["response"].is_a?(Hash)
raise CredentialFormatError, "Credential must contain string-like type, id, rawId and an object response"
end

new(
type: credential["type"],
id: credential["id"],
Expand Down