]> git.openstreetmap.org Git - rails.git/blob - app/models/client_application.rb
Display the changeset comment (if any) as part of the description of
[rails.git] / app / models / client_application.rb
1 require 'oauth'
2 class ClientApplication < ActiveRecord::Base
3   belongs_to :user
4   has_many :tokens, :class_name => "OauthToken"
5   validates_presence_of :name, :url, :key, :secret
6   validates_uniqueness_of :key
7   before_validation_on_create :generate_keys
8   
9   def self.verify_request(request, options = {}, &block)
10     begin
11       signature = OAuth::Signature.build(request, options, &block)
12       logger.info "Signature Base String: #{signature.signature_base_string}"
13       logger.info "Consumer: #{signature.send :consumer_key}"
14       logger.info "Token: #{signature.send :token}"
15       return false unless OauthNonce.remember(signature.request.nonce, signature.request.timestamp)
16       value = signature.verify
17       logger.info "Signature verification returned: #{value.to_s}"
18       value
19     rescue OAuth::Signature::UnknownSignatureMethod => e
20       logger.info "ERROR"+e.to_s
21       false
22     end
23   end
24   
25   def self.all_permissions
26     PERMISSIONS
27   end
28
29   def oauth_server
30     @oauth_server ||= OAuth::Server.new("http://" + SERVER_URL)
31   end
32   
33   def credentials
34     @oauth_client ||= OAuth::Consumer.new(key, secret)
35   end
36     
37   def create_request_token
38     RequestToken.create :client_application => self
39   end
40
41   # the permissions that this client would like from the user
42   def permissions
43     ClientApplication.all_permissions.select { |p| self[p] }
44   end
45
46 protected
47   
48   # this is the set of permissions that the client can ask for. clients
49   # have to say up-front what permissions they want and when users sign up they
50   # can agree or not agree to each of them.
51   PERMISSIONS = [:allow_read_prefs, :allow_write_prefs, :allow_write_diary,
52                  :allow_write_api, :allow_read_gpx, :allow_write_gpx ]
53
54   def generate_keys
55     @oauth_client = oauth_server.generate_consumer_credentials
56     self.key = @oauth_client.key
57     self.secret = @oauth_client.secret
58   end
59 end