Module: Devise::Webauthn::Test::AuthenticatorHelpers

Defined in:
lib/devise/webauthn/test/authenticator_helpers.rb

Instance Method Summary collapse

Instance Method Details

#add_credential_to_authenticator(authenticator, resource, passkey:, name: "My Credential") ⇒ Object

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength



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
50
51
52
53
# File 'lib/devise/webauthn/test/authenticator_helpers.rb', line 25

def add_credential_to_authenticator(authenticator, resource, passkey:, name: "My Credential")
  credential_id = SecureRandom.random_bytes(16)
  encoded_credential_id = Base64.urlsafe_encode64(credential_id)
  key = OpenSSL::PKey.generate_key("ED25519")
  encoded_private_key = Base64.urlsafe_encode64(key.private_to_der)

  cose_public_key = COSE::Key::OKP.from_pkey(OpenSSL::PKey.read(key.public_to_der))
  cose_public_key.alg = -8
  encoded_cose_public_key = Base64.urlsafe_encode64(cose_public_key.serialize)

  credential_json = {
    "credentialId" => encoded_credential_id,
    "isResidentCredential" => passkey,
    "rpId" => "localhost",
    "privateKey" => encoded_private_key,
    "signCount" => 0
  }
  credential_json["userHandle"] = resource.ensure_webauthn_id! if passkey

  authenticator.add_credential(credential_json)

  resource.webauthn_credentials.create!(
    name: name,
    external_id: Base64.urlsafe_encode64(credential_id, padding: false),
    public_key: encoded_cose_public_key,
    sign_count: 0,
    authentication_factor: passkey ? :first_factor : :second_factor
  )
end

#add_passkey_to_authenticator(authenticator, resource, name: "My Passkey") ⇒ Object



15
16
17
# File 'lib/devise/webauthn/test/authenticator_helpers.rb', line 15

def add_passkey_to_authenticator(authenticator, resource, name: "My Passkey")
  add_credential_to_authenticator(authenticator, resource, passkey: true, name: name)
end

#add_security_key_to_authenticator(authenticator, resource, name: "My Security Key") ⇒ Object



19
20
21
# File 'lib/devise/webauthn/test/authenticator_helpers.rb', line 19

def add_security_key_to_authenticator(authenticator, resource, name: "My Security Key")
  add_credential_to_authenticator(authenticator, resource, passkey: false, name: name)
end

#add_virtual_authenticatorObject



7
8
9
10
11
12
13
# File 'lib/devise/webauthn/test/authenticator_helpers.rb', line 7

def add_virtual_authenticator
  options = Selenium::WebDriver::VirtualAuthenticatorOptions.new
  options.user_verification = true
  options.user_verified = true
  options.resident_key = true
  page.driver.browser.add_virtual_authenticator(options)
end