Class: WebAuthn::AttestedCredentialData

Inherits:
Object
  • Object
show all
Defined in:
lib/web_authn/attested_credential_data.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aaguid:, credential_id:, public_key:) ⇒ AttestedCredentialData

Returns a new instance of AttestedCredentialData.



5
6
7
8
9
# File 'lib/web_authn/attested_credential_data.rb', line 5

def initialize(aaguid:, credential_id:, public_key:)
  self.aaguid = aaguid
  self.credential_id = credential_id
  self.public_key = public_key
end

Instance Attribute Details

#aaguidObject

Returns the value of attribute aaguid.



3
4
5
# File 'lib/web_authn/attested_credential_data.rb', line 3

def aaguid
  @aaguid
end

#credential_idObject

Returns the value of attribute credential_id.



3
4
5
# File 'lib/web_authn/attested_credential_data.rb', line 3

def credential_id
  @credential_id
end

#public_keyObject

Returns the value of attribute public_key.



3
4
5
# File 'lib/web_authn/attested_credential_data.rb', line 3

def public_key
  @public_key
end

Class Method Details

.decode(attested_credential_data) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/web_authn/attested_credential_data.rb', line 12

def decode(attested_credential_data)
  length = (
    ((attested_credential_data.getbyte(16) << 8) & 0xFF) +
    (attested_credential_data.getbyte(17) & 0xFF)
  )
  aaguid,
  credential_id,
  _encoded_cose_key_ = [
    attested_credential_data.byteslice(0...16),
    attested_credential_data.byteslice(18...(18 + length)),
    attested_credential_data.byteslice((18 + length)..-1),
  ]
  cose_key = COSE::Key::EC2.from_cbor(_encoded_cose_key_)
  curve_name = case cose_key.curve
  when 1
    'prime256v1'
  when 2
    'secp384r1'
  when 3
    'secp521r1'
  else
    raise NotImplementedError, 'Non-supported EC curve'
  end
  ec_key = OpenSSL::PKey::EC.new curve_name
  ec_key.public_key = OpenSSL::PKey::EC::Point.new(
    OpenSSL::PKey::EC::Group.new(curve_name),
    OpenSSL::BN.new([
      '04' +
      cose_key.x_coordinate.unpack('H*').first +
      cose_key.y_coordinate.unpack('H*').first
    ].pack('H*'), 2)
  )
  new(
    aaguid: Base64.urlsafe_encode64(aaguid, padding: false),
    credential_id: Base64.urlsafe_encode64(credential_id, padding: false),
    public_key: ec_key
  )
end