diff options
author | Logan Leger <logan@loganleger.com> | 2014-11-21 17:52:22 -0600 |
---|---|---|
committer | Logan Leger <logan@loganleger.com> | 2014-12-01 21:43:16 -0600 |
commit | 7ad541f955dfa396f4a6b29700bed2e188cd9187 (patch) | |
tree | 51b05babd4c4a1484b609a85365fff736197c2cd /activesupport/lib/active_support | |
parent | cd77755ae46616f24ae0ad2e586be528bb326be1 (diff) | |
download | rails-7ad541f955dfa396f4a6b29700bed2e188cd9187.tar.gz rails-7ad541f955dfa396f4a6b29700bed2e188cd9187.tar.bz2 rails-7ad541f955dfa396f4a6b29700bed2e188cd9187.zip |
Add `#verified` and `#valid_message?` to MessageVerifier
This commit adds a `#verified` method to
`ActiveSupport::MessageVerifier` which will return either `false` when
it encounters an error or the message. `#verify` continues to raise an
`InvalidSignature` exception on error.
This commit also adds a convenience boolean method on `MessageVerifier`
as a way to check if a message is valid without performing the
decoding.
Diffstat (limited to 'activesupport/lib/active_support')
-rw-r--r-- | activesupport/lib/active_support/message_verifier.rb | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/activesupport/lib/active_support/message_verifier.rb b/activesupport/lib/active_support/message_verifier.rb index a8a8de5672..8e5d050540 100644 --- a/activesupport/lib/active_support/message_verifier.rb +++ b/activesupport/lib/active_support/message_verifier.rb @@ -34,21 +34,30 @@ module ActiveSupport @serializer = options[:serializer] || Marshal end - def verify(signed_message) - raise InvalidSignature if signed_message.blank? - + def valid_message?(signed_message) + return false if signed_message.blank? + data, digest = signed_message.split("--") - if data.present? && digest.present? && ActiveSupport::SecurityUtils.secure_compare(digest, generate_digest(data)) + data.present? && digest.present? && ActiveSupport::SecurityUtils.secure_compare(digest, generate_digest(data)) + end + + def verified(signed_message) + if valid_message?(signed_message) begin + data = signed_message.split("--")[0] @serializer.load(decode(data)) rescue ArgumentError => argument_error - raise InvalidSignature if argument_error.message =~ %r{invalid base64} + return false if argument_error.message =~ %r{invalid base64} raise end else - raise InvalidSignature + false end end + + def verify(signed_message) + verified(signed_message) || raise(InvalidSignature) + end def generate(value) data = encode(@serializer.dump(value)) |