Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/webauthn/authenticator_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class AuthenticatorData
FLAGS_LENGTH = 1
SIGN_COUNT_LENGTH = 4

SIGN_COUNT_POSITION = RP_ID_HASH_LENGTH + FLAGS_LENGTH

USER_PRESENT_FLAG_POSITION = 0
ATTESTED_CREDENTIAL_DATA_INCLUDED_FLAG_POSITION = 6

Expand Down Expand Up @@ -46,6 +48,10 @@ def credential
attested_credential_data.credential
end

def sign_count
@sign_count ||= data_at(SIGN_COUNT_POSITION, SIGN_COUNT_LENGTH).unpack1('L>')
end

def attested_credential_data
@attested_credential_data ||=
AttestedCredentialData.new(data_at(attested_credential_data_position))
Expand Down
13 changes: 7 additions & 6 deletions spec/support/fake_authenticator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

class FakeAuthenticator
class Base
def initialize(challenge: fake_challenge, rp_id: "localhost", context: {})
def initialize(challenge: fake_challenge, rp_id: "localhost", sign_count: 0, context: {})
@challenge = challenge
@rp_id = rp_id
@sign_count = sign_count
@context = context
end

Expand All @@ -24,14 +25,14 @@ def credential_id
@credential_id ||= SecureRandom.random_bytes(16)
end

private

attr_reader :challenge, :context, :rp_id

def rp_id_hash
OpenSSL::Digest::SHA256.digest(rp_id)
end

private

attr_reader :challenge, :context, :rp_id

def raw_flags
["#{user_present_bit}00000#{attested_credential_data_present_bit}0"].pack("b*")
end
Expand All @@ -49,7 +50,7 @@ def attested_credential_data
end

def raw_sign_count
"0000"
[@sign_count].pack('L>')
end

def user_present_bit
Expand Down
36 changes: 36 additions & 0 deletions spec/webauthn/authenticator_data_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

RSpec.describe WebAuthn::AuthenticatorData do
let(:authenticator) do
FakeAuthenticator::Base.new(rp_id: rp_id, sign_count: sign_count, context: { user_present: user_presence })
end

let(:rp_id) { "localhost" }
let(:sign_count) { 42 }
let(:user_presence) { true }

let(:authenticator_data) { described_class.new(authenticator.authenticator_data) }

describe "#rp_id_hash" do
subject { authenticator_data.rp_id_hash }
it { is_expected.to eq(authenticator.rp_id_hash) }
end

describe "#sign_count" do
subject { authenticator_data.sign_count }
it { is_expected.to eq(42) }
end

describe "#user_present?" do
subject { authenticator_data.user_present? }
context "when UP flag is set" do
let(:user_presence) { true }
it { is_expected.to be_truthy }
end

context "when UP flag is not set" do
let(:user_presence) { false }
it { is_expected.to be_falsy }
end
end
end