Class: U2F::SignResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/u2f/sign_response.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#client_dataObject

Returns the value of attribute client_data.



3
4
5
# File 'lib/u2f/sign_response.rb', line 3

def client_data
  @client_data
end

#client_data_jsonObject

Returns the value of attribute client_data_json.



3
4
5
# File 'lib/u2f/sign_response.rb', line 3

def client_data_json
  @client_data_json
end

#key_handleObject

Returns the value of attribute key_handle.



3
4
5
# File 'lib/u2f/sign_response.rb', line 3

def key_handle
  @key_handle
end

#signature_dataObject

Returns the value of attribute signature_data.



3
4
5
# File 'lib/u2f/sign_response.rb', line 3

def signature_data
  @signature_data
end

Class Method Details

.load_from_json(json) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# 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

Instance Method Details

#counterObject

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



21
22
23
# File 'lib/u2f/sign_response.rb', line 21

def counter
  signature_data.byteslice(1, 4).unpack('N').first
end

#signatureObject

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



28
29
30
# File 'lib/u2f/sign_response.rb', line 28

def signature
  signature_data.byteslice(5..-1)
end

#user_present?Boolean

If user presence was verified

Returns:

  • (Boolean)


34
35
36
# 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) ⇒ Object

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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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