]> git.openstreetmap.org Git - rails.git/blob - app/models/client_application.rb
Merge pull request #1732 from tomhughes/npemap
[rails.git] / app / models / client_application.rb
1 # == Schema Information
2 #
3 # Table name: client_applications
4 #
5 #  id                :integer          not null, primary key
6 #  name              :string
7 #  url               :string
8 #  support_url       :string
9 #  callback_url      :string
10 #  key               :string(50)
11 #  secret            :string(50)
12 #  user_id           :integer
13 #  created_at        :datetime
14 #  updated_at        :datetime
15 #  allow_read_prefs  :boolean          default(FALSE), not null
16 #  allow_write_prefs :boolean          default(FALSE), not null
17 #  allow_write_diary :boolean          default(FALSE), not null
18 #  allow_write_api   :boolean          default(FALSE), not null
19 #  allow_read_gpx    :boolean          default(FALSE), not null
20 #  allow_write_gpx   :boolean          default(FALSE), not null
21 #  allow_write_notes :boolean          default(FALSE), not null
22 #
23 # Indexes
24 #
25 #  index_client_applications_on_key      (key) UNIQUE
26 #  index_client_applications_on_user_id  (user_id)
27 #
28 # Foreign Keys
29 #
30 #  client_applications_user_id_fkey  (user_id => users.id)
31 #
32
33 require "oauth"
34
35 class ClientApplication < ActiveRecord::Base
36   belongs_to :user
37   has_many :tokens, :class_name => "OauthToken", :dependent => :delete_all
38   has_many :access_tokens
39   has_many :oauth2_verifiers
40   has_many :oauth_tokens
41
42   validates :key, :presence => true, :uniqueness => true
43   validates :name, :url, :secret, :presence => true
44   validates :url, :format => %r{\Ahttp(s?)://(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(/|/([\w#!:.?+=&%@!\-/]))?}i
45   validates :support_url, :allow_blank => true, :format => %r{\Ahttp(s?)://(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(/|/([\w#!:.?+=&%@!\-/]))?}i
46   validates :callback_url, :allow_blank => true, :format => %r{\A[a-z][a-z0-9.+-]*://(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(/|/([\w#!:.?+=&%@!\-/]))?}i
47
48   before_validation :generate_keys, :on => :create
49
50   attr_accessor :token_callback_url
51
52   def self.find_token(token_key)
53     token = OauthToken.includes(:client_application).find_by(:token => token_key)
54     token if token && token.authorized?
55   end
56
57   def self.verify_request(request, options = {}, &block)
58     signature = OAuth::Signature.build(request, options, &block)
59     return false unless OauthNonce.remember(signature.request.nonce, signature.request.timestamp)
60     value = signature.verify
61     value
62   rescue OAuth::Signature::UnknownSignatureMethod
63     false
64   end
65
66   def self.all_permissions
67     PERMISSIONS
68   end
69
70   def oauth_server
71     @oauth_server ||= OAuth::Server.new("https://" + SERVER_URL)
72   end
73
74   def credentials
75     @oauth_client ||= OAuth::Consumer.new(key, secret)
76   end
77
78   def create_request_token(_params = {})
79     params = { :client_application => self, :callback_url => token_callback_url }
80     permissions.each do |p|
81       params[p] = true
82     end
83     RequestToken.create(params)
84   end
85
86   def access_token_for_user(user)
87     unless token = access_tokens.valid.find_by(:user_id => user)
88       params = { :user => user }
89
90       permissions.each do |p|
91         params[p] = true
92       end
93
94       token = access_tokens.create(params)
95     end
96
97     token
98   end
99
100   # the permissions that this client would like from the user
101   def permissions
102     ClientApplication.all_permissions.select { |p| self[p] }
103   end
104
105   protected
106
107   # this is the set of permissions that the client can ask for. clients
108   # have to say up-front what permissions they want and when users sign up they
109   # can agree or not agree to each of them.
110   PERMISSIONS = [:allow_read_prefs, :allow_write_prefs, :allow_write_diary, :allow_write_api, :allow_read_gpx, :allow_write_gpx, :allow_write_notes].freeze
111
112   def generate_keys
113     self.key = OAuth::Helper.generate_key(40)[0, 40]
114     self.secret = OAuth::Helper.generate_key(40)[0, 40]
115   end
116 end