1 class ApiController < ApplicationController
2 skip_before_action :verify_authenticity_token
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"]
15 # Some clients (such asJOSM) send Accept headers which cannot be
16 # parse by Rails, for example:
18 # Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
20 # where both "*" and ".2" as a quality do not adhere to the syntax
21 # described in RFC 7231, section 5.3.1, etc.
23 # As a workaround, and for back compatibility, default to XML format.
25 Mime::Type.parse(accept_header)
26 rescue Mime::Type::InvalidMimeType
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
40 # Default to XML if no accept header was sent - this includes
41 # the unit tests which don't set one by default
45 request.formats = formats.compact
49 def authorize(realm = "Web Password", errormessage = "Couldn't authenticate you")
50 # make the current_user object from any auth sources we have
53 # handle authenticate pass/fail
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
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))
67 ApiAbility.new(current_user)
71 def deny_access(_exception)
74 report_error t("oauth.permissions.missing"), :forbidden
78 realm = "Web Password"
79 errormessage = "Couldn't authenticate you"
80 response.headers["WWW-Authenticate"] = "Basic realm=\"#{realm}\""
81 render :plain => errormessage, :status => :unauthorized
86 status = database_status
87 status = "offline" if status == "online" && Settings.status == "gpx_offline"
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
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
105 User.authenticate(:username => username, :password => passwd) # basic auth
109 # have we identified the user?
111 # check if the user has been banned
112 user_block = current_user.blocks.active.take
113 unless user_block.nil?
115 if user_block.zero_hour?
116 report_error t("application.setup_user_auth.blocked_zero_hour"), :forbidden
118 report_error t("application.setup_user_auth.blocked"), :forbidden
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?
127 report_error t("application.setup_user_auth.need_to_see_terms"), :forbidden