2017-07-06 11:23:22 -04:00
|
|
|
# frozen_string_literal: true
|
2017-08-03 17:45:23 -04:00
|
|
|
|
2017-07-06 11:23:22 -04:00
|
|
|
require "time"
|
|
|
|
|
|
|
|
module ActiveSupport
|
|
|
|
module Messages #:nodoc:
|
|
|
|
class Metadata #:nodoc:
|
2017-08-09 15:48:25 -04:00
|
|
|
def initialize(message, expires_at = nil, purpose = nil)
|
2019-10-18 19:02:51 -04:00
|
|
|
@message, @purpose = message, purpose
|
|
|
|
@expires_at = expires_at.is_a?(String) ? Time.iso8601(expires_at) : expires_at
|
2017-08-09 15:48:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def as_json(options = {})
|
|
|
|
{ _rails: { message: @message, exp: @expires_at, pur: @purpose } }
|
2017-07-06 11:23:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class << self
|
|
|
|
def wrap(message, expires_at: nil, expires_in: nil, purpose: nil)
|
|
|
|
if expires_at || expires_in || purpose
|
2017-08-09 15:48:25 -04:00
|
|
|
JSON.encode new(encode(message), pick_expiry(expires_at, expires_in), purpose)
|
2017-07-06 11:23:22 -04:00
|
|
|
else
|
|
|
|
message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def verify(message, purpose)
|
2017-08-09 15:48:25 -04:00
|
|
|
extract_metadata(message).verify(purpose)
|
2017-07-06 11:23:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def pick_expiry(expires_at, expires_in)
|
|
|
|
if expires_at
|
|
|
|
expires_at.utc.iso8601(3)
|
|
|
|
elsif expires_in
|
2017-07-23 06:29:03 -04:00
|
|
|
Time.now.utc.advance(seconds: expires_in).iso8601(3)
|
2017-07-06 11:23:22 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def extract_metadata(message)
|
2017-08-09 15:48:25 -04:00
|
|
|
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)
|
2017-07-06 11:23:22 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-09 15:48:25 -04:00
|
|
|
def encode(message)
|
|
|
|
::Base64.strict_encode64(message)
|
|
|
|
end
|
|
|
|
|
|
|
|
def decode(message)
|
|
|
|
::Base64.strict_decode64(message)
|
|
|
|
end
|
2017-07-06 11:23:22 -04:00
|
|
|
end
|
|
|
|
|
2017-08-09 15:48:25 -04:00
|
|
|
def verify(purpose)
|
|
|
|
@message if match?(purpose) && fresh?
|
2017-07-06 11:23:22 -04:00
|
|
|
end
|
2017-08-09 15:48:25 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
def match?(purpose)
|
|
|
|
@purpose.to_s == purpose.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def fresh?
|
2019-10-18 10:14:42 -04:00
|
|
|
@expires_at.nil? || Time.now.utc < @expires_at
|
2017-08-09 15:48:25 -04:00
|
|
|
end
|
2017-07-06 11:23:22 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|