encryptable_attributes 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.editorconfig +9 -0
- data/.gitignore +9 -0
- data/.travis.yml +6 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +87 -0
- data/LICENSE.txt +21 -0
- data/README.md +48 -0
- data/Rakefile +13 -0
- data/bin/console +14 -0
- data/bin/setup +9 -0
- data/db/config.yml +5 -0
- data/db/migrate/20180512123720_add_user_test_model.rb +8 -0
- data/db/schema.rb +20 -0
- data/encryptable_attributes.gemspec +31 -0
- data/lib/encryptable_attributes.rb +7 -0
- data/lib/encryptable_attributes/base.rb +46 -0
- data/lib/encryptable_attributes/version.rb +3 -0
- metadata +159 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5daff2b8fcca8a9a4e4b924fdfcdcc98489c6aac5babfb131fcc0974972824c2
|
4
|
+
data.tar.gz: 9dc2f517d69e1444c433cd067a3e023250fe245c6f74bbfca83817a6f19dc6b5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 509d8ed356ac43e3c181a44ce6e8e01421dc2d16945621b124eb5210ab28044c17e880d5b57f59b23528873148adf81e6c91694526db8ba56358f1b95373faf4
|
7
|
+
data.tar.gz: 2b43868c8aa86cc44ae78c07c2a4e224232626a367db16ac1fa4831dec83af550bcefc1fcc4e051a835d37a6f55a414cc8222ec8bbdd5cee4b7033ec8c25d396
|
data/.editorconfig
ADDED
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
encryptable_attributes (0.1.0)
|
5
|
+
activesupport
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
actionpack (5.2.0)
|
11
|
+
actionview (= 5.2.0)
|
12
|
+
activesupport (= 5.2.0)
|
13
|
+
rack (~> 2.0)
|
14
|
+
rack-test (>= 0.6.3)
|
15
|
+
rails-dom-testing (~> 2.0)
|
16
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
17
|
+
actionview (5.2.0)
|
18
|
+
activesupport (= 5.2.0)
|
19
|
+
builder (~> 3.1)
|
20
|
+
erubi (~> 1.4)
|
21
|
+
rails-dom-testing (~> 2.0)
|
22
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
23
|
+
activemodel (5.2.0)
|
24
|
+
activesupport (= 5.2.0)
|
25
|
+
activerecord (5.2.0)
|
26
|
+
activemodel (= 5.2.0)
|
27
|
+
activesupport (= 5.2.0)
|
28
|
+
arel (>= 9.0)
|
29
|
+
activesupport (5.2.0)
|
30
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
31
|
+
i18n (>= 0.7, < 2)
|
32
|
+
minitest (~> 5.1)
|
33
|
+
tzinfo (~> 1.1)
|
34
|
+
arel (9.0.0)
|
35
|
+
builder (3.2.3)
|
36
|
+
concurrent-ruby (1.0.5)
|
37
|
+
crass (1.0.4)
|
38
|
+
erubi (1.7.1)
|
39
|
+
i18n (1.0.1)
|
40
|
+
concurrent-ruby (~> 1.0)
|
41
|
+
loofah (2.2.2)
|
42
|
+
crass (~> 1.0.2)
|
43
|
+
nokogiri (>= 1.5.9)
|
44
|
+
method_source (0.9.0)
|
45
|
+
mini_portile2 (2.3.0)
|
46
|
+
minitest (5.11.3)
|
47
|
+
nokogiri (1.8.2)
|
48
|
+
mini_portile2 (~> 2.3.0)
|
49
|
+
rack (2.0.5)
|
50
|
+
rack-test (1.0.0)
|
51
|
+
rack (>= 1.0, < 3)
|
52
|
+
rails-dom-testing (2.0.3)
|
53
|
+
activesupport (>= 4.2.0)
|
54
|
+
nokogiri (>= 1.6)
|
55
|
+
rails-html-sanitizer (1.0.4)
|
56
|
+
loofah (~> 2.2, >= 2.2.2)
|
57
|
+
railties (5.2.0)
|
58
|
+
actionpack (= 5.2.0)
|
59
|
+
activesupport (= 5.2.0)
|
60
|
+
method_source
|
61
|
+
rake (>= 0.8.7)
|
62
|
+
thor (>= 0.18.1, < 2.0)
|
63
|
+
rake (10.5.0)
|
64
|
+
sqlite3 (1.3.13)
|
65
|
+
standalone_migrations (5.2.5)
|
66
|
+
activerecord (>= 4.2.7, < 5.3.0)
|
67
|
+
railties (>= 4.2.7, < 5.3.0)
|
68
|
+
rake (>= 10.0)
|
69
|
+
thor (0.20.0)
|
70
|
+
thread_safe (0.3.6)
|
71
|
+
tzinfo (1.2.5)
|
72
|
+
thread_safe (~> 0.1)
|
73
|
+
|
74
|
+
PLATFORMS
|
75
|
+
ruby
|
76
|
+
|
77
|
+
DEPENDENCIES
|
78
|
+
activerecord
|
79
|
+
bundler (~> 1.16)
|
80
|
+
encryptable_attributes!
|
81
|
+
minitest (~> 5.0)
|
82
|
+
rake (~> 10.0)
|
83
|
+
sqlite3
|
84
|
+
standalone_migrations
|
85
|
+
|
86
|
+
BUNDLED WITH
|
87
|
+
1.16.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Nils Sommer
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# EncryptableAttributes [![Build Status](https://travis-ci.org/nsommer/encryptable_attributes.svg?branch=master)](https://travis-ci.org/nsommer/encryptable_attributes)
|
2
|
+
|
3
|
+
With the `encryptable_attributes` gem, you transparently encrypt and decrypt attributes of an ActiveRecord model. It uses `ActiveSupport::MessageEncryptor` to encrypt and decrypt values and provides a simple class-level DSL for configuration.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'encryptable_attributes'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install encryptable_attributes
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Consider a simple ActiveRecord model `Message` with a `title` and a `body` field. To store those attributes' values encrypted, use the following code snippet.
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class Message < ActiveRecord::Base
|
27
|
+
include EncryptableAttributes::Base
|
28
|
+
|
29
|
+
secure_key ENV.fetch('SECRET_KEY')
|
30
|
+
secure_attrs :title, :body
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
34
|
+
ActiveRecord models use an `attributes` hash internally to keep attributes. EncryptablesAttributes overrides the accessor methods for the corresponding attributes and encrypts given values before storing them in the `attributes` hash and decrypts them when reading them from the `attributes` hash.
|
35
|
+
|
36
|
+
## Development
|
37
|
+
|
38
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
39
|
+
|
40
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
41
|
+
|
42
|
+
## Contributing
|
43
|
+
|
44
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/nsommer/encryptable_attributes.
|
45
|
+
|
46
|
+
## License
|
47
|
+
|
48
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rake/testtask"
|
3
|
+
|
4
|
+
require 'standalone_migrations'
|
5
|
+
StandaloneMigrations::Tasks.load_tasks
|
6
|
+
|
7
|
+
Rake::TestTask.new(:test) do |t|
|
8
|
+
t.libs << "test"
|
9
|
+
t.libs << "lib"
|
10
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
11
|
+
end
|
12
|
+
|
13
|
+
task :default => :test
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "encryptable_attributes"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/db/config.yml
ADDED
data/db/schema.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# This file is auto-generated from the current state of the database. Instead
|
2
|
+
# of editing this file, please use the migrations feature of Active Record to
|
3
|
+
# incrementally modify your database, and then regenerate this schema definition.
|
4
|
+
#
|
5
|
+
# Note that this schema.rb definition is the authoritative source for your
|
6
|
+
# database schema. If you need to create the application database on another
|
7
|
+
# system, you should be using db:schema:load, not running all the migrations
|
8
|
+
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
|
9
|
+
# you'll amass, the slower it'll run and the greater likelihood for issues).
|
10
|
+
#
|
11
|
+
# It's strongly recommended that you check this file into your version control system.
|
12
|
+
|
13
|
+
ActiveRecord::Schema.define(version: 2018_05_12_123720) do
|
14
|
+
|
15
|
+
create_table "users", force: :cascade do |t|
|
16
|
+
t.string "name"
|
17
|
+
t.string "secret_info"
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "encryptable_attributes/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "encryptable_attributes"
|
8
|
+
spec.version = EncryptableAttributes::VERSION
|
9
|
+
spec.authors = ["Nils Sommer"]
|
10
|
+
spec.email = ["mail@nilssommer.de"]
|
11
|
+
|
12
|
+
spec.summary = "DSL for encryption and decryption of attributes in ActiveRecord models"
|
13
|
+
spec.homepage = "https://github.com/nsommer/encryptable_attributes"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_dependency "activesupport"
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
28
|
+
spec.add_development_dependency "activerecord"
|
29
|
+
spec.add_development_dependency "standalone_migrations"
|
30
|
+
spec.add_development_dependency "sqlite3"
|
31
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module EncryptableAttributes
|
2
|
+
module Base
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
# Override ActiveRecord accessor
|
6
|
+
def [](key)
|
7
|
+
send key
|
8
|
+
end
|
9
|
+
|
10
|
+
# Override ActiveRecord accessor
|
11
|
+
def []=(key, value)
|
12
|
+
send "#{key}=", value
|
13
|
+
end
|
14
|
+
|
15
|
+
class_methods do
|
16
|
+
def secure_key(key)
|
17
|
+
@@secure_key = key
|
18
|
+
end
|
19
|
+
|
20
|
+
def secure_attrs(*attr_names)
|
21
|
+
attr_names.each do |attr_name|
|
22
|
+
define_method :"#{attr_name}=" do |value|
|
23
|
+
write_attribute(attr_name, crypt.encrypt_and_sign(value))
|
24
|
+
end
|
25
|
+
|
26
|
+
define_method :"#{attr_name}" do
|
27
|
+
crypt.decrypt_and_verify(read_attribute(attr_name))
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
protected
|
34
|
+
|
35
|
+
def new_crypt
|
36
|
+
len = ActiveSupport::MessageEncryptor.key_len
|
37
|
+
salt = SecureRandom.random_bytes(len)
|
38
|
+
key = ActiveSupport::KeyGenerator.new(@@secure_key).generate_key(salt, len)
|
39
|
+
@crypt = ActiveSupport::MessageEncryptor.new(key)
|
40
|
+
end
|
41
|
+
|
42
|
+
def crypt
|
43
|
+
@crypt ||= new_crypt
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
metadata
ADDED
@@ -0,0 +1,159 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: encryptable_attributes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nils Sommer
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-05-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.16'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.16'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: activerecord
|
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
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: standalone_migrations
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: sqlite3
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description:
|
112
|
+
email:
|
113
|
+
- mail@nilssommer.de
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".editorconfig"
|
119
|
+
- ".gitignore"
|
120
|
+
- ".travis.yml"
|
121
|
+
- Gemfile
|
122
|
+
- Gemfile.lock
|
123
|
+
- LICENSE.txt
|
124
|
+
- README.md
|
125
|
+
- Rakefile
|
126
|
+
- bin/console
|
127
|
+
- bin/setup
|
128
|
+
- db/config.yml
|
129
|
+
- db/migrate/20180512123720_add_user_test_model.rb
|
130
|
+
- db/schema.rb
|
131
|
+
- encryptable_attributes.gemspec
|
132
|
+
- lib/encryptable_attributes.rb
|
133
|
+
- lib/encryptable_attributes/base.rb
|
134
|
+
- lib/encryptable_attributes/version.rb
|
135
|
+
homepage: https://github.com/nsommer/encryptable_attributes
|
136
|
+
licenses:
|
137
|
+
- MIT
|
138
|
+
metadata: {}
|
139
|
+
post_install_message:
|
140
|
+
rdoc_options: []
|
141
|
+
require_paths:
|
142
|
+
- lib
|
143
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
144
|
+
requirements:
|
145
|
+
- - ">="
|
146
|
+
- !ruby/object:Gem::Version
|
147
|
+
version: '0'
|
148
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
requirements: []
|
154
|
+
rubyforge_project:
|
155
|
+
rubygems_version: 2.7.6
|
156
|
+
signing_key:
|
157
|
+
specification_version: 4
|
158
|
+
summary: DSL for encryption and decryption of attributes in ActiveRecord models
|
159
|
+
test_files: []
|