Class: Clarion::Registrator

Inherits:
Object
  • Object
show all
Defined in:
lib/clarion/registrator.rb

Defined Under Namespace

Classes: Error, InvalidAttestation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(counter, rp_name: 'clarion', rp_id:, user_handle: SecureRandom.base64(64), user_name: 'clarion user', display_name: user_name) ⇒ Registrator

Returns a new instance of Registrator.



11
12
13
14
15
16
17
18
# File 'lib/clarion/registrator.rb', line 11

def initialize(counter, rp_name: 'clarion', rp_id:, user_handle: SecureRandom.base64(64), user_name: 'clarion user', display_name: user_name)
  @counter = counter
  @rp_id = rp_id
  @rp_name = rp_name
  @user_handle = user_handle
  @user_name = user_name
  @display_name = display_name
end

Instance Attribute Details

#counterObject (readonly)

Returns the value of attribute counter.



20
21
22
# File 'lib/clarion/registrator.rb', line 20

def counter
  @counter
end

#display_nameObject (readonly)

Returns the value of attribute display_name.



20
21
22
# File 'lib/clarion/registrator.rb', line 20

def display_name
  @display_name
end

#rp_idObject (readonly)

Returns the value of attribute rp_id.



20
21
22
# File 'lib/clarion/registrator.rb', line 20

def rp_id
  @rp_id
end

#rp_nameObject (readonly)

Returns the value of attribute rp_name.



20
21
22
# File 'lib/clarion/registrator.rb', line 20

def rp_name
  @rp_name
end

#user_handleObject (readonly)

Returns the value of attribute user_handle.



20
21
22
# File 'lib/clarion/registrator.rb', line 20

def user_handle
  @user_handle
end

#user_nameObject (readonly)

Returns the value of attribute user_name.



20
21
22
# File 'lib/clarion/registrator.rb', line 20

def user_name
  @user_name
end

Instance Method Details

#challengeObject



22
23
24
# File 'lib/clarion/registrator.rb', line 22

def challenge
  @challenge ||= SecureRandom.random_bytes(32)
end

#credential_creation_optionsObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/clarion/registrator.rb', line 26

def credential_creation_options
  {
    publicKey: {
      timeout: 60000,
      # Convert to ArrayBuffer in register.js
      challenge: challenge.each_byte.map(&:ord),
      attestation: 'none',
      pubKeyCredParams: [WebAuthn::CRED_PARAM_ES256],
      rp: {
        name: rp_name,
      },
      user: {
        id: Base64.decode64(user_handle).each_byte.map(&:ord),
        displayName: display_name,
        name: user_name,
      },
    },
  }
end

#register!(challenge: self.challenge(), origin:, attestation_object:, client_data_json:) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/clarion/registrator.rb', line 47

def register!(challenge: self.challenge(), origin:, attestation_object:, client_data_json:)
  attestation = WebAuthn::AuthenticatorAttestationResponse.new(
    attestation_object: attestation_object,
    client_data_json: client_data_json
  )

  unless attestation.valid?(challenge, origin, rp_id: rp_id)
    raise InvalidAttestation, "invalid attestation"
  end

  key = Key.new(
    type: 'webauthn',
    handle: Base64.urlsafe_encode64(attestation.credential.id).gsub(/\r?\n|=+/,''),
    user_handle: user_handle,
    public_key: Base64.encode64(attestation.credential.public_key).gsub(/\r?\n/,''),
    counter: attestation.authenticator_data.sign_count,
  )
  if counter
    counter.store(key)
  end
  key
end