class MessageVerifier
Ruby on Rails 5.1.7
Since v2.3.18MessageVerifier makes it easy to generate and verify messages which are signed to prevent tampering.
This is useful for cases like remember-me tokens and auto-unsubscribe links where the session store isn’t suitable or available.
Remember Me:
cookies[:remember_me] = @verifier.generate([@user.id, 2.weeks.from_now])
In the authentication filter:
id, time = @verifier.verify(cookies[:remember_me])
if Time.now < time
self.current_user = User.find(id)
end
By default it uses Marshal to serialize the message. If you want to use another serialization method, you can set the serializer in the options hash upon initialization:
@verifier = ActiveSupport::MessageVerifier.new('s3Krit', serializer: YAML)
MessageVerifier creates HMAC signatures using SHA1 hash algorithm by default. If you want to use a different hash algorithm, you can change it by providing :digest key as an option while initializing the verifier:
@verifier = ActiveSupport::MessageVerifier.new('s3Krit', digest: 'SHA256')
Inherits from
Namespace
Classes
Methods (defined here)
- # generate
- # valid_message?
- # verified
- # verify
- self. new
Private methods (3)
Show private methods Implementation detail — not part of the public API.
- # decode
- # encode
- # generate_digest
Methods (inherited)
From Object
(25)
- # <
- # acts_like?
- # blank?
- # change
- # create_fixtures
- # deep_dup
- # destroy
- # duplicable?
- # html_safe?
- # in?
- # instance_values
- # instance_variable_names
- # perform
- # presence
- # presence_in
- # present?
- # subscribed
- # to_param
- # to_query
- # try
- # try!
- # unescape
- # unsubscribed
- # with_options
- self. table_name_prefix