encrypted_form_fields 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d6aea0ee8b40654f443b625193e3e98ee3bdb5f0
4
+ data.tar.gz: 551356231ce1ec66184686a3a60a58836b0c27e0
5
+ SHA512:
6
+ metadata.gz: 462c4c84e0d97d4758ceae69511301ef5f8918972989e84a1a7db79fa21e247c0cf99bd3661ff7d3bac9a65a6e0bf2273839299823d8b6c656c3435757cb197c
7
+ data.tar.gz: 58a4a166da91bdf453a3a6c05de33ed37db5c7bc0bfe6614b5c148567bf5ae3f6e5714889c338bb0d35bcd499f1b4b3b7c34b739aa8daa6144a6fc97449d2cb7
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in encrypted_form_fields.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Ville Lautanala
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,59 @@
1
+ # EncryptedFormFields [![Build Status](https://travis-ci.org/lautis/encrypted_form_fields.png)](https://travis-ci.org/lautis/encrypted_form_fields)
2
+
3
+ Encrypted form fields for Rails apps.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'encrypted_form_fields'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install encrypted_form_fields
18
+
19
+ ## Usage
20
+
21
+ Configure necessary encryption keys in Rails initializer:
22
+
23
+ ```ruby
24
+ EncryptedFormFields.secret_key_base = # your secret key base
25
+ EncryptedFormFields.secret_token = # your secret token
26
+
27
+ ```
28
+
29
+ Create encrypted inputs in your view:
30
+
31
+ ```erb
32
+
33
+ <%= form_for(user) do |f| %>
34
+ <%= f.encrypted_field :secrets %>
35
+ <%= encrypted_field_tag :field_name, "secret data" %>
36
+ <% end %>
37
+
38
+ ```
39
+
40
+ Then access the data in controller:
41
+
42
+ ```ruby
43
+
44
+ class SomeController
45
+ def create
46
+ # do stuff...
47
+ encrypted_params # This will contain values of encrypted parameters
48
+ # do stuff...
49
+ end
50
+ end
51
+ ```
52
+
53
+ ## Contributing
54
+
55
+ 1. Fork it ( http://github.com/lautis/encrypted_form_fields/fork )
56
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
57
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
58
+ 4. Push to the branch (`git push origin my-new-feature`)
59
+ 5. Create a new Pull Request
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << "test"
6
+ t.pattern = "test/*_test.rb"
7
+ end
8
+
9
+ task default: :test
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'encrypted_form_fields/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "encrypted_form_fields"
8
+ spec.version = EncryptedFormFields::VERSION
9
+ spec.authors = ["Ville Lautanala"]
10
+ spec.email = ["lautis@gmail.com"]
11
+ spec.summary = %q{Encrypted form fields for Rails}
12
+ spec.description = %q{Encrypted form fields for Rails}
13
+ spec.homepage = "https://github.com/lautis/encrypted_form_fields"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "actionpack", "~> 4.0.0"
22
+ spec.add_runtime_dependency "activesupport", "~> 4.0.0"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "minitest"
26
+ end
@@ -0,0 +1,50 @@
1
+ require "action_controller"
2
+ require "action_view"
3
+ require "active_support/message_encryptor"
4
+
5
+ require "encrypted_form_fields/version"
6
+ require "encrypted_form_fields/encrypted_parameters"
7
+ require "encrypted_form_fields/helpers/form_builder"
8
+ require 'encrypted_form_fields/railtie' if defined?(Rails)
9
+
10
+ module EncryptedFormFields
11
+ class << self
12
+ def secret_key_base=(key)
13
+ @encryptor = nil
14
+ @secret_key_base = key
15
+ end
16
+
17
+ def secret_key_base
18
+ @secret_key_base
19
+ end
20
+
21
+ def secret_token=(key)
22
+ @encryptor = nil
23
+ @secret_token = key
24
+ end
25
+
26
+ def secret_token
27
+ @secret_token
28
+ end
29
+
30
+ def prefix_name(name)
31
+ first, rest = name.split("[", 2)
32
+ rest = "[" + rest if rest
33
+ "_encrypted[#{first}]#{rest}"
34
+ end
35
+
36
+ delegate :encrypt_and_sign, :decrypt_and_verify, to: :encryptor
37
+
38
+ private
39
+
40
+ def encryptor
41
+ @encryptor ||= begin
42
+ key = ActiveSupport::KeyGenerator.new(secret_token).generate_key(secret_key_base)
43
+ ActiveSupport::MessageEncryptor.new(key)
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ ActionController::Base.send(:include, EncryptedFormFields::EncryptedParameters)
50
+ ActionView::Helpers::FormBuilder.send(:include, EncryptedFormFields::Helpers::FormBuilder)
@@ -0,0 +1,31 @@
1
+ module EncryptedFormFields
2
+ module EncryptedParameters
3
+ # Decrypt encrypted parameters
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
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,20 @@
1
+ require 'action_view/helpers/tags/hidden_field'
2
+
3
+ module EncryptedFormFields
4
+ module Helpers
5
+ class EncryptedField < ActionView::Helpers::Tags::HiddenField
6
+ def initialize(object_name, method_name, template_object, options = {})
7
+ super(object_name, method_name, template_object, options.dup)
8
+ value = @options.with_indifferent_access.fetch("value") { value_before_type_cast(object) }
9
+ @options["value"] = EncryptedFormFields.encrypt_and_sign(value)
10
+ @object_name = EncryptedFormFields.prefix_name(@object_name)
11
+ end
12
+
13
+ class << self
14
+ def field_type
15
+ "hidden"
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,15 @@
1
+ module EncryptedFormFields
2
+ module Helpers
3
+ module FormBuilder
4
+ def encrypted_field(method, options = {})
5
+ @template.encrypted_field(@object_name, method, objectify_options(options))
6
+ end
7
+
8
+ private
9
+
10
+ def objectify_options(options)
11
+ @default_options.merge(options.merge(object: @object))
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,18 @@
1
+ require 'encrypted_form_fields/helpers/encrypted_field'
2
+
3
+ module EncryptedFormFields
4
+ module Helpers
5
+ module FormHelper
6
+ # Returns a hidden and encrypted input tag for accessing a specified
7
+ # attribute (identified by +method+) on an object assigned to the template
8
+ # (identified by +object+).
9
+ #
10
+ # ==== Examples
11
+ # encrypted_field(:user, :email_verified_at)
12
+ # # => <input type="hidden" id="_encrypted_user_email_verified_at" name="_encrypted[user][email_verified_at]" value="#{encrypt(@user.email_verified_at})" />
13
+ def encrypted_field(object_name, method, options = {})
14
+ EncryptedField.new(object_name, method, self, options).render
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,25 @@
1
+ module EncryptedFormFields
2
+ module Helpers
3
+ module FormTagHelper
4
+ # Creates a hidden input field used with encrypted content. Use this field
5
+ # to transmit data that user shouldn't see or be able to modify.
6
+ #
7
+ # ==== Options
8
+ # * Creates standard HTML attributes for the tag.
9
+ #
10
+ # ==== Examples
11
+ # encrypted_field_tag 'email_verified_at', Time.now.to_s
12
+ # => <input id="email_verified_at" name="_encrypted_email_verified_at" type="hidden" value="[encrypted]" />
13
+ def encrypted_field_tag(name, value = nil, options = {})
14
+ encrypted_value = EncryptedFormFields.encrypt_and_sign(value)
15
+ prefixed_name = EncryptedFormFields.prefix_name(name)
16
+ tag :input, {
17
+ "type" => "hidden",
18
+ "name" => prefixed_name,
19
+ "id" => sanitize_to_id(name),
20
+ "value" => encrypted_value
21
+ }.update(options.stringify_keys)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,12 @@
1
+ # lib/my_gem/railtie.rb
2
+ require "encrypted_form_fields/helpers/form_tag_helper"
3
+ require "encrypted_form_fields/helpers/form_helper"
4
+
5
+ module EncryptedFormFields
6
+ class Railtie < Rails::Railtie
7
+ initializer "encrypted_form_fields.view_helpers" do
8
+ ActionView::Base.send(:include, EncryptedFormFields::Helpers::FormHelper)
9
+ ActionView::Base.send(:include, EncryptedFormFields::Helpers::FormTagHelper)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module EncryptedFormFields
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,27 @@
1
+ require 'test_helper'
2
+
3
+ class EncryptedParametersTest < MiniTest::Unit::TestCase
4
+ def test_missing_encrypted_params
5
+ controller = MockController.new({})
6
+ assert_equal({}, controller.encrypted_params)
7
+ end
8
+
9
+ def test_invalid_encrypted_params
10
+ controller = MockController.new({"_encrypted" => {"key" => "value"}})
11
+ assert_raises ActiveSupport::MessageVerifier::InvalidSignature do
12
+ controller.encrypted_params
13
+ end
14
+ end
15
+
16
+ def test_properly_encrypted_params
17
+ value = EncryptedFormFields.encrypt_and_sign("value")
18
+ controller = MockController.new({"_encrypted" => {"key" => value}})
19
+ assert_equal({"key" => "value"}, controller.encrypted_params)
20
+
21
+ controller = MockController.new({"_encrypted" => {"key" => [value]}})
22
+ assert_equal({"key" => ["value"]}, controller.encrypted_params)
23
+
24
+ controller = MockController.new({"_encrypted" => {"key" => {"nested" => value}}})
25
+ assert_equal({"key" => {"nested" => "value"}}, controller.encrypted_params)
26
+ end
27
+ end
@@ -0,0 +1,23 @@
1
+ require 'test_helper'
2
+ require 'encrypted_form_fields/helpers/form_helper'
3
+
4
+ class FormBuilderTest < MiniTest::Unit::TestCase
5
+ def setup
6
+ super
7
+ @template = Object.new
8
+ @template.extend ActionView::Helpers::FormHelper
9
+ @template.extend EncryptedFormFields::Helpers::FormHelper
10
+ @template.extend ActionView::Helpers::FormOptionsHelper
11
+ @object = Struct.new(:bar).new(SecureRandom.base64)
12
+ end
13
+
14
+ def test_encrypted_form_tag
15
+ form_builder = ActionView::Helpers::FormBuilder.new(:foo, @object, @template, {})
16
+ tag = HTML::Document.new(form_builder.encrypted_field(:bar)).find(tag: "input")
17
+ decrypted_value = EncryptedFormFields.decrypt_and_verify(tag.attributes["value"])
18
+ assert_equal @object.bar, decrypted_value
19
+ assert_equal "_encrypted[foo][bar]", tag.attributes["name"]
20
+ assert_equal "hidden", tag.attributes["type"]
21
+ assert_equal "_encrypted_foo_bar", tag.attributes["id"]
22
+ end
23
+ end
@@ -0,0 +1,15 @@
1
+ require 'test_helper'
2
+ require 'encrypted_form_fields/helpers/form_tag_helper'
3
+
4
+ class FormTagHelperTest < ActionView::TestCase
5
+ tests EncryptedFormFields::Helpers::FormTagHelper
6
+
7
+ def test_encrypted_form_tag
8
+ tag = HTML::Document.new(encrypted_field_tag "field", "value").find(tag: "input")
9
+ decrypted_value = EncryptedFormFields.decrypt_and_verify(tag.attributes["value"])
10
+ assert_equal "value", decrypted_value
11
+ assert_equal "_encrypted[field]", tag.attributes["name"]
12
+ assert_equal "hidden", tag.attributes["type"]
13
+ assert_equal "field", tag.attributes["id"]
14
+ end
15
+ end
@@ -0,0 +1,23 @@
1
+ ENV['RAILS_ENV'] = 'test'
2
+ require 'minitest/unit'
3
+ require 'minitest/autorun'
4
+ require 'minitest/pride'
5
+ require 'securerandom'
6
+ require 'encrypted_form_fields'
7
+
8
+ EncryptedFormFields.secret_key_base = SecureRandom.hex
9
+ EncryptedFormFields.secret_token = SecureRandom.hex
10
+
11
+ class MockController
12
+ attr_accessor :request
13
+
14
+ def initialize(params = {})
15
+ @params = params
16
+ end
17
+
18
+ def params
19
+ ActionController::Parameters.new(@params)
20
+ end
21
+ end
22
+
23
+ MockController.send(:include, EncryptedFormFields::EncryptedParameters)
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: encrypted_form_fields
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ville Lautanala
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: actionpack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 4.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 4.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 4.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Encrypted form fields for Rails
84
+ email:
85
+ - lautis@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - .travis.yml
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - encrypted_form_fields.gemspec
97
+ - lib/encrypted_form_fields.rb
98
+ - lib/encrypted_form_fields/encrypted_parameters.rb
99
+ - lib/encrypted_form_fields/helpers/encrypted_field.rb
100
+ - lib/encrypted_form_fields/helpers/form_builder.rb
101
+ - lib/encrypted_form_fields/helpers/form_helper.rb
102
+ - lib/encrypted_form_fields/helpers/form_tag_helper.rb
103
+ - lib/encrypted_form_fields/railtie.rb
104
+ - lib/encrypted_form_fields/version.rb
105
+ - test/encrypted_parameters_test.rb
106
+ - test/form_builder_test.rb
107
+ - test/form_tag_helper_test.rb
108
+ - test/test_helper.rb
109
+ homepage: https://github.com/lautis/encrypted_form_fields
110
+ licenses:
111
+ - MIT
112
+ metadata: {}
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - '>='
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.1.11
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: Encrypted form fields for Rails
133
+ test_files:
134
+ - test/encrypted_parameters_test.rb
135
+ - test/form_builder_test.rb
136
+ - test/form_tag_helper_test.rb
137
+ - test/test_helper.rb