aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2011-09-15 09:14:16 -0700
committerJosé Valim <jose.valim@gmail.com>2011-09-15 09:14:16 -0700
commit06c3ca8173add65c4eeaa1942195348e6ebb0574 (patch)
tree234254725c5702f5e94c39626fcfc06e387f12a5 /activesupport/lib
parentda7f0426ec7b0aa053489633c2a8a3da6423654f (diff)
parenta8aaef676217f53f2812cd56f71a6b00c5d22162 (diff)
downloadrails-06c3ca8173add65c4eeaa1942195348e6ebb0574.tar.gz
rails-06c3ca8173add65c4eeaa1942195348e6ebb0574.tar.bz2
rails-06c3ca8173add65c4eeaa1942195348e6ebb0574.zip
Merge pull request #3031 from wvanbergen/master
Custom serializers and deserializers in MessageVerifier and MessageEncryptor.
Diffstat (limited to 'activesupport/lib')
-rw-r--r--activesupport/lib/active_support/message_encryptor.rb8
-rw-r--r--activesupport/lib/active_support/message_verifier.rb8
2 files changed, 12 insertions, 4 deletions
diff --git a/activesupport/lib/active_support/message_encryptor.rb b/activesupport/lib/active_support/message_encryptor.rb
index 4f7cd12d48..05d6790075 100644
--- a/activesupport/lib/active_support/message_encryptor.rb
+++ b/activesupport/lib/active_support/message_encryptor.rb
@@ -13,9 +13,13 @@ module ActiveSupport
class InvalidMessage < StandardError; end
OpenSSLCipherError = OpenSSL::Cipher.const_defined?(:CipherError) ? OpenSSL::Cipher::CipherError : OpenSSL::CipherError
+ attr_accessor :serializer, :deserializer
+
def initialize(secret, cipher = 'aes-256-cbc')
@secret = secret
@cipher = cipher
+ @serializer = lambda { |value| Marshal.dump(value) }
+ @deserializer = lambda { |value| Marshal.load(value) }
end
def encrypt(value)
@@ -27,7 +31,7 @@ module ActiveSupport
cipher.key = @secret
cipher.iv = iv
- encrypted_data = cipher.update(Marshal.dump(value))
+ encrypted_data = cipher.update(serializer.call(value))
encrypted_data << cipher.final
[encrypted_data, iv].map {|v| ActiveSupport::Base64.encode64s(v)}.join("--")
@@ -44,7 +48,7 @@ module ActiveSupport
decrypted_data = cipher.update(encrypted_data)
decrypted_data << cipher.final
- Marshal.load(decrypted_data)
+ deserializer.call(decrypted_data)
rescue OpenSSLCipherError, TypeError
raise InvalidMessage
end
diff --git a/activesupport/lib/active_support/message_verifier.rb b/activesupport/lib/active_support/message_verifier.rb
index 8f3946325a..e38e242cfe 100644
--- a/activesupport/lib/active_support/message_verifier.rb
+++ b/activesupport/lib/active_support/message_verifier.rb
@@ -21,9 +21,13 @@ module ActiveSupport
class MessageVerifier
class InvalidSignature < StandardError; end
+ attr_accessor :serializer, :deserializer
+
def initialize(secret, digest = 'SHA1')
@secret = secret
@digest = digest
+ @serializer = lambda { |value| Marshal.dump(value) }
+ @deserializer = lambda { |value| Marshal.load(value) }
end
def verify(signed_message)
@@ -31,14 +35,14 @@ module ActiveSupport
data, digest = signed_message.split("--")
if data.present? && digest.present? && secure_compare(digest, generate_digest(data))
- Marshal.load(ActiveSupport::Base64.decode64(data))
+ deserializer.call(ActiveSupport::Base64.decode64(data))
else
raise InvalidSignature
end
end
def generate(value)
- data = ActiveSupport::Base64.encode64s(Marshal.dump(value))
+ data = ActiveSupport::Base64.encode64s(serializer.call(value))
"#{data}--#{generate_digest(data)}"
end