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 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))
69 ApiAbility.new(current_user)
73 def deny_access(_exception)
74 if doorkeeper_token || current_token
76 report_error t("oauth.permissions.missing"), :forbidden
80 realm = "Web Password"
81 errormessage = "Couldn't authenticate you"
82 response.headers["WWW-Authenticate"] = "Basic realm=\"#{realm}\""
83 render :plain => errormessage, :status => :unauthorized
88 status = database_status
89 status = "offline" if status == "online" && Settings.status == "gpx_offline"
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
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
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
111 User.authenticate(:username => username, :password => passwd) # basic auth
115 # have we identified the user?
117 # check if the user has been banned
118 user_block = current_user.blocks.active.take
119 unless user_block.nil?
121 if user_block.zero_hour?
122 report_error t("application.setup_user_auth.blocked_zero_hour"), :forbidden
124 report_error t("application.setup_user_auth.blocked"), :forbidden
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?
133 report_error t("application.setup_user_auth.need_to_see_terms"), :forbidden