Module: Practical::Test::Helpers::Passkey::System::Selenium

Extended by:
ActiveSupport::Concern
Includes:
Base
Defined in:
app/lib/practical/test/helpers/passkey/system/selenium.rb

Instance Method Summary collapse

Instance Method Details

#add_virtual_authenticator(options: authenticator_options) ⇒ Object



69
70
71
# File 'app/lib/practical/test/helpers/passkey/system/selenium.rb', line 69

def add_virtual_authenticator(options: authenticator_options)
  @virtual_authenticator ||= Capybara.current_session.driver.browser.add_virtual_authenticator(options)
end

#authenticator_options(options: {}) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'app/lib/practical/test/helpers/passkey/system/selenium.rb', line 59

def authenticator_options(options: {})
  options = options.reverse_merge({
    protocol: :ctap2,
    resident_key: true,
    user_verification: true,
    user_verified: true
  })
  Selenium::WebDriver::VirtualAuthenticatorOptions.new(**options)
end

#create_credential(rp_id:, user_handle: generate_user_handle, id:, keypair:) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/lib/practical/test/helpers/passkey/system/selenium.rb', line 77

def create_credential(rp_id:, user_handle: generate_user_handle, id:, keypair:)
  private_key = Base64.strict_encode64(keypair.private_to_der)
  decoded_private_key = Base64.strict_decode64(private_key).bytes

  return Selenium::WebDriver::Credential.resident(
    id: id,
    private_key: decoded_private_key,
    rp_id: rp_id,
    user_handle: user_handle
  )

  Selenium::WebDriver::Credential.resident(
    id: generate_credential_id,
    private_key: decoded_private_key,
    rp_id: "localhost",
    user_handle: generate_user_handle
  )

  keypair.public_key.to_bn.to_s(2)
  private_key = keypair.private_key.to_bn.to_s(2)
  return Selenium::WebDriver::Credential.resident(id: id.bytes,
                                                  private_key: private_key.bytes,
                                                  rp_id: rp_id,
                                                  user_handle: user_handle.bytes
                                                 )
end

#create_passkey_for_user_and_return_webauthn_credential(user:) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/lib/practical/test/helpers/passkey/system/selenium.rb', line 31

def create_passkey_for_user_and_return_webauthn_credential(user:)
  webauthn_credential = create_webauthn_credential_from_scratch(webauthn_client: user_webauthn_client,
                                                                rp_id: user_relying_party_id,
                                                                relying_party: user_relying_party
                                                               )
  # rubocop:disable Layout/LineLength
  keypair = fake_authenticator.instance_variable_get("@credentials")[user_relying_party_id][webauthn_credential.id][:credential_key]
  # rubocop:enable Layout/LineLength

  resident_credential = create_credential(
    rp_id: user_relying_party_id,
    id: webauthn_credential.id.bytes,
    keypair: keypair
  )

  authenticator = add_virtual_authenticator
  authenticator.add_credential(resident_credential)

  user.passkeys.create!(
    label: SecureRandom.hex,
    external_id: Base64.strict_encode64(webauthn_credential.id),
    public_key: Base64.strict_encode64(webauthn_credential.public_key),
    sign_count: 0
  )

  return webauthn_credential
end

#default_authenticatorObject



73
74
75
# File 'app/lib/practical/test/helpers/passkey/system/selenium.rb', line 73

def default_authenticator
  @virtual_authenticator
end

#generate_user_handleObject



104
105
106
# File 'app/lib/practical/test/helpers/passkey/system/selenium.rb', line 104

def generate_user_handle
  SecureRandom.uuid.bytes
end