]> git.openstreetmap.org Git - rails.git/blob - app/models/client_application.rb
Merge remote-tracking branch 'upstream/pull/3264'
[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 class ClientApplication < ApplicationRecord
34   belongs_to :user
35   has_many :tokens, :class_name => "OauthToken", :dependent => :delete_all
36   has_many :access_tokens
37   has_many :oauth2_verifiers
38   has_many :oauth_tokens
39
40   validates :key, :presence => true, :uniqueness => true
41   validates :name, :url, :secret, :presence => true
42   validates :url, :format => /\A#{URI::DEFAULT_PARSER.make_regexp(%w[http https])}\z/
43   validates :support_url, :allow_blank => true, :format => /\A#{URI::DEFAULT_PARSER.make_regexp(%w[http https])}\z/
44   validates :callback_url, :allow_blank => true, :format => /\A#{URI::DEFAULT_PARSER.make_regexp}\z/
45
46   before_validation :generate_keys, :on => :create
47
48   attr_accessor :token_callback_url
49
50   def self.find_token(token_key)
51     token = OauthToken.includes(:client_application).find_by(:token => token_key)
52     token if token&.authorized?
53   end
54
55   def self.verify_request(request, options = {}, &block)
56     signature = OAuth::Signature.build(request, options, &block)
57     return false unless OauthNonce.remember(signature.request.nonce, signature.request.timestamp)
58
59     signature.verify
60   rescue OAuth::Signature::UnknownSignatureMethod
61     false
62   end
63
64   def self.all_permissions
65     Oauth.scopes.collect { |s| :"allow_#{s.name}" }
66   end
67
68   def oauth_server
69     @oauth_server ||= OAuth::Server.new("https://#{Settings.server_url}")
70   end
71
72   def credentials
73     @credentials ||= OAuth::Consumer.new(key, secret)
74   end
75
76   def create_request_token(_params = {})
77     params = { :client_application => self, :callback_url => token_callback_url }
78     permissions.each do |p|
79       params[p] = true
80     end
81     RequestToken.create(params)
82   end
83
84   def access_token_for_user(user)
85     unless token = access_tokens.valid.find_by(:user_id => user)
86       params = { :user => user }
87
88       permissions.each do |p|
89         params[p] = true
90       end
91
92       token = access_tokens.create(params)
93     end
94
95     token
96   end
97
98   # the permissions that this client would like from the user
99   def permissions
100     ClientApplication.all_permissions.select { |p| self[p] }
101   end
102
103   protected
104
105   def generate_keys
106     self.key = OAuth::Helper.generate_key(40)[0, 40]
107     self.secret = OAuth::Helper.generate_key(40)[0, 40]
108   end
109 end