blob: db14ac0b1c128e0988c22e11255d78eedb1fe462 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# frozen_string_literal: true
require "time"
module ActiveSupport
module Messages #:nodoc:
class Metadata #:nodoc:
def initialize(expires_at, purpose)
@expires_at, @purpose = expires_at, purpose.to_s
end
class << self
def wrap(message, expires_at: nil, expires_in: nil, purpose: nil)
if expires_at || expires_in || purpose
{ "value" => message, "_rails" => { "exp" => pick_expiry(expires_at, expires_in), "pur" => purpose } }
else
message
end
end
def verify(message, purpose)
metadata = extract_metadata(message)
if metadata.nil?
message if purpose.nil?
elsif metadata.match?(purpose) && metadata.fresh?
message["value"]
end
end
private
def pick_expiry(expires_at, expires_in)
if expires_at
expires_at.utc.iso8601(3)
elsif expires_in
Time.now.utc.advance(seconds: expires_in).iso8601(3)
end
end
def extract_metadata(message)
if message.is_a?(Hash) && message.key?("_rails")
new(message["_rails"]["exp"], message["_rails"]["pur"])
end
end
end
def match?(purpose)
@purpose == purpose.to_s
end
def fresh?
@expires_at.nil? || Time.now.utc < Time.iso8601(@expires_at)
end
end
end
end
|