aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/messages/metadata.rb
blob: e97caac7662bcd02cd23342bba5e79beadadcdb9 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# frozen_string_literal: true

require "time"

module ActiveSupport
  module Messages #:nodoc:
    class Metadata #:nodoc:
      def initialize(message, expires_at = nil, purpose = nil)
        @message, @expires_at, @purpose = message, expires_at, purpose
      end

      def as_json(options = {})
        { _rails: { message: @message, exp: @expires_at, pur: @purpose } }
      end

      class << self
        def wrap(message, expires_at: nil, expires_in: nil, purpose: nil)
          if expires_at || expires_in || purpose
            JSON.encode new(encode(message), pick_expiry(expires_at, expires_in), purpose)
          else
            message
          end
        end

        def verify(message, purpose)
          extract_metadata(message).verify(purpose)
        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)
            data = JSON.decode(message) rescue nil

            if data.is_a?(Hash) && data.key?("_rails")
              new(decode(data["_rails"]["message"]), data["_rails"]["exp"], data["_rails"]["pur"])
            else
              new(message)
            end
          end

          def encode(message)
            ::Base64.strict_encode64(message)
          end

          def decode(message)
            ::Base64.strict_decode64(message)
          end
      end

      def verify(purpose)
        @message if match?(purpose) && fresh?
      end

      private
        def match?(purpose)
          @purpose.to_s == purpose.to_s
        end

        def fresh?
          @expires_at.nil? || Time.now.utc < Time.iso8601(@expires_at)
        end
    end
  end
end