aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/active_file/verified_key_with_expiration.rb23
1 files changed, 16 insertions, 7 deletions
diff --git a/lib/active_file/verified_key_with_expiration.rb b/lib/active_file/verified_key_with_expiration.rb
index b475f40f40..e9e811d364 100644
--- a/lib/active_file/verified_key_with_expiration.rb
+++ b/lib/active_file/verified_key_with_expiration.rb
@@ -1,15 +1,24 @@
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(seconds: expires_in) : nil ])
- end
+ class << self
+ def encode(key, expires_in: nil)
+ verifier.generate([ key, expires_at(expires_in) ])
+ end
- def self.decode(encoded_key)
- key, expires_at = verifier.verified(encoded_key)
+ def decode(encoded_key)
+ key, expires_at = verifier.verified(encoded_key)
- if key
- key if expires_at.nil? || Time.now.utc < expires_at
+ 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