2 class ClientApplication < ActiveRecord::Base
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
9 validates_format_of :url, :with => /\Ahttp(s?):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/i
10 validates_format_of :support_url, :with => /\Ahttp(s?):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/i, :allow_blank=>true
11 validates_format_of :callback_url, :with => /\Ahttp(s?):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/i, :allow_blank=>true
13 attr_accessor :token_callback_url
15 def self.find_token(token_key)
16 token = OauthToken.find_by_token(token_key, :include => :client_application)
17 if token && token.authorized?
24 def self.verify_request(request, options = {}, &block)
26 signature = OAuth::Signature.build(request, options, &block)
27 logger.info "Signature Base String: #{signature.signature_base_string}"
28 logger.info "Consumer: #{signature.send :consumer_key}"
29 logger.info "Token: #{signature.send :token}"
30 return false unless OauthNonce.remember(signature.request.nonce, signature.request.timestamp)
31 value = signature.verify
32 logger.info "Signature verification returned: #{value.to_s}"
34 rescue OAuth::Signature::UnknownSignatureMethod => e
35 logger.info "ERROR"+e.to_s
40 def self.all_permissions
45 @oauth_server ||= OAuth::Server.new("http://" + SERVER_URL)
49 @oauth_client ||= OAuth::Consumer.new(key, secret)
52 def create_request_token
53 RequestToken.create :client_application => self, :callback_url => self.token_callback_url
56 # the permissions that this client would like from the user
58 ClientApplication.all_permissions.select { |p| self[p] }
63 # this is the set of permissions that the client can ask for. clients
64 # have to say up-front what permissions they want and when users sign up they
65 # can agree or not agree to each of them.
66 PERMISSIONS = [:allow_read_prefs, :allow_write_prefs, :allow_write_diary,
67 :allow_write_api, :allow_read_gpx, :allow_write_gpx ]
70 oauth_client = oauth_server.generate_consumer_credentials
71 self.key = oauth_client.key
72 self.secret = oauth_client.secret