MessageVerifier 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 < Time.now 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 attribute to something that responds to dump and load, e.g.:
@verifier.serializer = YAML
Methods
Classes and Modules
Class Public methods
# File activesupport/lib/active_support/message_verifier.rb, line 30 30: def initialize(secret, options = {}) 31: unless options.is_a?(Hash) 32: ActiveSupport::Deprecation.warn "The second parameter should be an options hash. Use :digest => 'algorithm' to specify the digest algorithm." 33: options = { :digest => options } 34: end 35: 36: @secret = secret 37: @digest = options[:digest] || 'SHA1' 38: @serializer = options[:serializer] || Marshal 39: end
Instance Public methods
# File activesupport/lib/active_support/message_verifier.rb, line 41 41: def verify(signed_message) 42: raise InvalidSignature if signed_message.blank? 43: 44: data, digest = signed_message.split("--") 45: if data.present? && digest.present? && secure_compare(digest, generate_digest(data)) 46: @serializer.load(::Base64.decode64(data)) 47: else 48: raise InvalidSignature 49: end 50: end