1
0

cookie_rotator.rb 1.1 KB

123456789101112131415161718192021222324252627
  1. # frozen_string_literal: true
  2. # TODO: Remove after 4.2.0
  3. Rails.application.configure do
  4. config.active_support.key_generator_hash_digest_class = OpenSSL::Digest::SHA1
  5. end
  6. Rails.application.config.after_initialize do
  7. Rails.application.config.action_dispatch.cookies_rotations.tap do |cookies|
  8. authenticated_encrypted_cookie_salt = Rails.application.config.action_dispatch.authenticated_encrypted_cookie_salt
  9. signed_cookie_salt = Rails.application.config.action_dispatch.signed_cookie_salt
  10. secret_key_base = Rails.application.secret_key_base
  11. # TODO: Switch to SHA1 after 4.2.0
  12. key_generator = ActiveSupport::KeyGenerator.new(
  13. secret_key_base, iterations: 1000, hash_digest_class: OpenSSL::Digest::SHA256
  14. )
  15. key_len = ActiveSupport::MessageEncryptor.key_len
  16. old_encrypted_secret = key_generator.generate_key(authenticated_encrypted_cookie_salt, key_len)
  17. old_signed_secret = key_generator.generate_key(signed_cookie_salt)
  18. cookies.rotate :encrypted, old_encrypted_secret
  19. cookies.rotate :signed, old_signed_secret
  20. end
  21. end