Sha256: 3b0d20503913ccebf72821341a1c69ebf4288e6509f840335cff754dc834da34

Contents?: true

Size: 1.64 KB

Versions: 1

Compression:

Stored size: 1.64 KB

Contents

#encoding UTF-8
require 'base64'
require 'openssl'
require 'date'

module Fernet
  class Verifier
    class UnknownTokenVersion < RuntimeError; end

    attr_reader :token
    attr_accessor :ttl, :enforce_ttl

    def initialize(opts = {})
      enforce_ttl = opts.has_key?(:enforce_ttl) ? opts[:enforce_ttl] : Configuration.enforce_ttl
      @token = Token.new(opts.fetch(:token),
                           enforce_ttl: enforce_ttl,
                           ttl: opts[:ttl],
                           now: opts[:now])
      @token.secret = opts.fetch(:secret)
    end

    def valid?
      @token.valid?
    end

    def message
      @token.message
    end

    def data
      puts "[WARNING] data is deprected. Use message instead"
      message
    end

    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?
    end

    def token_recent_enough?
      if enforce_ttl?
        good_till = @issued_at + (ttl.to_f / 24 / 60 / 60)
        (good_till.to_i >= now.to_i) && acceptable_clock_skew?
      else
        true
      end
    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 enforce_ttl?
      @enforce_ttl
    end

    def now
      @now ||= Time.now
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fernet-2.0.rc1 lib/fernet/verifier.rb