Skip to content

Commit

Permalink
Merge pull request #44047 from ryanhertz/cookie-rotator
Browse files Browse the repository at this point in the history
changed example cookie rotator to be in after_initialize block
  • Loading branch information
rafaelfranca committed Mar 15, 2022
2 parents 3fc9d12 + e9ef3d3 commit f864490
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions guides/source/upgrading_ruby_on_rails.md
Original file line number Diff line number Diff line change
Expand Up @@ -377,17 +377,20 @@ to register a rotator.
The following is an example for rotator for the encrypted cookies.

```ruby
Rails.application.config.action_dispatch.cookies_rotations.tap do |cookies|
salt = Rails.application.config.action_dispatch.authenticated_encrypted_cookie_salt
secret_key_base = Rails.application.secrets.secret_key_base

key_generator = ActiveSupport::KeyGenerator.new(
secret_key_base, iterations: 1000, hash_digest_class: OpenSSL::Digest::SHA1
)
key_len = ActiveSupport::MessageEncryptor.key_len
secret = key_generator.generate_key(salt, key_len)

cookies.rotate :encrypted, secret
# config/initializers/cookie_rotator.rb
Rails.application.config.after_initialize do
Rails.application.config.action_dispatch.cookies_rotations.tap do |cookies|
salt = Rails.application.config.action_dispatch.authenticated_encrypted_cookie_salt
secret_key_base = Rails.application.secrets.secret_key_base

key_generator = ActiveSupport::KeyGenerator.new(
secret_key_base, iterations: 1000, hash_digest_class: OpenSSL::Digest::SHA1
)
key_len = ActiveSupport::MessageEncryptor.key_len
secret = key_generator.generate_key(salt, key_len)

cookies.rotate :encrypted, secret
end
end
```

Expand Down

0 comments on commit f864490

Please sign in to comment.