1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/lib/active_storage/verified_key_with_expiration.rb
David Heinemeier Hansson c624df326a ActiveVault -> ActiveStorage
Yaroslav agreed to hand over the gem name ❤️
2017-07-06 11:33:29 +02:00

24 lines
640 B
Ruby

class ActiveStorage::VerifiedKeyWithExpiration
class_attribute :verifier, default: defined?(Rails) ? Rails.application.message_verifier('ActiveStorage') : nil
class << self
def encode(key, expires_in: nil)
verifier.generate([ key, expires_at(expires_in) ])
end
def decode(encoded_key)
key, expires_at = verifier.verified(encoded_key)
key if key && fresh?(expires_at)
end
private
def expires_at(expires_in)
expires_in ? Time.now.utc.advance(seconds: expires_in) : nil
end
def fresh?(expires_at)
expires_at.nil? || Time.now.utc < expires_at
end
end
end