Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/lib/selenium/webdriver/common/virtual_authenticator/credential.rb
1990 views
1
# frozen_string_literal: true
2
3
# Licensed to the Software Freedom Conservancy (SFC) under one
4
# or more contributor license agreements. See the NOTICE file
5
# distributed with this work for additional information
6
# regarding copyright ownership. The SFC licenses this file
7
# to you under the Apache License, Version 2.0 (the
8
# "License"); you may not use this file except in compliance
9
# with the License. You may obtain a copy of the License at
10
#
11
# http://www.apache.org/licenses/LICENSE-2.0
12
#
13
# Unless required by applicable law or agreed to in writing,
14
# software distributed under the License is distributed on an
15
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
# KIND, either express or implied. See the License for the
17
# specific language governing permissions and limitations
18
# under the License.
19
20
#
21
# A credential stored in a virtual authenticator.
22
# @see https://w3c.github.io/webauthn/#credential-parameters
23
#
24
25
module Selenium
26
module WebDriver
27
class Credential
28
class << self
29
def resident(**)
30
Credential.new(resident_credential: true, **)
31
end
32
33
def non_resident(**)
34
Credential.new(resident_credential: false, **)
35
end
36
37
def encode(byte_array)
38
Base64.urlsafe_encode64(byte_array&.pack('C*'))
39
end
40
41
def decode(base64)
42
Base64.urlsafe_decode64(base64).unpack('C*')
43
end
44
45
def from_json(opts)
46
user_handle = opts['userHandle'] ? decode(opts['userHandle']) : nil
47
new(id: decode(opts['credentialId']),
48
resident_credential: opts['isResidentCredential'],
49
rp_id: opts['rpId'],
50
private_key: opts['privateKey'],
51
sign_count: opts['signCount'],
52
user_handle: user_handle)
53
end
54
end
55
56
attr_reader :id, :resident_credential, :rp_id, :user_handle, :private_key, :sign_count
57
alias resident_credential? resident_credential
58
59
def initialize(id:, resident_credential:, rp_id:, private_key:, **opts)
60
@id = id
61
@resident_credential = resident_credential
62
@rp_id = rp_id
63
@user_handle = opts.delete(:user_handle) { nil }
64
@private_key = private_key
65
@sign_count = opts.delete(:sign_count) { 0 }
66
67
raise ArgumentError, "Invalid arguments: #{opts.keys}" unless opts.empty?
68
end
69
70
#
71
# @api private
72
#
73
74
def as_json(*)
75
credential_data = {'credentialId' => Credential.encode(id),
76
'isResidentCredential' => resident_credential?,
77
'rpId' => rp_id,
78
'privateKey' => Credential.encode(private_key),
79
'signCount' => sign_count}
80
credential_data['userHandle'] = Credential.encode(user_handle) if user_handle
81
credential_data
82
end
83
end # Credential
84
end # WebDriver
85
end # Selenium
86
87