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'
|
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])
|
2008-11-23 09:29:11 -05:00
|
|
|
# if time < Time.now
|
|
|
|
# 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)
|
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 = {})
|
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
|
|
|
|
2008-11-23 10:33:56 -05:00
|
|
|
def verify(signed_message)
|
2009-10-05 08:27:54 -04:00
|
|
|
raise InvalidSignature if signed_message.blank?
|
|
|
|
|
2008-11-23 09:29:11 -05:00
|
|
|
data, digest = signed_message.split("--")
|
2009-10-08 21:26:08 -04:00
|
|
|
if data.present? && digest.present? && secure_compare(digest, generate_digest(data))
|
2013-05-15 10:11:04 -04:00
|
|
|
begin
|
|
|
|
@serializer.load(::Base64.strict_decode64(data))
|
2013-12-07 13:56:09 -05:00
|
|
|
rescue ArgumentError => argument_error
|
|
|
|
raise InvalidSignature if argument_error.message =~ %r{invalid base64}
|
|
|
|
raise
|
2013-05-15 10:11:04 -04:00
|
|
|
end
|
2009-08-13 13:03:08 -04:00
|
|
|
else
|
|
|
|
raise InvalidSignature
|
2008-11-23 09:29:11 -05:00
|
|
|
end
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2008-11-23 10:33:56 -05:00
|
|
|
def generate(value)
|
2012-01-01 12:57:55 -05:00
|
|
|
data = ::Base64.strict_encode64(@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
|
2009-09-13 03:32:30 -04:00
|
|
|
# constant-time comparison algorithm to prevent timing attacks
|
|
|
|
def secure_compare(a, b)
|
|
|
|
return false unless a.bytesize == b.bytesize
|
2009-09-12 15:35:03 -04:00
|
|
|
|
2010-07-13 19:13:37 -04:00
|
|
|
l = a.unpack "C#{a.bytesize}"
|
2009-09-13 03:32:30 -04:00
|
|
|
|
2010-07-13 19:13:37 -04:00
|
|
|
res = 0
|
|
|
|
b.each_byte { |byte| res |= byte ^ l.shift }
|
|
|
|
res == 0
|
2009-08-13 13:03:08 -04:00
|
|
|
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
|