Sha256: 30b3ae78e910f7a8fdac2b64adcac854f86b751526779491ea783589678c6990

Contents?: true

Size: 963 Bytes

Versions: 2

Compression:

Stored size: 963 Bytes

Contents

require 'time'
require 'active_support/hash_with_indifferent_access'
require 'active_support/core_ext/time/calculations'

module OpenToken
  CLOCK_SKEW_TOLERANCE = 5 # in seconds

  class TokenExpiredError < StandardError;  end

  class Token < ActiveSupport::HashWithIndifferentAccess
    def validate!
      raise OpenToken::TokenExpiredError.new("#{Time.now.utc} is not within token duration: #{self.start_at} - #{self.end_at}") if self.expired?
    end
    #verify that the current time is between the not-before and not-on-or-after values
    def valid?
      (start_at - CLOCK_SKEW_TOLERANCE).past? && (end_at + CLOCK_SKEW_TOLERANCE).future?
    end
    def expired?
      !valid?
    end
    def start_at
      payload_date('not-before')
    end
    def end_at
      payload_date('not-on-or-after')
    end
    def valid_until
      payload_date('renew-until')
    end

    private
    def payload_date(key)
      Time.iso8601(self[key])
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
opentoken-1.2.5 lib/opentoken/token.rb
opentoken-1.2.3 lib/opentoken/token.rb