Sha256: a981841adcd5d5752d4f43e7cfe84446fd655ece7ef143ff6d6648d74dcd61e3

Contents?: true

Size: 849 Bytes

Versions: 7

Compression:

Stored size: 849 Bytes

Contents

module ExpirableToken
  def self.included(klass)
    klass.class_eval do
      cattr_accessor :default_lifetime
      self.default_lifetime = 1.minute

      belongs_to :user
      belongs_to :client

      before_validation :setup, :on => :create
      validates :client, :expires_at, :presence => true
      validates :token, :presence => true, :uniqueness => true

      # TODO: this should be a default scope once rails default_scope supports lambda's
      scope :valid, lambda {
        where(self.arel_table[:expires_at].gteq(Time.now.utc))
      }
    end
  end

  def expires_in
    (expires_at - Time.now.utc).to_i
  end

  def expired!
    self.expires_at = Time.now.utc
    self.save!
  end

  private

  def setup
    self.token = ActiveSupport::SecureRandom.base64(16)
    self.expires_at ||= self.default_lifetime.from_now
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
devise_oauth2_providable-0.1.6 lib/expirable_token.rb
devise_oauth2_providable-0.1.5 lib/expirable_token.rb
devise_oauth2_providable-0.1.4 lib/expirable_token.rb
devise_oauth2_providable-0.1.3 lib/expirable_token.rb
devise_oauth2_providable-0.1.2 lib/expirable_token.rb
devise_oauth2_providable-0.1.1 lib/expirable_token.rb
devise_oauth2_providable-0.1.0 lib/expirable_token.rb