lib/fernet/verifier.rb in fernet-2.1 vs lib/fernet/verifier.rb in fernet-2.1.1
- old
+ new
@@ -7,27 +7,24 @@
module Fernet
# Public: verifies Fernet Tokens
class Verifier
class UnknownTokenVersion < Fernet::Error; end
- attr_reader :token
- attr_accessor :ttl, :enforce_ttl
+ attr_reader :token, :enforce_ttl
+ attr_accessor :ttl
# Internal: initializes a Verifier
#
# opts - a hash containing
# * secret - the secret used to create the token (required)
# * token - the fernet token string (required)
# * enforce_ttl - whether to enforce TTL, defaults to Configuration.enforce_ttl
# * ttl - number of seconds the token is valid
def initialize(opts = {})
- enforce_ttl = opts.has_key?(:enforce_ttl) ? opts[:enforce_ttl] : Configuration.enforce_ttl
- @token = Token.new(opts.fetch(:token),
- secret: opts.fetch(:secret),
- enforce_ttl: enforce_ttl,
- ttl: opts[:ttl],
- now: opts[:now])
+ @enforce_ttl = opts.has_key?(:enforce_ttl) ? opts[:enforce_ttl] : Configuration.enforce_ttl
+ @opts = opts
+ create_token!
end
# Public: whether the verifier is valid. A verifier is valid if it's token
# is valid.
#
@@ -52,27 +49,23 @@
def inspect
"#<Fernet::Verifier @secret=[masked] @token=#{@token} @message=#{@message.inspect} @ttl=#{@ttl} @enforce_ttl=#{@enforce_ttl}>"
end
alias to_s inspect
- private
- def must_verify?
- @must_verify || @valid.nil?
+ # Public: sets the enforce_ttl configuration
+ #
+ # * val - whether to enforce TTL, defaults to Configuration.enforce_ttl
+ def enforce_ttl=(val)
+ @enforce_ttl = val
+ create_token!
end
- def acceptable_clock_skew?
- @issued_at < (now + MAX_CLOCK_SKEW)
- end
-
- def signatures_match?
- regenerated_bytes = @regenerated_mac.bytes.to_a
- received_bytes = @received_signature.bytes.to_a
- received_bytes.inject(0) do |accum, byte|
- accum |= byte ^ regenerated_bytes.shift
- end.zero?
- end
-
- def now
- @now ||= Time.now
+ private
+ def create_token!
+ @token = Token.new(@opts.fetch(:token),
+ secret: @opts.fetch(:secret),
+ enforce_ttl: enforce_ttl,
+ ttl: @opts[:ttl],
+ now: @opts[:now])
end
end
end