has_secure_whatever 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +100 -0
- data/Rakefile +27 -0
- data/lib/has_secure_whatever/configuration.rb +17 -0
- data/lib/has_secure_whatever/encryptor.rb +32 -0
- data/lib/has_secure_whatever/has_secure.rb +36 -0
- data/lib/has_secure_whatever/railtie.rb +7 -0
- data/lib/has_secure_whatever/version.rb +3 -0
- data/lib/has_secure_whatever.rb +6 -0
- data/lib/tasks/has_secure_whatever_tasks.rake +6 -0
- metadata +82 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8e5e8bbb64e0a7891303dbc7ee9893aedc797adbd23e451f13b023a4ffe52db2
|
4
|
+
data.tar.gz: 52e1981553fbb20b0270b29e8c370b8546d2358ac8df4c0797ab18accecbd718
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 27b5d00fafaa3374faeeabfec0d2703c6b4000e18d5118debaf9a2d966cbed3372e6d46ade17e16a95f58e88a233de7f6800ae69ec8d6ded4cb984b37dd9a4cb
|
7
|
+
data.tar.gz: 5fbcf9bb11b0e393eff768dd60f33700abbfc12ef560e66f0370c6eafbfcbdd2354462c98c0ad1d9970a6e5ed0c978543785dffa0816850fb97ce9cfdd64bbd7
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2018 Arandi Lopez
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
# Has secure... whatever...
|
2
|
+
|
3
|
+
Add encrypted attributes to your rails models and keep data safe in database, then get them back as normal text
|
4
|
+
|
5
|
+
**WARNING:** THIS PLUGIN IS NOT A REPLACEMENT OF RAILS' `has_secure_password`, THIS PLUGIN MUST NOT BE USED TO AUTHENTICATE USERS, AND/OR SHOULD NOT BE USED TO SAVE USERS PASSWORDS.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'has_secure_whatever'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
```bash
|
16
|
+
$ bundle install
|
17
|
+
```
|
18
|
+
|
19
|
+
Generate a new secret key
|
20
|
+
|
21
|
+
```
|
22
|
+
$ rails has_secure_whatever:generate_secret_key
|
23
|
+
H9dDCsP5hBRCGTuXc7R0CcsHZIX4vakSwCcvpHs1TQA=
|
24
|
+
```
|
25
|
+
|
26
|
+
Set the secret key in a initializer
|
27
|
+
|
28
|
+
```
|
29
|
+
# config/application.rb
|
30
|
+
# Some code here...
|
31
|
+
|
32
|
+
# It's better if you get it from a ENV or from your encrypted credentials
|
33
|
+
HasSecureWhatever.config.secret_key = "H9dDCsP5hBRCGTuXc7R0CcsHZIX4vakSwCcvpHs1TQA="
|
34
|
+
```
|
35
|
+
|
36
|
+
## Usage
|
37
|
+
|
38
|
+
Create a model or a migration to add the attributes that will save encrypted text. Convention is to name them as *attribute*\_digest
|
39
|
+
|
40
|
+
```
|
41
|
+
$ rails generate model message content_digest sender_id_digest
|
42
|
+
```
|
43
|
+
|
44
|
+
In your model, setup the secure attribute
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
# app/models/message.rb
|
48
|
+
class Message < ApplicationRecord
|
49
|
+
has_secure :content
|
50
|
+
has_secure :sender_id
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
If you don't want validations:
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
# app/models/message.rb
|
58
|
+
class Message < ApplicationRecord
|
59
|
+
has_secure :content, validations: false
|
60
|
+
has_secure :sender_id
|
61
|
+
end
|
62
|
+
```
|
63
|
+
|
64
|
+
If your encrypted attribute has a different column name:
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
# app/models/message.rb
|
68
|
+
class Message < ApplicationRecord
|
69
|
+
has_secure :content, digest_name: :content_encrypted
|
70
|
+
has_secure :sender_id
|
71
|
+
end
|
72
|
+
```
|
73
|
+
|
74
|
+
Now create and add data
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
|
78
|
+
message = Message.new
|
79
|
+
message.content = "This is a new message. Hello!"
|
80
|
+
message.sender_id = "ID-A786252"
|
81
|
+
message.save
|
82
|
+
|
83
|
+
message.content_digest #=> "RM9Q93FBH+qFRAnR+1AofpMB--BgDpGocU7hv3p+1q--LonJLa5biV6rxFu3z/oJmg=="
|
84
|
+
message.content #=> "This is a new message. Hello!"
|
85
|
+
```
|
86
|
+
|
87
|
+
## Contributing
|
88
|
+
|
89
|
+
1. Fork it ( https://github.com/arandilopez/has_secure_whatever/fork )
|
90
|
+
2. Create your feature branch (git checkout -b my-new-feature)
|
91
|
+
3. Commit your changes (git commit -am 'Add some feature')
|
92
|
+
4. Push to the branch (git push origin my-new-feature)
|
93
|
+
5. Create a new Pull Request
|
94
|
+
|
95
|
+
## Contributors
|
96
|
+
|
97
|
+
- [arandilopez](https://github.com/arandilopez) Arandi Lopez - creator, maintainer
|
98
|
+
|
99
|
+
## License
|
100
|
+
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,27 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'Has Secure Whatever'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
require 'bundler/gem_tasks'
|
18
|
+
|
19
|
+
require 'rake/testtask'
|
20
|
+
|
21
|
+
Rake::TestTask.new(:test) do |t|
|
22
|
+
t.libs << 'test'
|
23
|
+
t.pattern = 'test/**/*_test.rb'
|
24
|
+
t.verbose = false
|
25
|
+
end
|
26
|
+
|
27
|
+
task default: :test
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module HasSecureWhatever
|
2
|
+
class Configuration
|
3
|
+
attr_accessor :secret_key
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@secret_key = nil
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.configure
|
11
|
+
yield config
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.config
|
15
|
+
@configuration ||= Configuration.new
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module HasSecureWhatever
|
2
|
+
class Encryptor
|
3
|
+
KEY_LEN = 32
|
4
|
+
def initialize(key = nil)
|
5
|
+
@key = nil
|
6
|
+
if key
|
7
|
+
@key = key.unpack('m').first
|
8
|
+
else
|
9
|
+
secret_key = HasSecureWhatever.config.secret_key
|
10
|
+
@key = secret_key.unpack('m').first unless secret_key.nil?
|
11
|
+
end
|
12
|
+
if @key.nil?
|
13
|
+
raise "Secret key for has_secure is not set"
|
14
|
+
end
|
15
|
+
@crypt = ActiveSupport::MessageEncryptor.new(@key)
|
16
|
+
end
|
17
|
+
|
18
|
+
def encrypt(unencrypted_value)
|
19
|
+
@crypt.encrypt_and_sign(unencrypted_value)
|
20
|
+
end
|
21
|
+
|
22
|
+
def decrypt(encrypted_value)
|
23
|
+
@crypt.decrypt_and_verify(encrypted_value)
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.generate_secret_key
|
27
|
+
salt = SecureRandom.random_bytes(KEY_LEN)
|
28
|
+
key = ActiveSupport::KeyGenerator.new('password').generate_key(salt, KEY_LEN)
|
29
|
+
[key].pack('m')
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module HasSecureWhatever
|
2
|
+
module HasSecure
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
module ClassMethods
|
6
|
+
def has_secure(attribute, validations: true, digest_name: nil)
|
7
|
+
digest_attribute = digest_name || "#{attribute}_digest"
|
8
|
+
|
9
|
+
define_method("#{attribute}") do
|
10
|
+
encrypted_value = self.send("#{digest_attribute}")
|
11
|
+
if encrypted_value.nil?
|
12
|
+
return nil
|
13
|
+
else
|
14
|
+
Encryptor.new.decrypt(encrypted_value)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
define_method("#{attribute}=") do |unencrypted_value|
|
19
|
+
if unencrypted_value.nil?
|
20
|
+
self.send("#{digest_attribute}=", nil)
|
21
|
+
elsif !unencrypted_value.empty?
|
22
|
+
encrypted_value = Encryptor.new.encrypt(unencrypted_value)
|
23
|
+
self.send("#{digest_attribute}=", encrypted_value)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
if validations
|
28
|
+
include ActiveModel::Validations
|
29
|
+
validate do |record|
|
30
|
+
record.errors.add(attribute, :blank) unless record.send(digest_attribute).present?
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: has_secure_whatever
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Arandi Lopez
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-10-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 5.2.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 5.2.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sqlite3
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Add secure encrypted fields to protect sensitive information in database
|
42
|
+
email:
|
43
|
+
- arandilopez.93@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- MIT-LICENSE
|
49
|
+
- README.md
|
50
|
+
- Rakefile
|
51
|
+
- lib/has_secure_whatever.rb
|
52
|
+
- lib/has_secure_whatever/configuration.rb
|
53
|
+
- lib/has_secure_whatever/encryptor.rb
|
54
|
+
- lib/has_secure_whatever/has_secure.rb
|
55
|
+
- lib/has_secure_whatever/railtie.rb
|
56
|
+
- lib/has_secure_whatever/version.rb
|
57
|
+
- lib/tasks/has_secure_whatever_tasks.rake
|
58
|
+
homepage: https://github.com/arandilopez/has_secure_whatever
|
59
|
+
licenses:
|
60
|
+
- MIT
|
61
|
+
metadata: {}
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 2.7.3
|
79
|
+
signing_key:
|
80
|
+
specification_version: 4
|
81
|
+
summary: Add secure encrypted fields to protect sensitive information in database
|
82
|
+
test_files: []
|