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"
52 def authorize(realm = "Web Password", errormessage = "Couldn't authenticate you")
53 # make the current_user object from any auth sources we have
56 # handle authenticate pass/fail
58 # no auth, the user does not exist or the password was wrong
59 response.headers["WWW-Authenticate"] = "Basic realm=\"#{realm}\""
60 render :plain => errormessage, :status => :unauthorized
66 # Use capabilities from the oauth token if it exists and is a valid access token
67 if Authenticator.new(self, [:token]).allow?
68 ApiAbility.new(nil).merge(ApiCapability.new(current_token))
70 ApiAbility.new(current_user)
74 def deny_access(_exception)
77 report_error t("oauth.permissions.missing"), :forbidden
81 realm = "Web Password"
82 errormessage = "Couldn't authenticate you"
83 response.headers["WWW-Authenticate"] = "Basic realm=\"#{realm}\""
84 render :plain => errormessage, :status => :unauthorized
89 status = database_status
90 status = "offline" if status == "online" && Settings.status == "gpx_offline"
95 # sets up the current_user for use by other methods. this is mostly called
96 # from the authorize method, but can be called elsewhere if authorisation
99 # try and setup using OAuth
100 unless Authenticator.new(self, [:token]).allow?
101 username, passwd = get_auth_data # parse from headers
102 # authenticate per-scheme
103 self.current_user = if username.nil?
104 nil # no authentication provided - perhaps first connect (client should retry after 401)
105 elsif username == "token"
106 User.authenticate(:token => passwd) # preferred - random token for user from db, passed in basic auth
108 User.authenticate(:username => username, :password => passwd) # basic auth
112 # have we identified the user?
114 # check if the user has been banned
115 user_block = current_user.blocks.active.take
116 unless user_block.nil?
118 if user_block.zero_hour?
119 report_error t("application.setup_user_auth.blocked_zero_hour"), :forbidden
121 report_error t("application.setup_user_auth.blocked"), :forbidden
125 # if the user hasn't seen the contributor terms then don't
126 # allow editing - they have to go to the web site and see
127 # (but can decline) the CTs to continue.
128 if !current_user.terms_seen && flash[:skip_terms].nil?
130 report_error t("application.setup_user_auth.need_to_see_terms"), :forbidden