]> git.openstreetmap.org Git - rails.git/blob - app/models/client_application.rb
Disable the donation drive message
[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"
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   before_validation :generate_keys, :on => :create
17
18   attr_accessor :token_callback_url
19   
20   def self.find_token(token_key)
21     token = OauthToken.find_by_token(token_key, :include => :client_application)
22     if token && token.authorized?
23       token
24     else
25       nil
26     end
27   end
28
29   def self.verify_request(request, options = {}, &block)
30     begin
31       signature = OAuth::Signature.build(request, options, &block)
32       return false unless OauthNonce.remember(signature.request.nonce, signature.request.timestamp)
33       value = signature.verify
34       value
35     rescue OAuth::Signature::UnknownSignatureMethod => e
36       false
37     end
38   end
39   
40   def self.all_permissions
41     PERMISSIONS
42   end
43
44   def oauth_server
45     @oauth_server ||= OAuth::Server.new("http://" + SERVER_URL)
46   end
47   
48   def credentials
49     @oauth_client ||= OAuth::Consumer.new(key, secret)
50   end
51     
52   def create_request_token(params={})
53     params = { :client_application => self, :callback_url => self.token_callback_url }
54     permissions.each do |p|
55       params[p] = true
56     end
57     RequestToken.create(params)
58   end
59
60   def access_token_for_user(user)
61     unless token = access_tokens.valid.where(:user_id => user).first
62       params = { :user => user }
63
64       permissions.each do |p|
65         params[p] = true
66       end
67
68       token = access_tokens.create(params)
69     end
70     
71     token
72   end
73
74   # the permissions that this client would like from the user
75   def permissions
76     ClientApplication.all_permissions.select { |p| self[p] }
77   end
78
79 protected
80   
81   # this is the set of permissions that the client can ask for. clients
82   # have to say up-front what permissions they want and when users sign up they
83   # can agree or not agree to each of them.
84   PERMISSIONS = [:allow_read_prefs, :allow_write_prefs, :allow_write_diary,
85                  :allow_write_api, :allow_read_gpx, :allow_write_gpx ]
86
87   def generate_keys
88     self.key = OAuth::Helper.generate_key(40)[0,40]
89     self.secret = OAuth::Helper.generate_key(40)[0,40]
90   end
91 end