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 || mime == "*/*" then :xml
35 elsif mime.symbol == :json then :json
39 # Default to XML if no accept header was sent - this includes
40 # the unit tests which don't set one by default
44 request.formats = formats.compact
48 def authorize(realm = "Web Password", errormessage = "Couldn't authenticate you")
49 # make the current_user object from any auth sources we have
52 # handle authenticate pass/fail
54 # no auth, the user does not exist or the password was wrong
55 response.headers["WWW-Authenticate"] = "Basic realm=\"#{realm}\""
56 render :plain => errormessage, :status => :unauthorized
62 # Use capabilities from the oauth token if it exists and is a valid access token
63 if doorkeeper_token&.accessible?
64 ApiAbility.new(nil).merge(ApiCapability.new(doorkeeper_token))
65 elsif Authenticator.new(self, [:token]).allow?
66 ApiAbility.new(nil).merge(ApiCapability.new(current_token))
68 ApiAbility.new(current_user)
72 def deny_access(_exception)
73 if doorkeeper_token || current_token
75 report_error t("oauth.permissions.missing"), :forbidden
79 realm = "Web Password"
80 errormessage = "Couldn't authenticate you"
81 response.headers["WWW-Authenticate"] = "Basic realm=\"#{realm}\""
82 render :plain => errormessage, :status => :unauthorized
87 status = database_status
88 status = "offline" if status == "online" && Settings.status == "gpx_offline"
93 # sets up the current_user for use by other methods. this is mostly called
94 # from the authorize method, but can be called elsewhere if authorisation
97 # try and setup using OAuth
98 if doorkeeper_token&.accessible?
99 self.current_user = User.find(doorkeeper_token.resource_owner_id)
100 elsif Authenticator.new(self, [:token]).allow?
101 # self.current_user setup by OAuth
103 username, passwd = auth_data # parse from headers
104 # authenticate per-scheme
105 self.current_user = if username.nil?
106 nil # no authentication provided - perhaps first connect (client should retry after 401)
107 elsif username == "token"
108 User.authenticate(:token => passwd) # preferred - random token for user from db, passed in basic auth
110 User.authenticate(:username => username, :password => passwd) # basic auth
114 # have we identified the user?
116 # check if the user has been banned
117 user_block = current_user.blocks.active.take
118 unless user_block.nil?
120 if user_block.zero_hour?
121 report_error t("application.setup_user_auth.blocked_zero_hour"), :forbidden
123 report_error t("application.setup_user_auth.blocked"), :forbidden
127 # if the user hasn't seen the contributor terms then don't
128 # allow editing - they have to go to the web site and see
129 # (but can decline) the CTs to continue.
130 if !current_user.terms_seen && flash[:skip_terms].nil?
132 report_error t("application.setup_user_auth.need_to_see_terms"), :forbidden