Sha256: cda1e19eee41b031ea94ef58b3f1e6e2796174da0ce1bf96120448ec6edb4159

Contents?: true

Size: 728 Bytes

Versions: 5

Compression:

Stored size: 728 Bytes

Contents

# frozen_string_literal: true

module ApiSignature
  class Validator
    attr_reader :timestamp

    def initialize(options)
      @options = options
      @timestamp = Time.zone.at(@options[:timestamp].to_i)
    end

    def valid?(signature, secret)
      return false if signature.blank? || secret.blank? || expired?
      generator.generate_signature(secret) == signature
    end

    def expired?
      !alive?
    end

    private

    def generator
      @generator ||= Generator.new(@options)
    end

    def alive?
      alive_timerange.cover?(timestamp)
    end

    def alive_timerange
      @alive_timerange ||= (ttl.ago..ttl.from_now)
    end

    def ttl
      ApiSignature.signature_ttl || TTL
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
api_signature-0.1.4 lib/api_signature/validator.rb
api_signature-0.1.3 lib/api_signature/validator.rb
api_signature-0.1.2 lib/api_signature/validator.rb
api_signature-0.1.1 lib/api_signature/validator.rb
api_signature-0.1.0 lib/api_signature/validator.rb