Skip to content

Commit

Permalink
[rb] update virtual auth implementation to match conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Aug 4, 2022
1 parent 930edbd commit 13975e0
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 390 deletions.
7 changes: 2 additions & 5 deletions rb/lib/selenium/webdriver/common/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,17 +249,14 @@ def execute_async_script(script, *args)
end

#
# virtual-authenticator
# @return [VirtualAuthenticator]
# @see VirtualAuthenticator
#

def add_virtual_authenticator(options)
bridge.add_virtual_authenticator(options)
end

def remove_virtual_authenticator(authenticator)
bridge.remove_virtual_authenticator(authenticator)
end

#-------------------------------- sugar --------------------------------

#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,53 @@
module Selenium
module WebDriver
class VirtualAuthenticator
attr_reader :id, :bridge

attr_reader :options

#
# api private
# Use `Driver#add_virtual_authenticator`
#

def initialize(bridge, authenticator_id)
def initialize(bridge, authenticator_id, options)
@id = authenticator_id
@bridge = bridge
@options = options
@valid = true
end

def add_credential(credential)
credential = credential.as_json
credential[:authenticatorId] = @id
bridge.add_credential credential
@bridge.add_credential credential, @id
end

def credentials
credential_data = bridge.credentials @id
credential_data = @bridge.credentials @id
credential_data.map do |cred|
Credential.from_json(cred)
end
end

def remove_credential(credential_id)
credential_id = Base64.urlsafe_encode64(credential_id.pack('C*')) if credential_id.instance_of?(Array)
bridge.remove_credential credential_id, @id
credential_id = Credential.encode(credential_id) if credential_id.instance_of?(Array)
@bridge.remove_credential credential_id, @id
end

def remove_all_credentials
bridge.remove_all_credentials @id
@bridge.remove_all_credentials @id
end

def user_verified=(verified)
bridge.user_verified verified, @id
@bridge.user_verified verified, @id
end

def remove!
@bridge.remove_virtual_authenticator(@id)
@valid = false
end

def valid?
@valid
end
end # VirtualAuthenticator
end # WebDriver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,33 @@ class VirtualAuthenticatorOptions
PROTOCOL = {ctap2: "ctap2", u2f: "ctap1/u2f"}.freeze
TRANSPORT = {ble: "ble", usb: "usb", nfc: "nfc", internal: "internal"}.freeze

attr_accessor :protocol, :transport, :has_resident_key, :has_user_verification, :is_user_consenting,
:is_user_verified

def initialize(protocol: PROTOCOL[:ctap2], transport: TRANSPORT[:usb], has_resident_key: false,
has_user_verification: false, is_user_consenting: true, is_user_verified: false)
attr_accessor :protocol, :transport, :resident_key, :user_verification, :user_consenting, :user_verified
alias_method :resident_key?, :resident_key
alias_method :user_verification?, :user_verification
alias_method :user_consenting?, :user_consenting
alias_method :user_verified?, :user_verified

def initialize(protocol: :ctap2, transport: :usb, resident_key: false,
user_verification: false, user_consenting: true, user_verified: false)
@protocol = protocol
@transport = transport
@has_resident_key = has_resident_key
@has_user_verification = has_user_verification
@is_user_consenting = is_user_consenting
@is_user_verified = is_user_verified
@resident_key = resident_key
@user_verification = user_verification
@user_consenting = user_consenting
@user_verified = user_verified
end

#
# @api private
#

def as_json(*)
{
protocol: @protocol,
transport: @transport,
hasResidentKey: @has_resident_key,
hasUserVerification: @has_user_verification,
isUserConsenting: @is_user_consenting,
isUserVerified: @is_user_verified
}
{'protocol' => PROTOCOL[protocol],
'transport' => TRANSPORT[transport],
'hasResidentKey' => resident_key?,
'hasUserVerification' => user_verification?,
'isUserConsenting' => user_consenting?,
'isUserVerified' => user_verified?}
end
end # VirtualAuthenticatorOptions
end # WebDriver
Expand Down
16 changes: 8 additions & 8 deletions rb/lib/selenium/webdriver/remote/bridge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -574,31 +574,31 @@ def shadow_root(element)

def add_virtual_authenticator(options)
authenticator_id = execute :add_virtual_authenticator, {}, options.as_json
VirtualAuthenticator.new(self, authenticator_id)
VirtualAuthenticator.new(self, authenticator_id, options)
end

def remove_virtual_authenticator(authenticator)
execute :remove_virtual_authenticator, {}, {authenticatorId: authenticator.id}
def remove_virtual_authenticator(id)
execute :remove_virtual_authenticator, {authenticatorId: id}
end

def add_credential(credential)
execute :add_credential, {}, credential
def add_credential(credential, id)
execute :add_credential, {authenticatorId: id}, credential
end

def credentials(authenticator_id)
execute :get_credentials, {}, {authenticatorId: authenticator_id}
end

def remove_credential(credential_id, authenticator_id)
execute :remove_credential, {}, {credentialId: credential_id, authenticatorId: authenticator_id}
execute :remove_credential, {credentialId: credential_id, authenticatorId: authenticator_id}
end

def remove_all_credentials(authenticator_id)
execute :remove_all_credentials, {}, {authenticatorId: authenticator_id}
execute :remove_all_credentials, {authenticatorId: authenticator_id}
end

def user_verified(verified, authenticator_id)
execute :set_user_verified, {}, {authenticatorId: authenticator_id, isUserVerified: verified}
execute :set_user_verified, {authenticatorId: authenticator_id}, {isUserVerified: verified}
end

private
Expand Down
3 changes: 2 additions & 1 deletion rb/lib/selenium/webdriver/remote/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ class Bridge
remove_virtual_authenticator: [:delete, 'session/:session_id/webauthn/authenticator/:authenticatorId'],
add_credential: [:post, 'session/:session_id/webauthn/authenticator/:authenticatorId/credential'],
get_credentials: [:get, 'session/:session_id/webauthn/authenticator/:authenticatorId/credentials'],
remove_credential: [:delete, 'session/:session_id/webauthn/authenticator/:authenticatorId/credentials/:credentialId'],
remove_credential: [:delete,
'session/:session_id/webauthn/authenticator/:authenticatorId/credentials/:credentialId'],
remove_all_credentials: [:delete, 'session/:session_id/webauthn/authenticator/:authenticatorId/credentials'],
set_user_verified: [:post, 'session/:session_id/webauthn/authenticator/:authenticatorId/uv']

Expand Down
Loading

0 comments on commit 13975e0

Please sign in to comment.