Skip to content

Commit

Permalink
ruby 2.2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
neurosnap committed Jan 20, 2022
1 parent 90f7d29 commit d064357
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 15 deletions.
18 changes: 11 additions & 7 deletions lib/webauthn/authenticator_assertion_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,23 @@ def initialize(credential_id:, authenticator_data:, signature:, **options)
def valid?(original_challenge, original_origin, allowed_credentials:, rp_id: nil)
super(original_challenge, original_origin, rp_id: rp_id) &&
valid_credential?(allowed_credentials) &&
valid_signature?(credential_public_key(allowed_credentials))
valid_signature_with_credentials?(allowed_credentials)
end

def authenticator_data
@authenticator_data ||= WebAuthn::AuthenticatorData.new(authenticator_data_bytes)
end

def valid_signature_with_credentials?(allows_credentials)
valid_signature?(credential_public_key(allows_credentials))
end

def valid_credential?(allowed_credentials)
allowed_credential_ids = allowed_credentials.map { |credential| credential[:id] }

allowed_credential_ids.include?(credential_id)
end

private

attr_reader :credential_id, :authenticator_data_bytes, :signature
Expand All @@ -40,12 +50,6 @@ def valid_signature?(public_key_bytes)
)
end

def valid_credential?(allowed_credentials)
allowed_credential_ids = allowed_credentials.map { |credential| credential[:id] }

allowed_credential_ids.include?(credential_id)
end

def credential_public_key(allowed_credentials)
matched_credential = allowed_credentials.find do |credential|
credential[:id] == credential_id
Expand Down
4 changes: 2 additions & 2 deletions lib/webauthn/authenticator_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def credential
end

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

def attested_credential_data
Expand All @@ -67,7 +67,7 @@ def attested_credential_data
end

def flags
@flags ||= data_at(flags_position, FLAGS_LENGTH).unpack1("b*")
@flags ||= data_at(flags_position, FLAGS_LENGTH).unpack("b*")[0]
end

private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def id_position
end

def id_length
@id_length ||= data_at(id_length_position, ID_LENGTH_LENGTH).unpack1(UINT16_BIG_ENDIAN_FORMAT)
@id_length ||= data_at(id_length_position, ID_LENGTH_LENGTH).unpack(UINT16_BIG_ENDIAN_FORMAT)[0]
end

def id_length_position
Expand Down
8 changes: 4 additions & 4 deletions lib/webauthn/authenticator_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ def client_data
@client_data ||= WebAuthn::ClientData.new(client_data_json)
end

private

attr_reader :client_data_json

def valid_type?
client_data.type == type
end
Expand All @@ -39,6 +35,10 @@ def valid_rp_id?(rp_id)
OpenSSL::Digest::SHA256.digest(rp_id) == authenticator_data.rp_id_hash
end

private

attr_reader :client_data_json

def rp_id_from_origin(original_origin)
URI.parse(original_origin).host
end
Expand Down
3 changes: 2 additions & 1 deletion lib/webauthn/utils.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# frozen_string_literal: true
require 'base64url'

module WebAuthn
module Utils
def self.authenticator_decode(str)
Base64.urlsafe_decode64(str)
Base64URL.decode(str)
end
end
end
1 change: 1 addition & 0 deletions webauthn.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Gem::Specification.new do |spec|

spec.add_dependency "cbor", "~> 0.5.9.2"
spec.add_dependency "cose", "~> 0.1.0"
spec.add_dependency "base64url", "~> 1.0.1"

spec.add_development_dependency "bundler", "~> 1.16"
spec.add_development_dependency "byebug", "~> 10.0"
Expand Down

0 comments on commit d064357

Please sign in to comment.