Skip to content

Commit

Permalink
Merge branch 'master' into handle_mime_types
Browse files Browse the repository at this point in the history
  • Loading branch information
alxberardi committed Dec 13, 2016
2 parents 40b38bb + ce91098 commit 7da43e2
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 35 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This changelog adheres to [Keep a CHANGELOG](http://keepachangelog.com/).
- Native JSON parameter parsing
- Allows defining parameter parsers
- Scans and masks parameter keys
- Adds credit card brand validation

### Changed
- Occurrence now exposes query and body params separately
Expand Down
2 changes: 2 additions & 0 deletions gemfiles/Gemfile.ruby-2.1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

# ruby-2.1 compatible gems
gem 'rack', '~> 1.4'
gem 'activemodel', '>= 3', '< 5'
gem 'activesupport', '>= 3', '< 5'

# Specify your gem's dependencies in sensitive_data_filter.gemspec
gemspec path: '../'
2 changes: 2 additions & 0 deletions gemfiles/Gemfile.ruby-2.2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

# ruby-2.2 compatible gems
gem 'rack', '~> 1.4'
gem 'activemodel', '>= 3', '< 5'
gem 'activesupport', '>= 3', '< 5'

# Specify your gem's dependencies in sensitive_data_filter.gemspec
gemspec path: '../'
38 changes: 3 additions & 35 deletions lib/sensitive_data_filter/types/credit_card.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# frozen_string_literal: true
require 'credit_card_validations'

module SensitiveDataFilter
module Types
module CreditCard
Expand All @@ -21,7 +23,7 @@ module CreditCard
module_function def valid?(number)
return false unless number.is_a? String
return false unless number.match CARD
Luhn.new(number.gsub(SEPARATORS, '')).valid?
CreditCardValidations::Detector.new(number.gsub(SEPARATORS, '')).brand.present?
end

module_function def scan(value)
Expand All @@ -33,40 +35,6 @@ module CreditCard
return value unless value.is_a? String
scan(value).inject(value) { |acc, elem| acc.gsub(elem, FILTERED) }
end

# Adapted from https://github.com/rolfb/luhn-ruby/blob/master/lib/luhn.rb
class Luhn
def initialize(number)
@number = number
end

def valid?
numbers = split_digits(@number)
numbers.last == checksum(numbers[0..-2].join)
end

private

def checksum(number)
products = luhn_doubled(number)
sum = products.inject(0) { |acc, elem| acc + sum_of(elem) }
checksum = 10 - (sum % 10)
checksum == 10 ? 0 : checksum
end

def luhn_doubled(number)
numbers = split_digits(number).reverse
numbers.map.with_index { |n, i| i.even? ? n * 2 : n * 1 }.reverse
end

def sum_of(number)
split_digits(number).inject(:+)
end

def split_digits(number)
number.to_s.split(//).map(&:to_i)
end
end
end
end
end
1 change: 1 addition & 0 deletions sensitive_data_filter.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Gem::Specification.new do |spec|

spec.add_dependency 'rack', '>= 1.4'
spec.add_dependency 'facets', '~> 3.1'
spec.add_dependency 'credit_card_validations', '~> 3.2'

spec.add_development_dependency 'bundler', '~> 1.13'
spec.add_development_dependency 'rake', '~> 10.0'
Expand Down
6 changes: 6 additions & 0 deletions spec/sensitive_data/types/credit_card_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,11 @@ def validations(cards)
specify { expect(scan).to be_empty }
specify { expect(mask).to eq value }
end

context 'a value that is a luhn but not a credit card' do
let(:value) { '1234-5678-9012-3528' }
specify { expect(scan).to be_empty }
specify { expect(mask).to eq value }
end
end
end

0 comments on commit 7da43e2

Please sign in to comment.