Module: Oauth2Token

Included in:
AccessToken, AuthorizationCode, RefreshToken
Defined in:
lib/oauth2_token.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/oauth2_token.rb', line 2

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

Instance Method Details

#expired!Object



25
26
27
28
# File 'lib/oauth2_token.rb', line 25

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

#expires_inObject



21
22
23
# File 'lib/oauth2_token.rb', line 21

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