]> git.openstreetmap.org Git - rails.git/blob - app/models/client_application.rb
Return GPX traces as application/gpx+xml instead of text/xml
[rails.git] / app / models / client_application.rb
1 require 'oauth'
2
3 class ClientApplication < ActiveRecord::Base
4   belongs_to :user
5   has_many :tokens, :class_name => "OauthToken", :dependent => :delete_all
6   has_many :access_tokens
7   has_many :oauth2_verifiers
8   has_many :oauth_tokens
9
10   validates_presence_of :name, :url, :key, :secret
11   validates_uniqueness_of :key
12   validates_format_of :url, :with => /\Ahttp(s?):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/i
13   validates_format_of :support_url, :with => /\Ahttp(s?):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/i, :allow_blank=>true
14   validates_format_of :callback_url, :with => /\A[a-z][a-z0-9.+-]*:\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/i, :allow_blank=>true
15
16   attr_accessible :name, :url, :support_url, :callback_url,
17                   :allow_read_prefs, :allow_write_prefs,
18                   :allow_write_diary, :allow_write_api,
19                   :allow_read_gpx, :allow_write_gpx,
20                   :allow_write_notes
21
22   before_validation :generate_keys, :on => :create
23
24   attr_accessor :token_callback_url
25   
26   def self.find_token(token_key)
27     token = OauthToken.find_by_token(token_key, :include => :client_application)
28     if token && token.authorized?
29       token
30     else
31       nil
32     end
33   end
34
35   def self.verify_request(request, options = {}, &block)
36     begin
37       signature = OAuth::Signature.build(request, options, &block)
38       return false unless OauthNonce.remember(signature.request.nonce, signature.request.timestamp)
39       value = signature.verify
40       value
41     rescue OAuth::Signature::UnknownSignatureMethod => e
42       false
43     end
44   end
45   
46   def self.all_permissions
47     PERMISSIONS
48   end
49
50   def oauth_server
51     @oauth_server ||= OAuth::Server.new("http://" + SERVER_URL)
52   end
53   
54   def credentials
55     @oauth_client ||= OAuth::Consumer.new(key, secret)
56   end
57     
58   def create_request_token(params={})
59     params = { :client_application => self, :callback_url => self.token_callback_url }
60     permissions.each do |p|
61       params[p] = true
62     end
63     RequestToken.create(params, :without_protection => true)
64   end
65
66   def access_token_for_user(user)
67     unless token = access_tokens.valid.where(:user_id => user).first
68       params = { :user => user }
69
70       permissions.each do |p|
71         params[p] = true
72       end
73
74       token = access_tokens.create(params, :without_protection => true)
75     end
76     
77     token
78   end
79
80   # the permissions that this client would like from the user
81   def permissions
82     ClientApplication.all_permissions.select { |p| self[p] }
83   end
84
85 protected
86   
87   # this is the set of permissions that the client can ask for. clients
88   # have to say up-front what permissions they want and when users sign up they
89   # can agree or not agree to each of them.
90   PERMISSIONS = [:allow_read_prefs, :allow_write_prefs, :allow_write_diary,
91                  :allow_write_api, :allow_read_gpx, :allow_write_gpx,
92                  :allow_write_notes]
93
94   def generate_keys
95     self.key = OAuth::Helper.generate_key(40)[0,40]
96     self.secret = OAuth::Helper.generate_key(40)[0,40]
97   end
98 end