Skip to content

Commit c7792a0

Browse files
Document MessageEncryptor method options [ci-skip]
This documents the options on the methods themselves, so that the reader does not have to scan the class summary documentation for a specific option.
1 parent 5eb793a commit c7792a0

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

activesupport/lib/active_support/message_encryptor.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,52 @@ def initialize(secret, sign_secret = nil, cipher: nil, digest: nil, serializer:
165165

166166
# Encrypt and sign a message. We need to sign the message in order to avoid
167167
# padding attacks. Reference: https://www.limited-entropy.com/padding-oracle-attacks/.
168+
#
169+
# ==== Options
170+
#
171+
# [+:expires_at+]
172+
# The datetime at which the message expires. After this datetime,
173+
# verification of the message will fail.
174+
#
175+
# message = encryptor.encrypt_and_sign("hello", expires_at: Time.now.tomorrow)
176+
# encryptor.decrypt_and_verify(message) # => "hello"
177+
# # 24 hours later...
178+
# encryptor.decrypt_and_verify(message) # => nil
179+
#
180+
# [+:expires_in+]
181+
# The duration for which the message is valid. After this duration has
182+
# elapsed, verification of the message will fail.
183+
#
184+
# message = encryptor.encrypt_and_sign("hello", expires_in: 24.hours)
185+
# encryptor.decrypt_and_verify(message) # => "hello"
186+
# # 24 hours later...
187+
# encryptor.decrypt_and_verify(message) # => nil
188+
#
189+
# [+:purpose+]
190+
# The purpose of the message. If specified, the same purpose must be
191+
# specified when verifying the message; otherwise, verification will fail.
192+
# (See #decrypt_and_verify.)
168193
def encrypt_and_sign(value, expires_at: nil, expires_in: nil, purpose: nil)
169194
verifier.generate(_encrypt(value, expires_at: expires_at, expires_in: expires_in, purpose: purpose))
170195
end
171196

172197
# Decrypt and verify a message. We need to verify the message in order to
173198
# avoid padding attacks. Reference: https://www.limited-entropy.com/padding-oracle-attacks/.
199+
#
200+
# ==== Options
201+
#
202+
# [+:purpose+]
203+
# The purpose that the message was generated with. If the purpose does not
204+
# match, +decrypt_and_verify+ will return +nil+.
205+
#
206+
# message = encryptor.encrypt_and_sign("hello", purpose: "greeting")
207+
# encryptor.decrypt_and_verify(message, purpose: "greeting") # => "hello"
208+
# encryptor.decrypt_and_verify(message) # => nil
209+
#
210+
# message = encryptor.encrypt_and_sign("bye")
211+
# encryptor.decrypt_and_verify(message) # => "bye"
212+
# encryptor.decrypt_and_verify(message, purpose: "greeting") # => nil
213+
#
174214
def decrypt_and_verify(data, purpose: nil, **)
175215
_decrypt(verifier.verify(data), purpose)
176216
end

0 commit comments

Comments
 (0)