encrypted_form_fields 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d6aea0ee8b40654f443b625193e3e98ee3bdb5f0
4
- data.tar.gz: 551356231ce1ec66184686a3a60a58836b0c27e0
3
+ metadata.gz: 4f69c60e1d96d41b3309dc75dca2c159dfaddbe3
4
+ data.tar.gz: 5fd199062fd322787e7c0160df85237aa34a6ba9
5
5
  SHA512:
6
- metadata.gz: 462c4c84e0d97d4758ceae69511301ef5f8918972989e84a1a7db79fa21e247c0cf99bd3661ff7d3bac9a65a6e0bf2273839299823d8b6c656c3435757cb197c
7
- data.tar.gz: 58a4a166da91bdf453a3a6c05de33ed37db5c7bc0bfe6614b5c148567bf5ae3f6e5714889c338bb0d35bcd499f1b4b3b7c34b739aa8daa6144a6fc97449d2cb7
6
+ metadata.gz: 3083de3950d099bff7df16714ce0cf73a2ce261d395a35b0ddb1a929aa8a8e2ae8495f2149c1319431c03bb3e62143841ac911bdf2f27640896aabb21fba73b7
7
+ data.tar.gz: 421e9369ee6cc02f7cbfe6650844c06610d0f65639fe5fb62305747a117ef4ac1c1e26defcb85f6c51ff181b6461b4ac10c2cc696bf0273ae9446bc8dae03c06
@@ -18,8 +18,8 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_runtime_dependency "actionpack", "~> 4.0.0"
22
- spec.add_runtime_dependency "activesupport", "~> 4.0.0"
21
+ spec.add_runtime_dependency "actionpack", "~> 4.0"
22
+ spec.add_runtime_dependency "activesupport", "~> 4.0"
23
23
  spec.add_development_dependency "rake"
24
24
  spec.add_development_dependency "bundler", "~> 1.3"
25
25
  spec.add_development_dependency "minitest"
@@ -3,6 +3,7 @@ require "action_view"
3
3
  require "active_support/message_encryptor"
4
4
 
5
5
  require "encrypted_form_fields/version"
6
+ require "encrypted_form_fields/dfs"
6
7
  require "encrypted_form_fields/encrypted_parameters"
7
8
  require "encrypted_form_fields/helpers/form_builder"
8
9
  require 'encrypted_form_fields/railtie' if defined?(Rails)
@@ -35,6 +36,18 @@ module EncryptedFormFields
35
36
 
36
37
  delegate :encrypt_and_sign, :decrypt_and_verify, to: :encryptor
37
38
 
39
+ # Decrypt encrypted parameters object
40
+ def decrypt_parameters(params)
41
+ Dfs.traverse(params || {}) do |value|
42
+ EncryptedFormFields.decrypt_and_verify(value)
43
+ end
44
+ end
45
+
46
+ # Encrypt hash values
47
+ def encrypt_parameters(hash = {})
48
+ Dfs.traverse(hash, &method(:encrypt_and_sign))
49
+ end
50
+
38
51
  private
39
52
 
40
53
  def encryptor
@@ -0,0 +1,30 @@
1
+ module EncryptedFormFields
2
+ module Dfs
3
+ extend self
4
+
5
+ def traverse(value, &block)
6
+ if value.is_a?(Hash)
7
+ traverse_hash(value, &block)
8
+ elsif value.is_a?(Array)
9
+ traverse_array(value, &block)
10
+ else
11
+ yield value
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def traverse_hash(hash, &block)
18
+ hash.inject({}.with_indifferent_access) do |result, (key, value)|
19
+ result[key] = traverse(value, &block)
20
+ result
21
+ end
22
+ end
23
+
24
+ def traverse_array(array, &block)
25
+ array.map do |value|
26
+ traverse(value, &block)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -2,30 +2,8 @@ module EncryptedFormFields
2
2
  module EncryptedParameters
3
3
  # Decrypt encrypted parameters
4
4
  def encrypted_params
5
- @encrypted_params ||= decrypt_value(params["_encrypted"] || {})
6
- end
7
-
8
- private
9
-
10
- def decrypt_array(array)
11
- array.map(&method(:decrypt_value))
12
- end
13
-
14
- def decrypt_hash(hash)
15
- hash.inject({}.with_indifferent_access) do |result, (key, value)|
16
- result[key] = decrypt_value(value)
17
- result
18
- end
19
- end
20
-
21
- def decrypt_value(value)
22
- if value.is_a?(Hash)
23
- decrypt_hash(value)
24
- elsif value.is_a?(Array)
25
- decrypt_array(value)
26
- else
27
- EncryptedFormFields.decrypt_and_verify(value)
28
- end
5
+ @encrypted_params ||=
6
+ EncryptedFormFields.decrypt_parameters(params["_encrypted"] || {})
29
7
  end
30
8
  end
31
9
  end
@@ -12,7 +12,7 @@ module EncryptedFormFields
12
12
  # => <input id="email_verified_at" name="_encrypted_email_verified_at" type="hidden" value="[encrypted]" />
13
13
  def encrypted_field_tag(name, value = nil, options = {})
14
14
  encrypted_value = EncryptedFormFields.encrypt_and_sign(value)
15
- prefixed_name = EncryptedFormFields.prefix_name(name)
15
+ prefixed_name = EncryptedFormFields.prefix_name(name.to_s)
16
16
  tag :input, {
17
17
  "type" => "hidden",
18
18
  "name" => prefixed_name,
@@ -1,3 +1,3 @@
1
1
  module EncryptedFormFields
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -0,0 +1,15 @@
1
+ require 'test_helper'
2
+
3
+ class EncryptedFormFieldsTest < MiniTest::Unit::TestCase
4
+ def test_encrypting_parameters
5
+ hash = EncryptedFormFields.encrypt_parameters({"foo" => "bar"})
6
+ decrypted = EncryptedFormFields.decrypt_and_verify(hash["foo"])
7
+ assert_equal("bar", decrypted)
8
+ end
9
+
10
+ def test_decrypting_parameters
11
+ value = EncryptedFormFields.encrypt_and_sign("bar")
12
+ hash = EncryptedFormFields.decrypt_parameters({"foo" => {"0" => value}})
13
+ assert_equal("bar", hash["foo"]["0"])
14
+ end
15
+ end
@@ -12,4 +12,10 @@ class FormTagHelperTest < ActionView::TestCase
12
12
  assert_equal "hidden", tag.attributes["type"]
13
13
  assert_equal "field", tag.attributes["id"]
14
14
  end
15
+
16
+ def test_symbol_form_field_name
17
+ tag = HTML::Document.new(encrypted_field_tag :field, "value").find(tag: "input")
18
+ decrypted_value = EncryptedFormFields.decrypt_and_verify(tag.attributes["value"])
19
+ assert_equal "_encrypted[field]", tag.attributes["name"]
20
+ end
15
21
  end
@@ -1,5 +1,4 @@
1
1
  ENV['RAILS_ENV'] = 'test'
2
- require 'minitest/unit'
3
2
  require 'minitest/autorun'
4
3
  require 'minitest/pride'
5
4
  require 'securerandom'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: encrypted_form_fields
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ville Lautanala
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-09 00:00:00.000000000 Z
11
+ date: 2013-12-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -16,28 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - ~>
18
18
  - !ruby/object:Gem::Version
19
- version: 4.0.0
19
+ version: '4.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ~>
25
25
  - !ruby/object:Gem::Version
26
- version: 4.0.0
26
+ version: '4.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activesupport
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ~>
32
32
  - !ruby/object:Gem::Version
33
- version: 4.0.0
33
+ version: '4.0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ~>
39
39
  - !ruby/object:Gem::Version
40
- version: 4.0.0
40
+ version: '4.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -95,6 +95,7 @@ files:
95
95
  - Rakefile
96
96
  - encrypted_form_fields.gemspec
97
97
  - lib/encrypted_form_fields.rb
98
+ - lib/encrypted_form_fields/dfs.rb
98
99
  - lib/encrypted_form_fields/encrypted_parameters.rb
99
100
  - lib/encrypted_form_fields/helpers/encrypted_field.rb
100
101
  - lib/encrypted_form_fields/helpers/form_builder.rb
@@ -102,6 +103,7 @@ files:
102
103
  - lib/encrypted_form_fields/helpers/form_tag_helper.rb
103
104
  - lib/encrypted_form_fields/railtie.rb
104
105
  - lib/encrypted_form_fields/version.rb
106
+ - test/encrypted_form_fields_test.rb
105
107
  - test/encrypted_parameters_test.rb
106
108
  - test/form_builder_test.rb
107
109
  - test/form_tag_helper_test.rb
@@ -131,6 +133,7 @@ signing_key:
131
133
  specification_version: 4
132
134
  summary: Encrypted form fields for Rails
133
135
  test_files:
136
+ - test/encrypted_form_fields_test.rb
134
137
  - test/encrypted_parameters_test.rb
135
138
  - test/form_builder_test.rb
136
139
  - test/form_tag_helper_test.rb