]> git.openstreetmap.org Git - rails.git/blob - app/models/oauth_token.rb
Serve swfobject via the assets pipeline
[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, :secret
9
10   before_validation :generate_keys, :on => :create
11   
12   def self.find_token(token_key)
13     token = OauthToken.find_by_token(token_key, :include => :client_application)
14     if token && token.authorized?
15       logger.info "Loaded #{token.token} which was authorized by (user_id=#{token.user_id}) on the #{token.authorized_at}"
16       token
17     else
18       nil
19     end
20   end
21   
22   def invalidated?
23     invalidated_at != nil
24   end
25   
26   def invalidate!
27     update_attribute(:invalidated_at, Time.now)
28   end
29   
30   def authorized?
31     authorized_at != nil && !invalidated?
32   end
33   
34   def to_query
35     "oauth_token=#{token}&oauth_token_secret=#{secret}"
36   end
37     
38 protected
39   
40   def generate_keys
41     @oauth_token = client_application.oauth_server.generate_credentials
42     self.token = @oauth_token[0]
43     self.secret = @oauth_token[1]
44   end
45 end