]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api_controller.rb
Fix new rubocop warnings
[rails.git] / app / controllers / api_controller.rb
1 class ApiController < ApplicationController
2   skip_before_action :verify_authenticity_token
3
4   private
5
6   ##
7   # Set allowed request formats if no explicit format has been
8   # requested via a URL suffix. Allowed formats are taken from
9   # any HTTP Accept header with XML as the default.
10   def set_request_formats
11     unless params[:format]
12       accept_header = request.headers["HTTP_ACCEPT"]
13
14       if accept_header
15         # Some clients (such asJOSM) send Accept headers which cannot be
16         # parse by Rails, for example:
17         #
18         #   Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
19         #
20         # where both "*" and ".2" as a quality do not adhere to the syntax
21         # described in RFC 7231, section 5.3.1, etc.
22         #
23         # As a workaround, and for back compatibility, default to XML format.
24         mimetypes = begin
25           Mime::Type.parse(accept_header)
26         rescue Mime::Type::InvalidMimeType
27           Array(Mime[:xml])
28         end
29
30         # Allow XML and JSON formats, and treat an all formats wildcard
31         # as XML for backwards compatibility - all other formats are discarded
32         # which will result in a 406 Not Acceptable response being sent
33         formats = mimetypes.map do |mime|
34           if mime.symbol == :xml then :xml
35           elsif mime.symbol == :json then :json
36           elsif mime == "*/*" then :xml
37           end
38         end
39       else
40         # Default to XML if no accept header was sent - this includes
41         # the unit tests which don't set one by default
42         formats = Array(:xml)
43       end
44
45       request.formats = formats.compact
46     end
47   end
48
49   def authorize(realm = "Web Password", errormessage = "Couldn't authenticate you")
50     # make the current_user object from any auth sources we have
51     setup_user_auth
52
53     # handle authenticate pass/fail
54     unless current_user
55       # no auth, the user does not exist or the password was wrong
56       response.headers["WWW-Authenticate"] = "Basic realm=\"#{realm}\""
57       render :plain => errormessage, :status => :unauthorized
58       false
59     end
60   end
61
62   def current_ability
63     # Use capabilities from the oauth token if it exists and is a valid access token
64     if doorkeeper_token&.accessible?
65       ApiAbility.new(nil).merge(ApiCapability.new(doorkeeper_token))
66     elsif Authenticator.new(self, [:token]).allow?
67       ApiAbility.new(nil).merge(ApiCapability.new(current_token))
68     else
69       ApiAbility.new(current_user)
70     end
71   end
72
73   def deny_access(_exception)
74     if doorkeeper_token || current_token
75       set_locale
76       report_error t("oauth.permissions.missing"), :forbidden
77     elsif current_user
78       head :forbidden
79     else
80       realm = "Web Password"
81       errormessage = "Couldn't authenticate you"
82       response.headers["WWW-Authenticate"] = "Basic realm=\"#{realm}\""
83       render :plain => errormessage, :status => :unauthorized
84     end
85   end
86
87   def gpx_status
88     status = database_status
89     status = "offline" if status == "online" && Settings.status == "gpx_offline"
90     status
91   end
92
93   ##
94   # sets up the current_user for use by other methods. this is mostly called
95   # from the authorize method, but can be called elsewhere if authorisation
96   # is optional.
97   def setup_user_auth
98     # try and setup using OAuth
99     if doorkeeper_token&.accessible?
100       self.current_user = User.find(doorkeeper_token.resource_owner_id)
101     elsif Authenticator.new(self, [:token]).allow?
102       # self.current_user setup by OAuth
103     else
104       username, passwd = get_auth_data # parse from headers
105       # authenticate per-scheme
106       self.current_user = if username.nil?
107                             nil # no authentication provided - perhaps first connect (client should retry after 401)
108                           elsif username == "token"
109                             User.authenticate(:token => passwd) # preferred - random token for user from db, passed in basic auth
110                           else
111                             User.authenticate(:username => username, :password => passwd) # basic auth
112                           end
113     end
114
115     # have we identified the user?
116     if current_user
117       # check if the user has been banned
118       user_block = current_user.blocks.active.take
119       unless user_block.nil?
120         set_locale
121         if user_block.zero_hour?
122           report_error t("application.setup_user_auth.blocked_zero_hour"), :forbidden
123         else
124           report_error t("application.setup_user_auth.blocked"), :forbidden
125         end
126       end
127
128       # if the user hasn't seen the contributor terms then don't
129       # allow editing - they have to go to the web site and see
130       # (but can decline) the CTs to continue.
131       if !current_user.terms_seen && flash[:skip_terms].nil?
132         set_locale
133         report_error t("application.setup_user_auth.need_to_see_terms"), :forbidden
134       end
135     end
136   end
137 end