Class: Capybara::Simulated::WebauthnState
- Inherits:
-
Object
- Object
- Capybara::Simulated::WebauthnState
- Defined in:
- lib/capybara/simulated/webauthn_state.rb
Overview
Per-Browser virtual WebAuthn authenticator state. Mirrors the subset of the WebAuthn Level 2 spec real apps (Discourse’s security key + passkey flows) exercise: ES256 keys, fmt=“none” attestation, AAGUID per authenticator, excludeCredentials + userVerification + resident-key enforcement, and the CDP ‘WebAuthn.*` surface that tests drive through `cdp.with_virtual_authenticator`.
Defined Under Namespace
Classes: CborBytes, Credential, Error
Constant Summary collapse
- ES256_ALG =
-7
- P256_CRV =
1- KTY_EC2 =
2- FLAG_UP =
Flags byte in authenticator data (rfc8152 / WebAuthn level 2).
0x01- FLAG_UV =
User Present
0x04- FLAG_BE =
User Verified
0x08- FLAG_BS =
Backup Eligible
0x10- FLAG_AT =
Backup State
0x40- FLAG_ED =
Attested credential data included
0x80- DEFAULT_AAGUID =
Extension data included
("\x00" * 16).b.freeze
Instance Method Summary collapse
- #add_credential(handle, credential) ⇒ Object
- #add_virtual_authenticator(options) ⇒ Object
- #create(json) ⇒ Object
- #get(json) ⇒ Object
- #get_credentials(handle) ⇒ Object
-
#initialize ⇒ WebauthnState
constructor
A new instance of WebauthnState.
- #remove_credential(handle, credential_id_b64) ⇒ Object
- #remove_virtual_authenticator(handle) ⇒ Object
- #set_user_verified(handle, verified) ⇒ Object
Constructor Details
#initialize ⇒ WebauthnState
Returns a new instance of WebauthnState.
46 47 48 49 |
# File 'lib/capybara/simulated/webauthn_state.rb', line 46 def initialize @authenticators = {} @next_handle = 1 end |
Instance Method Details
#add_credential(handle, credential) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/capybara/simulated/webauthn_state.rb', line 67 def add_credential(handle, credential) auth = @authenticators[handle.to_s] or return raw_id = Base64.urlsafe_decode64(credential['credentialId'].to_s) priv = OpenSSL::PKey::EC.new(Base64.urlsafe_decode64(credential['privateKey'].to_s)) auth[:credentials][raw_id] = Credential.new( raw_id: raw_id, private_key: priv, rp_id: credential['rpId'].to_s, sign_count: credential['signCount'].to_i, resident: !!credential['isResidentCredential'], user_handle: credential['userHandle'] ? Base64.urlsafe_decode64(credential['userHandle'].to_s) : nil ) raw_id end |
#add_virtual_authenticator(options) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/capybara/simulated/webauthn_state.rb', line 51 def add_virtual_authenticator() opts = ( || {}).transform_keys(&:to_s) handle = "csim-auth-#{@next_handle}" @next_handle += 1 @authenticators[handle] = { options: opts, credentials: {}, aaguid: DEFAULT_AAGUID } handle end |
#create(json) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/capybara/simulated/webauthn_state.rb', line 105 def create(json) req = JSON.parse(json.to_s) auth = pick_authenticator_for_create(req) or raise Error.new('NotAllowedError', 'no compatible virtual authenticator') rp_id = req.dig('rp', 'id').to_s rp_id = host_from_origin(req['origin']) if rp_id.empty? challenge = Base64.urlsafe_decode64(req['challenge'].to_s) user_id = Base64.urlsafe_decode64(req.dig('user', 'id').to_s) opts = auth[:options] unless (req['pubKeyCredParams'] || []).any? {|p| p['alg'].to_i == ES256_ALG } raise Error.new('NotSupportedError', 'no supported pubKeyCredParam (ES256 only)') end (req['excludeCredentials'] || []).each do |c| if auth[:credentials][Base64.urlsafe_decode64(c['id'].to_s)] raise Error.new('InvalidStateError', 'credential already registered on this authenticator') end end sel = req['authenticatorSelection'] || {} uv_req = sel['userVerification'] require_resident = %w[required preferred].include?(sel['residentKey']) || sel['requireResidentKey'] if require_resident && !opts['hasResidentKey'] raise Error.new('ConstraintError', 'resident-key required but authenticator does not support it') end if uv_req == 'required' && !user_verified?(opts) raise Error.new('NotAllowedError', 'user verification required but not performed') end key = OpenSSL::PKey::EC.generate('prime256v1') raw_id = SecureRandom.random_bytes(32) is_resident = !!(opts['hasResidentKey'] && require_resident) flags = FLAG_UP | FLAG_AT flags |= FLAG_UV if user_verified?(opts) # BE/BS mark the credential as syncable — clients use this to # decide whether to surface "passkey" UX vs "this device only". flags |= (FLAG_BE | FLAG_BS) if opts['hasResidentKey'] && opts['hasUserVerification'] auth_data = OpenSSL::Digest::SHA256.digest(rp_id) + [flags].pack('C') + [0].pack('N') + auth[:aaguid] + [raw_id.bytesize].pack('n') + raw_id + cose_ec2_pubkey(key) attestation_object = cbor_encode( 'fmt' => 'none', 'attStmt' => {}, 'authData' => CborBytes.new(auth_data) ) client_data = JSON.dump( type: 'webauthn.create', challenge: Base64.urlsafe_encode64(challenge.b, padding: false), origin: req['origin'].to_s, crossOrigin: false ) auth[:credentials][raw_id] = Credential.new( raw_id: raw_id, private_key: key, rp_id: rp_id, sign_count: 0, resident: is_resident || !!opts['hasResidentKey'], user_handle: user_id ) { 'credentialId' => Base64.urlsafe_encode64(raw_id.b, padding: false), 'clientDataJSON' => Base64.urlsafe_encode64(client_data.b, padding: false), 'attestationObject' => Base64.urlsafe_encode64(attestation_object.b, padding: false) } end |
#get(json) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/capybara/simulated/webauthn_state.rb', line 183 def get(json) req = JSON.parse(json.to_s) rp_id = req['rpId'].to_s rp_id = host_from_origin(req['origin']) if rp_id.empty? challenge = Base64.urlsafe_decode64(req['challenge'].to_s) allow = (req['allowCredentials'] || []).map {|c| Base64.urlsafe_decode64(c['id'].to_s) } uv_req = req['userVerification'] pick = pick_credential_for_get(rp_id, allow) raise Error.new('NotAllowedError', 'no matching credential') unless pick auth, cred = pick opts = auth[:options] if uv_req == 'required' && !user_verified?(opts) raise Error.new('NotAllowedError', 'user verification required but not performed') end cred.sign_count += 1 flags = FLAG_UP flags |= FLAG_UV if user_verified?(opts) flags |= (FLAG_BE | FLAG_BS) if cred.resident && opts['hasUserVerification'] auth_data = OpenSSL::Digest::SHA256.digest(rp_id) + [flags].pack('C') + [cred.sign_count].pack('N') client_data = JSON.dump( type: 'webauthn.get', challenge: Base64.urlsafe_encode64(challenge.b, padding: false), origin: req['origin'].to_s, crossOrigin: false ) signed_payload = auth_data + OpenSSL::Digest::SHA256.digest(client_data) signature = cred.private_key.sign(OpenSSL::Digest::SHA256.new, signed_payload) # WebAuthn level 2: userHandle is only returned for resident # (discoverable) credentials. Real authenticators don't surface # the bound user for plain server-side credentials. user_handle_out = cred.resident && cred.user_handle && !cred.user_handle.empty? ? Base64.urlsafe_encode64(cred.user_handle.b, padding: false) : nil { 'credentialId' => Base64.urlsafe_encode64(cred.raw_id.b, padding: false), 'clientDataJSON' => Base64.urlsafe_encode64(client_data.b, padding: false), 'authenticatorData' => Base64.urlsafe_encode64(auth_data.b, padding: false), 'signature' => Base64.urlsafe_encode64(signature.b, padding: false), 'userHandle' => user_handle_out } end |
#get_credentials(handle) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/capybara/simulated/webauthn_state.rb', line 87 def get_credentials(handle) auth = @authenticators[handle.to_s] or return [] auth[:credentials].values.map {|c| { 'credentialId' => Base64.urlsafe_encode64(c.raw_id.b, padding: false), 'rpId' => c.rp_id, 'isResidentCredential' => c.resident, 'signCount' => c.sign_count, 'userHandle' => c.user_handle ? Base64.urlsafe_encode64(c.user_handle.b, padding: false) : nil } } end |
#remove_credential(handle, credential_id_b64) ⇒ Object
82 83 84 85 |
# File 'lib/capybara/simulated/webauthn_state.rb', line 82 def remove_credential(handle, credential_id_b64) auth = @authenticators[handle.to_s] or return auth[:credentials].delete(Base64.urlsafe_decode64(credential_id_b64.to_s)) end |
#remove_virtual_authenticator(handle) ⇒ Object
63 64 65 |
# File 'lib/capybara/simulated/webauthn_state.rb', line 63 def remove_virtual_authenticator(handle) @authenticators.delete(handle.to_s) end |
#set_user_verified(handle, verified) ⇒ Object
100 101 102 103 |
# File 'lib/capybara/simulated/webauthn_state.rb', line 100 def set_user_verified(handle, verified) auth = @authenticators[handle.to_s] or return auth[:options]['isUserVerified'] = !!verified end |