Class: Unmagic::Passkeys::WebAuthn::Authenticator::Response

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/unmagic/passkeys/web_authn/authenticator/response.rb

Overview

Action Pack WebAuthn Authenticator Response

Abstract base class for WebAuthn authenticator responses. Provides common validation logic for both registration (attestation) and authentication (assertion) ceremonies.

This class should not be instantiated directly. Use AttestationResponse for registration or AssertionResponse for authentication.

Validation

The validate! method performs security checks required by the WebAuthn specification:

  • Challenge verification - ensures the response matches the server-generated challenge

  • Origin verification - ensures the response comes from the expected origin

  • User verification - optionally requires biometric or PIN verification

Example

response = Unmagic::Passkeys::WebAuthn::Authenticator::AssertionResponse.new(
  client_data_json: client_data_json,
  authenticator_data: authenticator_data,
  signature: signature,
  credential: credential,
  origin: "https://example.com",
  user_verification: :required
)

response.validate!

Direct Known Subclasses

AssertionResponse, AttestationResponse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_data_json:, origin: nil, user_verification: :preferred) ⇒ Response

Returns a new instance of Response.



47
48
49
50
51
# File 'lib/unmagic/passkeys/web_authn/authenticator/response.rb', line 47

def initialize(client_data_json:, origin: nil, user_verification: :preferred)
  @client_data_json = client_data_json
  @origin = origin
  @user_verification = user_verification.to_sym
end

Instance Attribute Details

#client_data_jsonObject (readonly)

Returns the value of attribute client_data_json.



35
36
37
# File 'lib/unmagic/passkeys/web_authn/authenticator/response.rb', line 35

def client_data_json
  @client_data_json
end

#originObject

Returns the value of attribute origin.



36
37
38
# File 'lib/unmagic/passkeys/web_authn/authenticator/response.rb', line 36

def origin
  @origin
end

#user_verificationObject

Returns the value of attribute user_verification.



36
37
38
# File 'lib/unmagic/passkeys/web_authn/authenticator/response.rb', line 36

def user_verification
  @user_verification
end

Instance Method Details

#authenticator_dataObject



72
73
74
# File 'lib/unmagic/passkeys/web_authn/authenticator/response.rb', line 72

def authenticator_data
  nil
end

#client_dataObject

Parses the client data JSON string into a Hash. Raises InvalidResponseError if the JSON is malformed.



66
67
68
69
70
# File 'lib/unmagic/passkeys/web_authn/authenticator/response.rb', line 66

def client_data
  @client_data ||= JSON.parse(client_data_json)
rescue JSON::ParserError
  raise Unmagic::Passkeys::WebAuthn::InvalidResponseError, "Client data is not valid JSON"
end

#relying_partyObject

Returns the RelyingParty used for RP ID validation.



60
61
62
# File 'lib/unmagic/passkeys/web_authn/authenticator/response.rb', line 60

def relying_party
  Unmagic::Passkeys::WebAuthn.relying_party
end

#validate!Object



53
54
55
56
57
# File 'lib/unmagic/passkeys/web_authn/authenticator/response.rb', line 53

def validate!
  super
rescue ActiveModel::ValidationError
  raise Unmagic::Passkeys::WebAuthn::InvalidResponseError, errors.full_messages.join(", ")
end