]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api_controller.rb
Merge remote-tracking branch 'upstream/pull/3147'
[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 Authenticator.new(self, [:token]).allow?
65       ApiAbility.new(nil).merge(ApiCapability.new(current_token))
66     else
67       ApiAbility.new(current_user)
68     end
69   end
70
71   def deny_access(_exception)
72     if current_token
73       set_locale
74       report_error t("oauth.permissions.missing"), :forbidden
75     elsif current_user
76       head :forbidden
77     else
78       realm = "Web Password"
79       errormessage = "Couldn't authenticate you"
80       response.headers["WWW-Authenticate"] = "Basic realm=\"#{realm}\""
81       render :plain => errormessage, :status => :unauthorized
82     end
83   end
84
85   def gpx_status
86     status = database_status
87     status = "offline" if status == "online" && Settings.status == "gpx_offline"
88     status
89   end
90
91   ##
92   # sets up the current_user for use by other methods. this is mostly called
93   # from the authorize method, but can be called elsewhere if authorisation
94   # is optional.
95   def setup_user_auth
96     # try and setup using OAuth
97     unless Authenticator.new(self, [:token]).allow?
98       username, passwd = get_auth_data # parse from headers
99       # authenticate per-scheme
100       self.current_user = if username.nil?
101                             nil # no authentication provided - perhaps first connect (client should retry after 401)
102                           elsif username == "token"
103                             User.authenticate(:token => passwd) # preferred - random token for user from db, passed in basic auth
104                           else
105                             User.authenticate(:username => username, :password => passwd) # basic auth
106                           end
107     end
108
109     # have we identified the user?
110     if current_user
111       # check if the user has been banned
112       user_block = current_user.blocks.active.take
113       unless user_block.nil?
114         set_locale
115         if user_block.zero_hour?
116           report_error t("application.setup_user_auth.blocked_zero_hour"), :forbidden
117         else
118           report_error t("application.setup_user_auth.blocked"), :forbidden
119         end
120       end
121
122       # if the user hasn't seen the contributor terms then don't
123       # allow editing - they have to go to the web site and see
124       # (but can decline) the CTs to continue.
125       if !current_user.terms_seen && flash[:skip_terms].nil?
126         set_locale
127         report_error t("application.setup_user_auth.need_to_see_terms"), :forbidden
128       end
129     end
130   end
131 end