|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "spec_helper" |
| 4 | + |
| 5 | +require "base64" |
| 6 | +require "securerandom" |
| 7 | +require "webauthn/authenticator_assertion_response" |
| 8 | +require "webauthn/configuration" |
| 9 | +require "webauthn/public_key_credential_with_assertion" |
| 10 | + |
| 11 | +RSpec.describe "PublicKeyCredentialWithAssertion" do |
| 12 | + describe "#verify" do |
| 13 | + let(:client) { WebAuthn::FakeClient.new(origin, encoding: false) } |
| 14 | + let(:challenge) { Base64.urlsafe_encode64(raw_challenge) } |
| 15 | + let(:raw_challenge) { fake_challenge } |
| 16 | + let(:origin) { fake_origin } |
| 17 | + |
| 18 | + let!(:credential) { create_credential(client: client) } |
| 19 | + let(:credential_raw_id) { credential[0] } |
| 20 | + let(:credential_id) { Base64.urlsafe_encode64(credential_raw_id) } |
| 21 | + let(:credential_type) { "public-key" } |
| 22 | + let(:credential_public_key) { Base64.urlsafe_encode64(credential[1]) } |
| 23 | + let(:credential_sign_count) { credential[2] } |
| 24 | + |
| 25 | + let(:assertion_response) do |
| 26 | + response = client.get(challenge: raw_challenge, sign_count: 1)["response"] |
| 27 | + |
| 28 | + WebAuthn::AuthenticatorAssertionResponse.new( |
| 29 | + authenticator_data: response["authenticatorData"], |
| 30 | + client_data_json: response["clientDataJSON"], |
| 31 | + signature: response["signature"] |
| 32 | + ) |
| 33 | + end |
| 34 | + |
| 35 | + let(:public_key_credential) do |
| 36 | + WebAuthn::PublicKeyCredentialWithAssertion.new( |
| 37 | + type: credential_type, |
| 38 | + id: credential_id, |
| 39 | + raw_id: credential_raw_id, |
| 40 | + response: assertion_response |
| 41 | + ) |
| 42 | + end |
| 43 | + |
| 44 | + before do |
| 45 | + WebAuthn.configuration.origin = origin |
| 46 | + end |
| 47 | + |
| 48 | + it "works" do |
| 49 | + expect( |
| 50 | + public_key_credential.verify(challenge, public_key: credential_public_key, sign_count: credential_sign_count) |
| 51 | + ).to be_truthy |
| 52 | + |
| 53 | + expect(public_key_credential.id).not_to be_empty |
| 54 | + expect(public_key_credential.user_handle).to be_nil |
| 55 | + expect(public_key_credential.sign_count).to eq(credential_sign_count + 1) |
| 56 | + end |
| 57 | + |
| 58 | + context "when type is invalid" do |
| 59 | + context "because it is missing" do |
| 60 | + let(:credential_type) { nil } |
| 61 | + |
| 62 | + it "fails" do |
| 63 | + expect do |
| 64 | + public_key_credential.verify( |
| 65 | + challenge, |
| 66 | + public_key: credential_public_key, |
| 67 | + sign_count: credential_sign_count |
| 68 | + ) |
| 69 | + end.to raise_error(RuntimeError) |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + context "because it is something else" do |
| 74 | + let(:credential_type) { "password" } |
| 75 | + |
| 76 | + it "fails" do |
| 77 | + expect do |
| 78 | + public_key_credential.verify( |
| 79 | + challenge, |
| 80 | + public_key: credential_public_key, |
| 81 | + sign_count: credential_sign_count |
| 82 | + ) |
| 83 | + end.to raise_error(RuntimeError) |
| 84 | + end |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + context "when id is invalid" do |
| 89 | + context "because it is missing" do |
| 90 | + let(:credential_id) { nil } |
| 91 | + |
| 92 | + it "fails" do |
| 93 | + expect do |
| 94 | + public_key_credential.verify( |
| 95 | + challenge, |
| 96 | + public_key: credential_public_key, |
| 97 | + sign_count: credential_sign_count |
| 98 | + ) |
| 99 | + end.to raise_error(RuntimeError) |
| 100 | + end |
| 101 | + end |
| 102 | + |
| 103 | + context "because it is not the base64url of raw id" do |
| 104 | + let(:credential_id) { Base64.urlsafe_encode64(credential_raw_id + "a") } |
| 105 | + |
| 106 | + it "fails" do |
| 107 | + expect do |
| 108 | + public_key_credential.verify( |
| 109 | + challenge, |
| 110 | + public_key: credential_public_key, |
| 111 | + sign_count: credential_sign_count |
| 112 | + ) |
| 113 | + end.to raise_error(RuntimeError) |
| 114 | + end |
| 115 | + end |
| 116 | + end |
| 117 | + |
| 118 | + context "when challenge is invalid" do |
| 119 | + let(:challenge) { Base64.urlsafe_encode64("another challenge") } |
| 120 | + |
| 121 | + it "fails" do |
| 122 | + expect do |
| 123 | + public_key_credential.verify( |
| 124 | + challenge, |
| 125 | + public_key: credential_public_key, |
| 126 | + sign_count: credential_sign_count |
| 127 | + ) |
| 128 | + end.to raise_error(WebAuthn::ChallengeVerificationError) |
| 129 | + end |
| 130 | + end |
| 131 | + |
| 132 | + context "when clientExtensionResults" do |
| 133 | + context "is not received" do |
| 134 | + let(:public_key_credential) do |
| 135 | + WebAuthn::PublicKeyCredentialWithAssertion.new( |
| 136 | + type: credential_type, |
| 137 | + id: credential_id, |
| 138 | + raw_id: credential_raw_id, |
| 139 | + client_extension_outputs: nil, |
| 140 | + response: assertion_response |
| 141 | + ) |
| 142 | + end |
| 143 | + |
| 144 | + it "works" do |
| 145 | + expect( |
| 146 | + public_key_credential.verify( |
| 147 | + challenge, |
| 148 | + public_key: credential_public_key, |
| 149 | + sign_count: credential_sign_count |
| 150 | + ) |
| 151 | + ).to be_truthy |
| 152 | + |
| 153 | + expect(public_key_credential.client_extension_outputs).to be_nil |
| 154 | + end |
| 155 | + end |
| 156 | + |
| 157 | + context "is received" do |
| 158 | + let(:public_key_credential) do |
| 159 | + WebAuthn::PublicKeyCredentialWithAssertion.new( |
| 160 | + type: credential_type, |
| 161 | + id: credential_id, |
| 162 | + raw_id: credential_raw_id, |
| 163 | + client_extension_outputs: { "txAuthSimple" => "Could you please verify yourself?" }, |
| 164 | + response: assertion_response |
| 165 | + ) |
| 166 | + end |
| 167 | + |
| 168 | + it "works" do |
| 169 | + expect( |
| 170 | + public_key_credential.verify( |
| 171 | + challenge, |
| 172 | + public_key: credential_public_key, |
| 173 | + sign_count: credential_sign_count |
| 174 | + ) |
| 175 | + ).to be_truthy |
| 176 | + |
| 177 | + expect(public_key_credential.client_extension_outputs) |
| 178 | + .to eq({ "txAuthSimple" => "Could you please verify yourself?" }) |
| 179 | + end |
| 180 | + end |
| 181 | + end |
| 182 | + |
| 183 | + context "when authentication extension input" do |
| 184 | + context "is not received" do |
| 185 | + let(:assertion_response) do |
| 186 | + response = client.get(challenge: raw_challenge, extensions: nil)["response"] |
| 187 | + |
| 188 | + WebAuthn::AuthenticatorAssertionResponse.new( |
| 189 | + authenticator_data: response["authenticatorData"], |
| 190 | + client_data_json: response["clientDataJSON"], |
| 191 | + signature: response["signature"] |
| 192 | + ) |
| 193 | + end |
| 194 | + |
| 195 | + it "works" do |
| 196 | + expect( |
| 197 | + public_key_credential.verify( |
| 198 | + challenge, |
| 199 | + public_key: credential_public_key, |
| 200 | + sign_count: credential_sign_count |
| 201 | + ) |
| 202 | + ).to be_truthy |
| 203 | + |
| 204 | + expect(public_key_credential.authenticator_extension_outputs).to be_nil |
| 205 | + end |
| 206 | + end |
| 207 | + |
| 208 | + context "is received" do |
| 209 | + let(:assertion_response) do |
| 210 | + response = client.get( |
| 211 | + challenge: raw_challenge, |
| 212 | + extensions: { "txAuthSimple" => "Could you please verify yourself?" } |
| 213 | + )["response"] |
| 214 | + |
| 215 | + WebAuthn::AuthenticatorAssertionResponse.new( |
| 216 | + authenticator_data: response["authenticatorData"], |
| 217 | + client_data_json: response["clientDataJSON"], |
| 218 | + signature: response["signature"] |
| 219 | + ) |
| 220 | + end |
| 221 | + |
| 222 | + it "works" do |
| 223 | + expect( |
| 224 | + public_key_credential.verify( |
| 225 | + challenge, |
| 226 | + public_key: credential_public_key, |
| 227 | + sign_count: credential_sign_count |
| 228 | + ) |
| 229 | + ).to be_truthy |
| 230 | + |
| 231 | + expect(public_key_credential.authenticator_extension_outputs) |
| 232 | + .to eq({ "txAuthSimple" => "Could you please verify yourself?" }) |
| 233 | + end |
| 234 | + end |
| 235 | + end |
| 236 | + end |
| 237 | +end |
0 commit comments