Module: Oauth2Token

Included in:
AccessToken, RefreshToken
Defined in:
lib/couchdb_oauth2/model/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
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/couchdb_oauth2/model/oauth2_token.rb', line 2

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

    belongs_to :account, :class_name => Rack::CouchdbOAuth2::Configuration..to_s
    belongs_to :client
    
    property  :token, String
    property :expires_at, Time
    
    view_by :expires_at
    view_by :account_id
    view_by :client_id
    view_by :token
    
    before_validation :setup, :on => :create
    validates :client, :expires_at, :account, :presence => true
    validates :token, :presence => true, :uniqueness => true      
    
    def self.valid
      view(:by_expires_at, :startkey => Time.now.utc)
    end

    def self.find_by_token(token)
      return nil if token.nil? || token.empty?
      self.first_from_view(:by_token, token)
    end
    
  end
end

Instance Method Details

#expired!Object



38
39
40
41
# File 'lib/couchdb_oauth2/model/oauth2_token.rb', line 38

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

#expired?Boolean



43
44
45
# File 'lib/couchdb_oauth2/model/oauth2_token.rb', line 43

def expired?
  self.expires_at < Time.now.utc
end

#expires_inObject



34
35
36
# File 'lib/couchdb_oauth2/model/oauth2_token.rb', line 34

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