Path: blob/trunk/rb/lib/selenium/webdriver/common/virtual_authenticator/credential.rb
1990 views
# frozen_string_literal: true12# Licensed to the Software Freedom Conservancy (SFC) under one3# or more contributor license agreements. See the NOTICE file4# distributed with this work for additional information5# regarding copyright ownership. The SFC licenses this file6# to you under the Apache License, Version 2.0 (the7# "License"); you may not use this file except in compliance8# with the License. You may obtain a copy of the License at9#10# http://www.apache.org/licenses/LICENSE-2.011#12# Unless required by applicable law or agreed to in writing,13# software distributed under the License is distributed on an14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15# KIND, either express or implied. See the License for the16# specific language governing permissions and limitations17# under the License.1819#20# A credential stored in a virtual authenticator.21# @see https://w3c.github.io/webauthn/#credential-parameters22#2324module Selenium25module WebDriver26class Credential27class << self28def resident(**)29Credential.new(resident_credential: true, **)30end3132def non_resident(**)33Credential.new(resident_credential: false, **)34end3536def encode(byte_array)37Base64.urlsafe_encode64(byte_array&.pack('C*'))38end3940def decode(base64)41Base64.urlsafe_decode64(base64).unpack('C*')42end4344def from_json(opts)45user_handle = opts['userHandle'] ? decode(opts['userHandle']) : nil46new(id: decode(opts['credentialId']),47resident_credential: opts['isResidentCredential'],48rp_id: opts['rpId'],49private_key: opts['privateKey'],50sign_count: opts['signCount'],51user_handle: user_handle)52end53end5455attr_reader :id, :resident_credential, :rp_id, :user_handle, :private_key, :sign_count56alias resident_credential? resident_credential5758def initialize(id:, resident_credential:, rp_id:, private_key:, **opts)59@id = id60@resident_credential = resident_credential61@rp_id = rp_id62@user_handle = opts.delete(:user_handle) { nil }63@private_key = private_key64@sign_count = opts.delete(:sign_count) { 0 }6566raise ArgumentError, "Invalid arguments: #{opts.keys}" unless opts.empty?67end6869#70# @api private71#7273def as_json(*)74credential_data = {'credentialId' => Credential.encode(id),75'isResidentCredential' => resident_credential?,76'rpId' => rp_id,77'privateKey' => Credential.encode(private_key),78'signCount' => sign_count}79credential_data['userHandle'] = Credential.encode(user_handle) if user_handle80credential_data81end82end # Credential83end # WebDriver84end # Selenium858687