class U2F::SignResponse

Attributes

client_data[RW]
client_data_json[RW]
key_handle[RW]
signature_data[RW]

Public Class Methods

load_from_json(json) click to toggle source
# File lib/u2f/sign_response.rb, line 5
def self.load_from_json(json)
  data = ::JSON.parse(json)
  instance = new
  instance.client_data_json =
    ::U2F.urlsafe_decode64(data['clientData'])
  instance.client_data =
    ClientData.load_from_json(instance.client_data_json)
  instance.key_handle = data['keyHandle']
  instance.signature_data =
    ::U2F.urlsafe_decode64(data['signatureData'])
  instance
end

Public Instance Methods

counter() click to toggle source

Counter value that the U2F token increments every time it performs an authentication operation

# File lib/u2f/sign_response.rb, line 21
def counter
  signature_data.byteslice(1, 4).unpack('N').first
end
signature() click to toggle source

signature is to be verified using the public key obtained during registration.

# File lib/u2f/sign_response.rb, line 28
def signature
  signature_data.byteslice(5..-1)
end
user_present?() click to toggle source

If user presence was verified

# File lib/u2f/sign_response.rb, line 34
def user_present?
  signature_data.byteslice(0).unpack('C').first == 1
end
verify(app_id, public_key_pem) click to toggle source

Verifies the response against an app id and the public key of the registered device

# File lib/u2f/sign_response.rb, line 41
def verify(app_id, public_key_pem)
  data = [
    ::U2F::DIGEST.digest(app_id),
    signature_data.byteslice(0, 5),
    ::U2F::DIGEST.digest(client_data_json)
  ].join

  public_key = OpenSSL::PKey.read(public_key_pem)

  begin
    public_key.verify(::U2F::DIGEST.new, signature, data)
  rescue OpenSSL::PKey::PKeyError
    false
  end
end