instance method generate

Ruby on Rails 8.1.2

Since v2.3.18

Available in: v2.3.18 v3.0.20 v3.1.12 v3.2.22.5 v4.0.13 v4.1.16 v4.2.9 v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

generate(value, **options)

Generates a signed message for the provided value.

The message is signed with the MessageVerifier‘s secret. Returns Base64-encoded message joined with the generated signature.

verifier = ActiveSupport::MessageVerifier.new("secret")
verifier.generate("signed message") # => "BAhJIhNzaWduZWQgbWVzc2FnZQY6BkVU--f67d5f27c3ee0b8483cebf2103757455e947493b"

Options

:expires_at

The datetime at which the message expires. After this datetime, verification of the message will fail.

message = verifier.generate("hello", expires_at: Time.now.tomorrow)
verifier.verified(message) # => "hello"
# 24 hours later...
verifier.verified(message) # => nil
verifier.verify(message)   # => raises ActiveSupport::MessageVerifier::InvalidSignature
:expires_in

The duration for which the message is valid. After this duration has elapsed, verification of the message will fail.

message = verifier.generate("hello", expires_in: 24.hours)
verifier.verified(message) # => "hello"
# 24 hours later...
verifier.verified(message) # => nil
verifier.verify(message)   # => raises ActiveSupport::MessageVerifier::InvalidSignature
:purpose

The purpose of the message. If specified, the same purpose must be specified when verifying the message; otherwise, verification will fail. (See #verified and #verify.)

Parameters

value req
options keyrest
Source
# File activesupport/lib/active_support/message_verifier.rb, line 306
    def generate(value, **options)
      create_message(value, **options)
    end

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

Defined in ActiveSupport::MessageVerifier

Type at least 2 characters to search.

↑↓ navigate · open · esc close