1 class ApiController < ApplicationController
2 skip_before_action :verify_authenticity_token
7 # Set default request format to xml unless a client requests a specific format,
8 # which can be done via (a) URL suffix and/or (b) HTTP Accept header, where
9 # the URL suffix always takes precedence over the Accept header.
10 def set_default_request_format
11 unless params[:format]
12 accept_header = request.headers["HTTP_ACCEPT"]
14 # e.g. unit tests don't set an Accept: header by default, force XML in this case
15 request.format = "xml"
21 # Some clients (JOSM) send Accept headers which cannot be parsed by Rails, example: *; q=.2
22 # To be fair, JOSM's Accept header doesn't adhere to RFC 7231, section 5.3.1, et al. either
23 # As a workaround for backwards compatibility, we're assuming XML format
25 req_mimetypes = Mime::Type.parse(accept_header)
26 rescue Mime::Type::InvalidMimeType
27 request.format = "xml"
31 # req_mimetypes contains all Accept header MIME types with descending priority
32 req_mimetypes.each do |mime|
33 if mime.symbol == :xml
34 request.format = "xml"
38 if mime.symbol == :json
39 request.format = "json"
43 # Any format, not explicitly requesting XML or JSON -> assume XML as default
45 request.format = "xml"
49 # In case the client requests some other format besides XML, JSON and */*,
50 # we deliberately don't set request.format. The framework will return an
51 # ActionController::UnknownFormat error to the client later on in this case.
56 def authorize(realm = "Web Password", errormessage = "Couldn't authenticate you")
57 # make the current_user object from any auth sources we have
60 # handle authenticate pass/fail
62 # no auth, the user does not exist or the password was wrong
63 response.headers["WWW-Authenticate"] = "Basic realm=\"#{realm}\""
64 render :plain => errormessage, :status => :unauthorized
70 # Use capabilities from the oauth token if it exists and is a valid access token
71 if Authenticator.new(self, [:token]).allow?
72 ApiAbility.new(nil).merge(ApiCapability.new(current_token))
74 ApiAbility.new(current_user)
78 def deny_access(_exception)
81 report_error t("oauth.permissions.missing"), :forbidden
85 realm = "Web Password"
86 errormessage = "Couldn't authenticate you"
87 response.headers["WWW-Authenticate"] = "Basic realm=\"#{realm}\""
88 render :plain => errormessage, :status => :unauthorized
93 status = database_status
94 status = "offline" if status == "online" && Settings.status == "gpx_offline"
99 # sets up the current_user for use by other methods. this is mostly called
100 # from the authorize method, but can be called elsewhere if authorisation
103 # try and setup using OAuth
104 unless Authenticator.new(self, [:token]).allow?
105 username, passwd = get_auth_data # parse from headers
106 # authenticate per-scheme
107 self.current_user = if username.nil?
108 nil # no authentication provided - perhaps first connect (client should retry after 401)
109 elsif username == "token"
110 User.authenticate(:token => passwd) # preferred - random token for user from db, passed in basic auth
112 User.authenticate(:username => username, :password => passwd) # basic auth
116 # have we identified the user?
118 # check if the user has been banned
119 user_block = current_user.blocks.active.take
120 unless user_block.nil?
122 if user_block.zero_hour?
123 report_error t("application.setup_user_auth.blocked_zero_hour"), :forbidden
125 report_error t("application.setup_user_auth.blocked"), :forbidden
129 # if the user hasn't seen the contributor terms then don't
130 # allow editing - they have to go to the web site and see
131 # (but can decline) the CTs to continue.
132 if !current_user.terms_seen && flash[:skip_terms].nil?
134 report_error t("application.setup_user_auth.need_to_see_terms"), :forbidden