diff options
author | José Valim <jose.valim@gmail.com> | 2011-09-15 10:50:19 -0700 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2011-09-15 10:50:19 -0700 |
commit | 28300f4ea86642eba5d60f5a64b9ea221f794031 (patch) | |
tree | 5d12c8e7fb9e3c7037a2ebe873bf46882ba1a3db | |
parent | 06c3ca8173add65c4eeaa1942195348e6ebb0574 (diff) | |
parent | 2d30d4cb888e2da7c1f0a8828b467d2e21c90cfa (diff) | |
download | rails-28300f4ea86642eba5d60f5a64b9ea221f794031.tar.gz rails-28300f4ea86642eba5d60f5a64b9ea221f794031.tar.bz2 rails-28300f4ea86642eba5d60f5a64b9ea221f794031.zip |
Merge pull request #3035 from wvanbergen/master
Make API for custom message serializers nicer.
-rw-r--r-- | activesupport/lib/active_support/message_encryptor.rb | 11 | ||||
-rw-r--r-- | activesupport/lib/active_support/message_verifier.rb | 16 | ||||
-rw-r--r-- | activesupport/test/message_encryptor_test.rb | 15 | ||||
-rw-r--r-- | activesupport/test/message_verifier_test.rb | 15 |
4 files changed, 39 insertions, 18 deletions
diff --git a/activesupport/lib/active_support/message_encryptor.rb b/activesupport/lib/active_support/message_encryptor.rb index 05d6790075..66e121be26 100644 --- a/activesupport/lib/active_support/message_encryptor.rb +++ b/activesupport/lib/active_support/message_encryptor.rb @@ -13,13 +13,12 @@ module ActiveSupport class InvalidMessage < StandardError; end OpenSSLCipherError = OpenSSL::Cipher.const_defined?(:CipherError) ? OpenSSL::Cipher::CipherError : OpenSSL::CipherError - attr_accessor :serializer, :deserializer + attr_accessor :serializer - def initialize(secret, cipher = 'aes-256-cbc') + def initialize(secret, cipher = 'aes-256-cbc', serializer = Marshal) @secret = secret @cipher = cipher - @serializer = lambda { |value| Marshal.dump(value) } - @deserializer = lambda { |value| Marshal.load(value) } + @serializer = serializer end def encrypt(value) @@ -31,7 +30,7 @@ module ActiveSupport cipher.key = @secret cipher.iv = iv - encrypted_data = cipher.update(serializer.call(value)) + encrypted_data = cipher.update(serializer.dump(value)) encrypted_data << cipher.final [encrypted_data, iv].map {|v| ActiveSupport::Base64.encode64s(v)}.join("--") @@ -48,7 +47,7 @@ module ActiveSupport decrypted_data = cipher.update(encrypted_data) decrypted_data << cipher.final - deserializer.call(decrypted_data) + serializer.load(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 e38e242cfe..0181070479 100644 --- a/activesupport/lib/active_support/message_verifier.rb +++ b/activesupport/lib/active_support/message_verifier.rb @@ -18,16 +18,20 @@ module ActiveSupport # self.current_user = User.find(id) # end # + # By default it uses Marshal to serialize the message. If you want to use another + # serialization method, you can set the serializer attribute to something that responds + # to dump and load, e.g.: + # + # @verifier.serializer = YAML class MessageVerifier class InvalidSignature < StandardError; end - attr_accessor :serializer, :deserializer + attr_accessor :serializer - def initialize(secret, digest = 'SHA1') + def initialize(secret, digest = 'SHA1', serializer = Marshal) @secret = secret @digest = digest - @serializer = lambda { |value| Marshal.dump(value) } - @deserializer = lambda { |value| Marshal.load(value) } + @serializer = serializer end def verify(signed_message) @@ -35,14 +39,14 @@ module ActiveSupport data, digest = signed_message.split("--") if data.present? && digest.present? && secure_compare(digest, generate_digest(data)) - deserializer.call(ActiveSupport::Base64.decode64(data)) + serializer.load(ActiveSupport::Base64.decode64(data)) else raise InvalidSignature end end def generate(value) - data = ActiveSupport::Base64.encode64s(serializer.call(value)) + data = ActiveSupport::Base64.encode64s(serializer.dump(value)) "#{data}--#{generate_digest(data)}" end diff --git a/activesupport/test/message_encryptor_test.rb b/activesupport/test/message_encryptor_test.rb index 95e64fe6d1..9c89512b42 100644 --- a/activesupport/test/message_encryptor_test.rb +++ b/activesupport/test/message_encryptor_test.rb @@ -11,6 +11,17 @@ require 'active_support/time' require 'active_support/json' class MessageEncryptorTest < Test::Unit::TestCase + + class JSONSerializer + def dump(value) + ActiveSupport::JSON.encode(value) + end + + def load(value) + ActiveSupport::JSON.decode(value) + end + end + def setup @encryptor = ActiveSupport::MessageEncryptor.new(SecureRandom.hex(64)) @data = { :some => "data", :now => Time.local(2010) } @@ -41,9 +52,7 @@ class MessageEncryptorTest < Test::Unit::TestCase end def test_alternative_serialization_method - @encryptor.serializer = lambda { |value| ActiveSupport::JSON.encode(value) } - @encryptor.deserializer = lambda { |value| ActiveSupport::JSON.decode(value) } - + @encryptor.serializer = JSONSerializer.new message = @encryptor.encrypt_and_sign({ :foo => 123, 'bar' => Time.utc(2010) }) assert_equal @encryptor.decrypt_and_verify(message), { "foo" => 123, "bar" => "2010-01-01T00:00:00Z" } end diff --git a/activesupport/test/message_verifier_test.rb b/activesupport/test/message_verifier_test.rb index 224e38a52a..10a1738786 100644 --- a/activesupport/test/message_verifier_test.rb +++ b/activesupport/test/message_verifier_test.rb @@ -11,6 +11,17 @@ require 'active_support/time' require 'active_support/json' class MessageVerifierTest < Test::Unit::TestCase + + class JSONSerializer + def dump(value) + ActiveSupport::JSON.encode(value) + end + + def load(value) + ActiveSupport::JSON.decode(value) + end + end + def setup @verifier = ActiveSupport::MessageVerifier.new("Hey, I'm a secret!") @data = { :some => "data", :now => Time.local(2010) } @@ -34,9 +45,7 @@ class MessageVerifierTest < Test::Unit::TestCase end def test_alternative_serialization_method - @verifier.serializer = lambda { |value| ActiveSupport::JSON.encode(value) } - @verifier.deserializer = lambda { |value| ActiveSupport::JSON.decode(value) } - + @verifier.serializer = JSONSerializer.new message = @verifier.generate({ :foo => 123, 'bar' => Time.utc(2010) }) assert_equal @verifier.verify(message), { "foo" => 123, "bar" => "2010-01-01T00:00:00Z" } end |