2012-01-01 12:57:55 -05:00
|
|
|
require 'base64'
|
2010-01-01 15:00:37 -05:00
|
|
|
require 'active_support/core_ext/object/blank'
|
2014-10-23 09:58:43 -04:00
|
|
|
require 'active_support/security_utils'
|
2010-01-01 14:59:51 -05:00
|
|
|
|
2008-11-23 09:29:11 -05:00
|
|
|
module ActiveSupport
|
2012-09-17 01:22:18 -04:00
|
|
|
# +MessageVerifier+ makes it easy to generate and verify messages which are
|
|
|
|
# signed to prevent tampering.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2012-09-17 01:22:18 -04:00
|
|
|
# This is useful for cases like remember-me tokens and auto-unsubscribe links
|
2012-12-05 01:11:54 -05:00
|
|
|
# where the session store isn't suitable or available.
|
2008-11-23 09:29:11 -05:00
|
|
|
#
|
|
|
|
# Remember Me:
|
2008-11-23 10:33:56 -05:00
|
|
|
# cookies[:remember_me] = @verifier.generate([@user.id, 2.weeks.from_now])
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2008-11-23 09:29:11 -05:00
|
|
|
# In the authentication filter:
|
|
|
|
#
|
2008-11-23 10:33:56 -05:00
|
|
|
# id, time = @verifier.verify(cookies[:remember_me])
|
2015-12-10 14:42:41 -05:00
|
|
|
# if Time.now < time
|
2008-11-23 09:29:11 -05:00
|
|
|
# self.current_user = User.find(id)
|
|
|
|
# end
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2012-09-17 01:22:18 -04:00
|
|
|
# By default it uses Marshal to serialize the message. If you want to use
|
2013-03-24 14:46:46 -04:00
|
|
|
# another serialization method, you can set the serializer in the options
|
|
|
|
# hash upon initialization:
|
2011-09-15 13:23:08 -04:00
|
|
|
#
|
2013-03-24 14:46:46 -04:00
|
|
|
# @verifier = ActiveSupport::MessageVerifier.new('s3Krit', serializer: YAML)
|
2016-02-17 16:48:04 -05:00
|
|
|
#
|
|
|
|
# +MessageVerifier+ creates HMAC signatures using SHA1 hash algorithm by default.
|
|
|
|
# If you want to use a different hash algorithm, you can change it by providing
|
|
|
|
# `:digest` key as an option while initializing the verifier:
|
|
|
|
#
|
|
|
|
# @verifier = ActiveSupport::MessageVerifier.new('s3Krit', digest: 'SHA256')
|
2008-11-23 09:29:11 -05:00
|
|
|
class MessageVerifier
|
|
|
|
class InvalidSignature < StandardError; end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2011-09-15 14:27:12 -04:00
|
|
|
def initialize(secret, options = {})
|
2014-09-12 12:47:33 -04:00
|
|
|
raise ArgumentError, 'Secret should not be nil.' unless secret
|
2008-11-23 09:29:11 -05:00
|
|
|
@secret = secret
|
2011-09-15 14:27:12 -04:00
|
|
|
@digest = options[:digest] || 'SHA1'
|
|
|
|
@serializer = options[:serializer] || Marshal
|
2008-11-23 09:29:11 -05:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2014-12-03 21:00:32 -05:00
|
|
|
# Checks if a signed message could have been generated by signing an object
|
|
|
|
# with the +MessageVerifier+'s secret.
|
|
|
|
#
|
|
|
|
# verifier = ActiveSupport::MessageVerifier.new 's3Krit'
|
|
|
|
# signed_message = verifier.generate 'a private message'
|
|
|
|
# verifier.valid_message?(signed_message) # => true
|
|
|
|
#
|
|
|
|
# tampered_message = signed_message.chop # editing the message invalidates the signature
|
|
|
|
# verifier.valid_message?(tampered_message) # => false
|
2014-11-21 18:52:22 -05:00
|
|
|
def valid_message?(signed_message)
|
2015-06-04 18:16:02 -04:00
|
|
|
return if signed_message.nil? || !signed_message.valid_encoding? || signed_message.blank?
|
2014-12-02 10:10:18 -05:00
|
|
|
|
2015-07-19 17:19:15 -04:00
|
|
|
data, digest = signed_message.split("--".freeze)
|
2014-11-21 18:52:22 -05:00
|
|
|
data.present? && digest.present? && ActiveSupport::SecurityUtils.secure_compare(digest, generate_digest(data))
|
|
|
|
end
|
|
|
|
|
2014-12-03 21:00:32 -05:00
|
|
|
# Decodes the signed message using the +MessageVerifier+'s secret.
|
|
|
|
#
|
|
|
|
# verifier = ActiveSupport::MessageVerifier.new 's3Krit'
|
|
|
|
#
|
|
|
|
# signed_message = verifier.generate 'a private message'
|
2014-12-04 11:09:47 -05:00
|
|
|
# verifier.verified(signed_message) # => 'a private message'
|
2014-12-03 21:00:32 -05:00
|
|
|
#
|
|
|
|
# Returns +nil+ if the message was not signed with the same secret.
|
|
|
|
#
|
|
|
|
# other_verifier = ActiveSupport::MessageVerifier.new 'd1ff3r3nt-s3Krit'
|
2014-12-04 11:09:47 -05:00
|
|
|
# other_verifier.verified(signed_message) # => nil
|
2014-12-03 21:00:32 -05:00
|
|
|
#
|
|
|
|
# Returns +nil+ if the message is not Base64-encoded.
|
|
|
|
#
|
|
|
|
# invalid_message = "f--46a0120593880c733a53b6dad75b42ddc1c8996d"
|
2014-12-04 11:09:47 -05:00
|
|
|
# verifier.verified(invalid_message) # => nil
|
2014-12-03 21:00:32 -05:00
|
|
|
#
|
|
|
|
# Raises any error raised while decoding the signed message.
|
|
|
|
#
|
|
|
|
# incompatible_message = "test--dad7b06c94abba8d46a15fafaef56c327665d5ff"
|
2014-12-04 11:09:47 -05:00
|
|
|
# verifier.verified(incompatible_message) # => TypeError: incompatible marshal file format
|
2014-11-21 18:52:22 -05:00
|
|
|
def verified(signed_message)
|
|
|
|
if valid_message?(signed_message)
|
2013-05-15 10:11:04 -04:00
|
|
|
begin
|
2015-07-19 17:19:15 -04:00
|
|
|
data = signed_message.split("--".freeze)[0]
|
2014-11-12 17:48:44 -05:00
|
|
|
@serializer.load(decode(data))
|
2013-12-07 13:56:09 -05:00
|
|
|
rescue ArgumentError => argument_error
|
2014-12-02 10:10:18 -05:00
|
|
|
return if argument_error.message =~ %r{invalid base64}
|
2013-12-07 13:56:09 -05:00
|
|
|
raise
|
2013-05-15 10:11:04 -04:00
|
|
|
end
|
2008-11-23 09:29:11 -05:00
|
|
|
end
|
|
|
|
end
|
2014-12-02 10:10:18 -05:00
|
|
|
|
2014-12-03 21:00:32 -05:00
|
|
|
# Decodes the signed message using the +MessageVerifier+'s secret.
|
|
|
|
#
|
|
|
|
# verifier = ActiveSupport::MessageVerifier.new 's3Krit'
|
|
|
|
# signed_message = verifier.generate 'a private message'
|
|
|
|
#
|
2014-12-04 11:09:47 -05:00
|
|
|
# verifier.verify(signed_message) # => 'a private message'
|
2014-12-03 21:00:32 -05:00
|
|
|
#
|
|
|
|
# Raises +InvalidSignature+ if the message was not signed with the same
|
|
|
|
# secret or was not Base64-encoded.
|
|
|
|
#
|
|
|
|
# other_verifier = ActiveSupport::MessageVerifier.new 'd1ff3r3nt-s3Krit'
|
2014-12-09 18:23:15 -05:00
|
|
|
# other_verifier.verify(signed_message) # => ActiveSupport::MessageVerifier::InvalidSignature
|
2014-11-21 18:52:22 -05:00
|
|
|
def verify(signed_message)
|
|
|
|
verified(signed_message) || raise(InvalidSignature)
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2014-12-03 21:00:32 -05:00
|
|
|
# Generates a signed message for the provided value.
|
|
|
|
#
|
|
|
|
# The message is signed with the +MessageVerifier+'s secret. Without knowing
|
|
|
|
# the secret, the original value cannot be extracted from the message.
|
|
|
|
#
|
|
|
|
# verifier = ActiveSupport::MessageVerifier.new 's3Krit'
|
|
|
|
# verifier.generate 'a private message' # => "BAhJIhRwcml2YXRlLW1lc3NhZ2UGOgZFVA==--e2d724331ebdee96a10fb99b089508d1c72bd772"
|
2008-11-23 10:33:56 -05:00
|
|
|
def generate(value)
|
2014-11-12 17:48:44 -05:00
|
|
|
data = encode(@serializer.dump(value))
|
2008-11-23 09:29:11 -05:00
|
|
|
"#{data}--#{generate_digest(data)}"
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2008-11-23 09:29:11 -05:00
|
|
|
private
|
2014-11-12 17:48:44 -05:00
|
|
|
def encode(data)
|
|
|
|
::Base64.strict_encode64(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
def decode(data)
|
|
|
|
::Base64.strict_decode64(data)
|
|
|
|
end
|
|
|
|
|
2008-11-23 09:29:11 -05:00
|
|
|
def generate_digest(data)
|
2008-11-23 18:29:03 -05:00
|
|
|
require 'openssl' unless defined?(OpenSSL)
|
2009-09-25 01:43:45 -04:00
|
|
|
OpenSSL::HMAC.hexdigest(OpenSSL::Digest.const_get(@digest).new, @secret, data)
|
2008-11-23 09:29:11 -05:00
|
|
|
end
|
|
|
|
end
|
2008-11-23 18:29:03 -05:00
|
|
|
end
|