9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/rails_validations_hmac/support/active_model/validations/hmac_validator.rb', line 9
def validate_each(record, attribute, hmac)
raise ArgumentError, "Definition :key is missing" unless options.has_key?(:key)
raise ArgumentError, "Definition :data is missing" unless options.has_key?(:data)
key = value_in_context(record, options[:key])
data = value_in_context(record, options[:data])
algorithm = value_in_context(record, options[:algorithm]) || DEFAULT_ALGORITHM
message = options[:message] or 'HMAC is not matching'
if key and data and algorithm
real_hmac = OpenSSL::HMAC.hexdigest("#{algorithm}", key, data)
valid = real_hmac == hmac
record.errors.add(attribute, message) unless valid
else
record.errors.add(:hmac, 'Comparing key not found') unless key
record.errors.add(:hmac, 'Comparing data not found') unless data
record.errors.add(:hmac, 'Comparing algorithm not found') unless algorithm
valid = false
end
end
|