Sha256: c9156fd4f56c29fc2053a1114599358927082559b99b0685e98a6a0e674d42d7

Contents?: true

Size: 1.52 KB

Versions: 17

Compression:

Stored size: 1.52 KB

Contents

require 'active_support/concern'
require 'active_record'

module Devise
  module Oauth2
    module ExpirableToken
      extend ActiveSupport::Concern

      module ClassMethods
        def expires_according_to(config_name)
          cattr_accessor :default_lifetime
          self.default_lifetime = Rails.application.config.devise_oauth2_rails4[config_name]

          belongs_to :user
          belongs_to :client

          # attr_accessible :user, :client

          after_initialize :init_token, :on => :create, :unless => :token?
          after_initialize :init_expires_at, :on => :create, :unless => :expires_at?
          validates :expires_at, :presence => true
          validates :client, :presence => true
          validates :token, :presence => true, :uniqueness => true

          default_scope lambda {
            where(self.arel_table[:expires_at].gteq(Time.now.utc))
          }

          include LocalInstanceMethods
        end
      end

      module LocalInstanceMethods
        # number of seconds until the token expires
        def expires_in
          (expires_at - Time.now.utc).to_i
        end

        # forcefully expire the token
        def expired!
          self.expires_at = Time.now.utc
          self.save!
        end

        private

        def init_token
          self.token = Devise::Oauth2.random_id
        end
        def init_expires_at
          self.expires_at = self.default_lifetime.from_now
        end
      end
    end
  end
end

ActiveRecord::Base.send :include, Devise::Oauth2::ExpirableToken

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
devise_oauth2_rails4-2.1.8 lib/devise/oauth2/expirable_token.rb
devise_oauth2_rails4-2.1.7 lib/devise/oauth2/expirable_token.rb
devise_oauth2_rails4-2.1.6 lib/devise/oauth2/expirable_token.rb
devise_oauth2_rails4-2.1.5 lib/devise/oauth2/expirable_token.rb
devise_oauth2_rails4-2.1.4 lib/devise/oauth2/expirable_token.rb
devise_oauth2_rails4-2.1.3 lib/devise/oauth2/expirable_token.rb
devise_oauth2_rails4-2.0.3 lib/devise/oauth2/expirable_token.rb
devise_oauth2_rails4-2.1.0 lib/devise/oauth2/expirable_token.rb
devise_oauth2_rails4-2.0.2 lib/devise/oauth2/expirable_token.rb
devise_oauth2_rails4-2.0.1 lib/devise/oauth2/expirable_token.rb
devise_oauth2_rails4-2.0.0 lib/devise/oauth2/expirable_token.rb
devise_oauth2_rails4-1.1.8 lib/devise/oauth2/expirable_token.rb
devise_oauth2_rails4-1.1.7 lib/devise/oauth2/expirable_token.rb
devise_oauth2_rails4-1.1.6 lib/devise/oauth2/expirable_token.rb
devise_oauth2_rails4-1.1.5 lib/devise/oauth2/expirable_token.rb
devise_oauth2_rails4-1.1.4 lib/devise/oauth2/expirable_token.rb
devise_oauth2_rails4-1.1.3 lib/devise/oauth2/expirable_token.rb