instance method rotate

Ruby on Rails 8.0.4

Since v7.1.6

Available in: v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

rotate(**options)
rotate(&block)

Adds options to the list of option sets. Messages will be signed using the first set in the list. When verifying, however, each set will be tried, in order, until one succeeds.

Notably, the :secret_generator option can specify a different secret generator than the one initially specified. The secret generator must respond to call, accept a salt, and return a suitable secret (string). The secret generator may also accept arbitrary kwargs.

If any options match the kwargs of the operative secret generator, those options will be passed to the secret generator instead of to the message verifier.

For fine-grained per-salt rotations, a block form is supported. The block will receive the salt, and should return an appropriate options Hash. The block may also return nil to indicate that the rotation does not apply to the given salt. For example:

verifiers = ActiveSupport::MessageVerifiers.new { ... }

verifiers.rotate do |salt|
  case salt
  when :foo
    { serializer: JSON, url_safe: true }
  when :bar
    { serializer: Marshal, url_safe: true }
  end
end

verifiers.rotate(serializer: Marshal, url_safe: false)

# Uses `serializer: JSON, url_safe: true`.
# Falls back to `serializer: Marshal, url_safe: false`.
verifiers[:foo]

# Uses `serializer: Marshal, url_safe: true`.
# Falls back to `serializer: Marshal, url_safe: false`.
verifiers[:bar]

# Uses `serializer: Marshal, url_safe: false`.
verifiers[:baz]
Source
# File activesupport/lib/active_support/message_verifiers.rb, line 61
    

Defined in activesupport/lib/active_support/message_verifiers.rb line 61 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveSupport::MessageVerifiers

Type at least 2 characters to search.

↑↓ navigate · open · esc close