yattr_encrypted 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.mdown +31 -3
- data/lib/yattr_encrypted.rb +5 -3
- data/lib/yattr_encrypted/version.rb +2 -2
- data/test/yattr_encrypted_test.rb +22 -1
- metadata +4 -4
data/README.mdown
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# YattrEncrypted #
|
2
2
|
|
3
|
-
Version: 0.1.
|
3
|
+
Version: 0.1.4 (but you should check lib/yattr_encrypted/version.rb to be sure)
|
4
4
|
|
5
5
|
## Applicability ##
|
6
6
|
|
@@ -88,6 +88,23 @@ matching encrypted values without retrieving the encrypted data.
|
|
88
88
|
|
89
89
|
### Options ###
|
90
90
|
|
91
|
+
**yattr_encrypted** accepts five options:
|
92
|
+
|
93
|
+
* :prefix - the prefix which is prepended to *attribute* to form the encrypted attribute
|
94
|
+
name. Defaults to ''
|
95
|
+
* :suffix - the suffix which appended to *attribute* to form the encrypted attribute
|
96
|
+
name. Defaults to '_encrypted'
|
97
|
+
* :key - allows attribute specific encryption keys. The default - if not specified - is
|
98
|
+
`Rails.application.config.secret_token`
|
99
|
+
* :read_filter - a LAMBDA (or Proc) which is called to modify the clear-text value of
|
100
|
+
the *attribute* prior to returning it.
|
101
|
+
* :write_filter - a LAMBDA (or Proc) which is called on the assigned value prior to
|
102
|
+
assigning it to the *attribute*
|
103
|
+
|
104
|
+
These options are discussed in some detail below in the applicable section.
|
105
|
+
|
106
|
+
### Encrypted Attribute Name ###
|
107
|
+
|
91
108
|
The encrypted field name defaults to `<field>_encrypted`. You can change this on
|
92
109
|
a field by field basis using the `:prefix` and `:suffix` - which define strings
|
93
110
|
which are prefixed and suffixed to the field name to create the encrypted field name.
|
@@ -99,7 +116,7 @@ They default to:
|
|
99
116
|
Notice that the underscore ('_') must be included in `:prefix` and `:suffix`, if you
|
100
117
|
want them.
|
101
118
|
|
102
|
-
### Encryption keys
|
119
|
+
### Encryption keys ###
|
103
120
|
|
104
121
|
The default encryption key is the value of `<application>::Application.config.secret_token`
|
105
122
|
which is in `config/initializers/secret_token.rb`.
|
@@ -114,7 +131,18 @@ and 'this is a very long secret key' is not.
|
|
114
131
|
|
115
132
|
If you supply your own key, it can be a String or a Proc which returns a String.
|
116
133
|
|
117
|
-
###
|
134
|
+
### Special Attribute Processing ###
|
135
|
+
|
136
|
+
Special processing for attribute values can be implemented by using the `:read_filter` and
|
137
|
+
`:write_filter` options of the `yattr_encrypted` macro.
|
138
|
+
|
139
|
+
A `:read_filter` is a proc (or lambda) which accepts a single argument. It is called on the
|
140
|
+
value of the *attribute* immediately before being returned by the clear text attribute accessor.
|
141
|
+
|
142
|
+
A `:write_filter` is a proc (or lambda) which accepts a single argument. It is called
|
143
|
+
on the value passed to the *attribute* writer prior to any other action in the writer.
|
144
|
+
|
145
|
+
### Encription Initial Values ###
|
118
146
|
|
119
147
|
As stated everywhere - random initial values are automatically generated for all fields.
|
120
148
|
They are prepended to the actual encrypted data and stripped during decryption. You can't
|
data/lib/yattr_encrypted.rb
CHANGED
@@ -76,6 +76,7 @@ module YattrEncrypted
|
|
76
76
|
|
77
77
|
# iterate through attributes and create accessors, verify encryped accessors exist
|
78
78
|
attributes.map { |x| x.to_sym }.each do |attribute|
|
79
|
+
puts "yate_encrypted: #{attribute}"
|
79
80
|
encrypted_attribute_name = [options[:prefix], attribute, options[:suffix]].join.to_sym
|
80
81
|
|
81
82
|
# barf if reader and write doesn't exist for encrypted attribute
|
@@ -86,23 +87,24 @@ module YattrEncrypted
|
|
86
87
|
|
87
88
|
tmp =<<-XXX
|
88
89
|
def #{attribute}
|
90
|
+
options = yate_encrypted_attributes[:#{attribute}]
|
89
91
|
unless @#{attribute} && !@#{attribute}.empty?
|
90
|
-
options = yate_encrypted_attributes[:#{attribute}]
|
91
92
|
@#{attribute} = #{encrypted_attribute_name} ? \
|
92
93
|
yate_decrypt(#{encrypted_attribute_name}, options[:key]) : \
|
93
94
|
''
|
94
95
|
self.yate_checksums[:#{attribute}] = yate_attribute_hash_value(:#{attribute})
|
95
96
|
self.yate_dirty[:#{attribute}] = true
|
96
97
|
end
|
97
|
-
@#{attribute}
|
98
|
+
options[:read_filter] ? options[:read_filter].call(@#{attribute}) : @#{attribute}
|
98
99
|
end
|
99
100
|
XXX
|
100
101
|
class_eval(tmp)
|
101
102
|
|
102
103
|
tmp =<<-XXX
|
103
104
|
def #{attribute}= value
|
104
|
-
@#{attribute} = value
|
105
105
|
options = yate_encrypted_attributes[:#{attribute}]
|
106
|
+
value = options[:write_filter].call(value) if options[:write_filter]
|
107
|
+
@#{attribute} = value
|
106
108
|
self.#{encrypted_attribute_name} = yate_encrypt(value, options[:key])
|
107
109
|
self.yate_checksums[:#{attribute}] = yate_attribute_hash_value(:#{attribute})
|
108
110
|
self.yate_dirty[:#{attribute}] = true
|
@@ -1,3 +1,3 @@
|
|
1
1
|
module YattrEncrypted
|
2
|
-
VERSION = '0.1.
|
3
|
-
end
|
2
|
+
VERSION = '0.1.4'
|
3
|
+
end
|
@@ -35,8 +35,12 @@ module ActiveRecord
|
|
35
35
|
end
|
36
36
|
|
37
37
|
class SomeClass < ActiveRecord::Base
|
38
|
-
attr_accessor :field_encrypted
|
38
|
+
attr_accessor :field_encrypted, :special_reader_encrypted, :special_writer_encrypted
|
39
39
|
yattr_encrypted :field, :key => 'a honkin big key: honk honk honk honk honk'
|
40
|
+
yattr_encrypted :special_reader, :key => 'a honkin big key: honk honk honk honk honk',
|
41
|
+
:read_filter => lambda { |val| val.strip }
|
42
|
+
yattr_encrypted :special_writer, :key => 'a honkin big key: honk honk honk honk honk',
|
43
|
+
:write_filter => lambda { |val| puts val; val.upcase }
|
40
44
|
end
|
41
45
|
|
42
46
|
class TestYattrEncrypted < MiniTest::Unit::TestCase
|
@@ -73,4 +77,21 @@ class TestYattrEncrypted < MiniTest::Unit::TestCase
|
|
73
77
|
decrypted = @sc.send(:yate_decrypt, @sc.field_encrypted, options[:key])
|
74
78
|
assert_equal( { key: 'value' }, decrypted, "decrypt @sc.field_encrypted should be correct")
|
75
79
|
end
|
80
|
+
|
81
|
+
def test_special_reader
|
82
|
+
value = 'a string with leading and trailing white space'
|
83
|
+
value_with_whitespace = ' ' + value + ' '
|
84
|
+
@sc.special_reader = value_with_whitespace
|
85
|
+
assert_equal value_with_whitespace, @sc.instance_variable_get(:@special_reader),
|
86
|
+
"@special_reader should include the leading and trailing whitespace"
|
87
|
+
assert_equal value, @sc.special_reader, "@sc.special_reader should have leading and trailing ws stripped"
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_special_writer
|
91
|
+
value = 'a string with leading and trailing white space'
|
92
|
+
@sc.special_writer = value
|
93
|
+
assert_equal value.upcase, @sc.instance_variable_get(:@special_writer),
|
94
|
+
"@special_writer should be upcased"
|
95
|
+
assert_equal value.upcase, @sc.special_writer, "@sc.special_writer should be upcased"
|
96
|
+
end
|
76
97
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yattr_encrypted
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-03-19 00:00:00.000000000Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: pry
|
16
|
-
requirement: &
|
16
|
+
requirement: &2151769420 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2151769420
|
25
25
|
description: Generates yattr_accessors that encrypt and decrypt attributes transparently.
|
26
26
|
Based on attr_encrypted by Sean Huber [https://github.com/shuber]
|
27
27
|
email: mike@clove.com
|
@@ -56,7 +56,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
56
56
|
version: '0'
|
57
57
|
segments:
|
58
58
|
- 0
|
59
|
-
hash:
|
59
|
+
hash: 2285454077099666257
|
60
60
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|