1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add commit in the EncryptedCookieJar

Gets rid of the option parsing and makes what the encryptor does stand out.
This commit is contained in:
Kasper Timm Hansen 2015-09-06 17:49:12 +02:00
parent b807ac7a7a
commit 94b313db8d

View file

@ -541,12 +541,11 @@ module ActionDispatch
end end
end end
class EncryptedCookieJar #:nodoc: class EncryptedCookieJar < AbstractCookieJar # :nodoc:
include ChainedCookieJars
include SerializedCookieJars include SerializedCookieJars
def initialize(parent_jar) def initialize(parent_jar)
@parent_jar = parent_jar super
if ActiveSupport::LegacyKeyGenerator === key_generator if ActiveSupport::LegacyKeyGenerator === key_generator
raise "You didn't set secrets.secret_key_base, which is required for this cookie jar. " + raise "You didn't set secrets.secret_key_base, which is required for this cookie jar. " +
@ -566,22 +565,13 @@ module ActionDispatch
end end
end end
# Encrypts and sets the cookie named +name+. The second argument may be the cookie's private
# value or a hash of options as documented above. def commit(options)
def []=(name, options) options[:value] = @encryptor.encrypt_and_sign(serialize(options[:value]))
if options.is_a?(Hash)
options.symbolize_keys! raise CookieOverflow if options[:value].bytesize > MAX_COOKIE_SIZE
else
options = { :value => options }
end end
options[:value] = @encryptor.encrypt_and_sign(serialize(options[:value]))
raise CookieOverflow if options[:value].bytesize > MAX_COOKIE_SIZE
@parent_jar[name] = options
end
private
def decrypt_and_verify(encrypted_message) def decrypt_and_verify(encrypted_message)
@encryptor.decrypt_and_verify(encrypted_message) @encryptor.decrypt_and_verify(encrypted_message)
rescue ActiveSupport::MessageVerifier::InvalidSignature, ActiveSupport::MessageEncryptor::InvalidMessage rescue ActiveSupport::MessageVerifier::InvalidSignature, ActiveSupport::MessageEncryptor::InvalidMessage