]> git.openstreetmap.org Git - rails.git/blob - app/models/oauth_token.rb
Improve formatting of note descriptions in GPX and RSS responses
[rails.git] / app / models / oauth_token.rb
1 class OauthToken < ActiveRecord::Base
2   belongs_to :client_application
3   belongs_to :user
4   validates_uniqueness_of :token
5   validates_presence_of :client_application, :token, :secret
6   before_validation_on_create :generate_keys
7   
8   def self.find_token(token_key)
9     token = OauthToken.find_by_token(token_key, :include => :client_application)
10     if token && token.authorized?
11       logger.info "Loaded #{token.token} which was authorized by (user_id=#{token.user_id}) on the #{token.authorized_at}"
12       token
13     else
14       nil
15     end
16   end
17   
18   def invalidated?
19     invalidated_at != nil
20   end
21   
22   def invalidate!
23     update_attribute(:invalidated_at, Time.now)
24   end
25   
26   def authorized?
27     authorized_at != nil && !invalidated?
28   end
29   
30   def to_query
31     "oauth_token=#{token}&oauth_token_secret=#{secret}"
32   end
33     
34 protected
35   
36   def generate_keys
37     @oauth_token = client_application.oauth_server.generate_credentials
38     self.token = @oauth_token[0]
39     self.secret = @oauth_token[1]
40   end
41 end