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