rails_validations_hmac 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,15 +7,24 @@ module ActiveModel
7
7
  DEFAULT_ALGORITHM = :sha1
8
8
 
9
9
  def validate_each(record, attribute, hmac)
10
- key = value_in_context(record, options[:key]) or raise ":key missing"
11
- data = value_in_context(record, options[:data]) or raise ":data missing"
12
- algorithm = value_in_context(record, options[:algorithm]) || DEFAULT_ALGORITHM
13
- message = options[:message] or 'HMAC is invalid'
10
+ raise ArgumentError, "Definition :key is missing" unless options.has_key?(:key)
11
+ raise ArgumentError, "Definition :data is missing" unless options.has_key?(:data)
14
12
 
15
- real_hmac = OpenSSL::HMAC.hexdigest("#{algorithm}", key, data)
13
+ key = value_in_context(record, options[:key])
14
+ data = value_in_context(record, options[:data])
15
+ algorithm = value_in_context(record, options[:algorithm]) || DEFAULT_ALGORITHM
16
+ message = options[:message] or 'HMAC is not matching'
16
17
 
17
- valid = real_hmac == hmac
18
- record.errors.add(attribute, message) unless valid
18
+ if key and data and algorithm
19
+ real_hmac = OpenSSL::HMAC.hexdigest("#{algorithm}", key, data)
20
+ valid = real_hmac == hmac
21
+ record.errors.add(attribute, message) unless valid
22
+ else
23
+ record.errors.add(:hmac, 'Comparing key not found') unless key
24
+ record.errors.add(:hmac, 'Comparing data not found') unless data
25
+ record.errors.add(:hmac, 'Comparing algorithm not found') unless algorithm
26
+ valid = false
27
+ end
19
28
  end
20
29
 
21
30
  private
@@ -23,9 +32,9 @@ module ActiveModel
23
32
  def value_in_context(record, evaluation)
24
33
  case evaluation
25
34
  when Symbol
26
- "#{evaluation}".split('.').inject(record) {|object, method| object.send(method) }
35
+ "#{evaluation}".split('.').inject(record) {|object, method| object.send(method) } rescue nil
27
36
  when Proc
28
- record.instance_exec(&evaluation)
37
+ record.instance_exec(&evaluation) rescue nil
29
38
  when Array
30
39
  evaluation.collect {|e| value_in_context(record, e) }.join
31
40
  else
@@ -1134,3 +1134,54 @@ Connecting to database specified by database.yml
1134
1134
   (0.1ms) rollback transaction
1135
1135
   (0.1ms) begin transaction
1136
1136
   (0.1ms) rollback transaction
1137
+ Connecting to database specified by database.yml
1138
+  (8.3ms) begin transaction
1139
+  (0.1ms) rollback transaction
1140
+  (0.1ms) begin transaction
1141
+  (0.1ms) rollback transaction
1142
+  (0.1ms) begin transaction
1143
+  (0.1ms) rollback transaction
1144
+  (0.1ms) begin transaction
1145
+  (0.1ms) rollback transaction
1146
+  (0.1ms) begin transaction
1147
+  (0.1ms) rollback transaction
1148
+  (0.1ms) begin transaction
1149
+  (0.1ms) rollback transaction
1150
+  (0.1ms) begin transaction
1151
+  (0.1ms) rollback transaction
1152
+  (0.1ms) begin transaction
1153
+  (0.1ms) rollback transaction
1154
+ Connecting to database specified by database.yml
1155
+  (0.8ms) begin transaction
1156
+  (0.1ms) rollback transaction
1157
+  (0.1ms) begin transaction
1158
+  (0.1ms) rollback transaction
1159
+  (0.1ms) begin transaction
1160
+  (0.1ms) rollback transaction
1161
+  (0.1ms) begin transaction
1162
+  (0.1ms) rollback transaction
1163
+  (0.1ms) begin transaction
1164
+  (0.1ms) rollback transaction
1165
+  (0.1ms) begin transaction
1166
+  (0.1ms) rollback transaction
1167
+  (0.1ms) begin transaction
1168
+  (0.1ms) rollback transaction
1169
+  (0.1ms) begin transaction
1170
+  (0.1ms) rollback transaction
1171
+ Connecting to database specified by database.yml
1172
+  (0.8ms) begin transaction
1173
+  (0.1ms) rollback transaction
1174
+  (0.1ms) begin transaction
1175
+  (0.1ms) rollback transaction
1176
+  (0.1ms) begin transaction
1177
+  (0.1ms) rollback transaction
1178
+  (0.1ms) begin transaction
1179
+  (0.1ms) rollback transaction
1180
+  (0.1ms) begin transaction
1181
+  (0.1ms) rollback transaction
1182
+  (0.1ms) begin transaction
1183
+  (0.1ms) rollback transaction
1184
+  (0.1ms) begin transaction
1185
+  (0.1ms) rollback transaction
1186
+  (0.1ms) begin transaction
1187
+  (0.1ms) rollback transaction
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_validations_hmac
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -212,7 +212,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
212
212
  version: '0'
213
213
  segments:
214
214
  - 0
215
- hash: 1495700232791925941
215
+ hash: -877944555676697387
216
216
  requirements: []
217
217
  rubyforge_project:
218
218
  rubygems_version: 1.8.24