aboutsummaryrefslogblamecommitdiffstats
path: root/lib/active_file/verified_key_with_expiration.rb
blob: 601401278b4ae61ac66bccb39d00e93c5467449b (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15














                                                                                                              
class ActiveFile::VerifiedKeyWithExpiration
  class_attribute :verifier, default: defined?(Rails) ? Rails.application.message_verifier('ActiveFile') : nil

  def self.encode(key, expires_in: nil)
    verifier.generate([ key, expires_in ? Time.now.utc.advance(sec: expires_in) : nil ])
  end

  def self.decode(encoded_key)
    key, expires_at = verifier.verified(encoded_key)

    if key
      key if expires_at.nil? || Time.now.utc < expires_at
    end
  end
end