]> git.openstreetmap.org Git - rails.git/blob - app/models/oauth_token.rb
Extract common code for parsing floats
[rails.git] / app / models / oauth_token.rb
1 class OauthToken < ActiveRecord::Base
2   belongs_to :client_application
3   belongs_to :user
4
5   scope :authorized, where("authorized_at IS NOT NULL and invalidated_at IS NULL")
6
7   validates_uniqueness_of :token
8   validates_presence_of :client_application, :token
9
10   before_validation :generate_keys, :on => :create
11   
12   def invalidated?
13     invalidated_at != nil
14   end
15   
16   def invalidate!
17     update_attributes({
18       :invalidated_at => Time.now
19     }, :without_protection => true)
20   end
21   
22   def authorized?
23     authorized_at != nil && !invalidated?
24   end
25   
26   def to_query
27     "oauth_token=#{token}&oauth_token_secret=#{secret}"
28   end
29     
30 protected
31   
32   def generate_keys
33     self.token = OAuth::Helper.generate_key(40)[0,40]
34     self.secret = OAuth::Helper.generate_key(40)[0,40]
35   end
36 end