]> git.openstreetmap.org Git - rails.git/blob - app/models/client_application.rb
Fix rubocop warnings
[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 :key, :presence => true, :uniqueness => true
11   validates :name, :url, :secret, :presence => true
12   validates :url, :format => %r{\Ahttp(s?)://(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(/|/([\w#!:.?+=&%@!\-/]))?}i
13   validates :support_url, :allow_blank => true, :format => %r{\Ahttp(s?)://(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(/|/([\w#!:.?+=&%@!\-/]))?}i
14   validates :callback_url, :allow_blank => true, :format => %r{\A[a-z][a-z0-9.+-]*://(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(/|/([\w#!:.?+=&%@!\-/]))?}i
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     token if token && token.authorized?
23   end
24
25   def self.verify_request(request, options = {}, &block)
26     signature = OAuth::Signature.build(request, options, &block)
27     return false unless OauthNonce.remember(signature.request.nonce, signature.request.timestamp)
28     value = signature.verify
29     value
30   rescue OAuth::Signature::UnknownSignatureMethod
31     false
32   end
33
34   def self.all_permissions
35     PERMISSIONS
36   end
37
38   def oauth_server
39     @oauth_server ||= OAuth::Server.new("http://" + SERVER_URL)
40   end
41
42   def credentials
43     @oauth_client ||= OAuth::Consumer.new(key, secret)
44   end
45
46   def create_request_token(params = {})
47     params = { :client_application => self, :callback_url => token_callback_url }
48     permissions.each do |p|
49       params[p] = true
50     end
51     RequestToken.create(params)
52   end
53
54   def access_token_for_user(user)
55     unless token = access_tokens.valid.find_by(:user_id => user)
56       params = { :user => user }
57
58       permissions.each do |p|
59         params[p] = true
60       end
61
62       token = access_tokens.create(params)
63     end
64
65     token
66   end
67
68   # the permissions that this client would like from the user
69   def permissions
70     ClientApplication.all_permissions.select { |p| self[p] }
71   end
72
73   protected
74
75   # this is the set of permissions that the client can ask for. clients
76   # have to say up-front what permissions they want and when users sign up they
77   # can agree or not agree to each of them.
78   PERMISSIONS = [:allow_read_prefs, :allow_write_prefs, :allow_write_diary,
79                  :allow_write_api, :allow_read_gpx, :allow_write_gpx,
80                  :allow_write_notes].freeze
81
82   def generate_keys
83     self.key = OAuth::Helper.generate_key(40)[0, 40]
84     self.secret = OAuth::Helper.generate_key(40)[0, 40]
85   end
86 end