- # asserts that the request method is the +method+ given as a parameter
- # or raises a suitable error. +method+ should be a symbol, e.g: :put or :get.
- def assert_method(method)
- ok = request.send((method.to_s.downcase + "?").to_sym)
- raise OSM::APIBadMethodError.new(method) unless ok
- end
-
- def api_call_timeout
- Timeout::timeout(APP_CONFIG['api_timeout'], OSM::APITimeoutError) do
- yield
- end
- end
-
-private
- # extract authorisation credentials from headers, returns user = nil if none
- def get_auth_data
- if request.env.has_key? 'X-HTTP_AUTHORIZATION' # where mod_rewrite might have put it
- authdata = request.env['X-HTTP_AUTHORIZATION'].to_s.split
- elsif request.env.has_key? 'REDIRECT_X_HTTP_AUTHORIZATION' # mod_fcgi
- authdata = request.env['REDIRECT_X_HTTP_AUTHORIZATION'].to_s.split
- elsif request.env.has_key? 'HTTP_AUTHORIZATION' # regular location
- authdata = request.env['HTTP_AUTHORIZATION'].to_s.split
- end
- # only basic authentication supported
- if authdata and authdata[0] == 'Basic'
- user, pass = Base64.decode64(authdata[1]).split(':',2)
- end
- return [user, pass]
- end
+ # Unfortunately if a PUT or POST request that has a body fails to
+ # read it then Apache will sometimes fail to return the response it
+ # is given to the client properly, instead erroring:
+ #
+ # https://issues.apache.org/bugzilla/show_bug.cgi?id=44782
+ #
+ # To work round this we call rewind on the body here, which is added
+ # as a filter, to force it to be fetched from Apache into a file.
+ def fetch_body
+ request.body.rewind
+ end
+
+ def map_layout
+ policy = request.content_security_policy.clone
+
+ policy.child_src(*policy.child_src, "http://127.0.0.1:8111", "https://127.0.0.1:8112")
+ policy.frame_src(*policy.frame_src, "http://127.0.0.1:8111", "https://127.0.0.1:8112")
+ policy.connect_src(*policy.connect_src, Settings.nominatim_url, Settings.overpass_url, Settings.fossgis_osrm_url, Settings.graphhopper_url, Settings.fossgis_valhalla_url)
+ policy.form_action(*policy.form_action, "render.openstreetmap.org")
+ policy.style_src(*policy.style_src, :unsafe_inline)
+
+ request.content_security_policy = policy
+
+ case Settings.status
+ when "database_offline", "api_offline"
+ flash.now[:warning] = t("layouts.osm_offline")
+ when "database_readonly", "api_readonly"
+ flash.now[:warning] = t("layouts.osm_read_only")
+ end
+
+ request.xhr? ? "xhr" : "map"
+ end
+
+ def preferred_editor
+ if params[:editor]
+ params[:editor]
+ elsif current_user&.preferred_editor
+ current_user.preferred_editor
+ else
+ Settings.default_editor
+ end
+ end
+
+ helper_method :preferred_editor
+
+ def update_totp
+ if Settings.key?(:totp_key)
+ cookies["_osm_totp_token"] = {
+ :value => ROTP::TOTP.new(Settings.totp_key, :interval => 3600).now,
+ :domain => "openstreetmap.org",
+ :expires => 1.hour.from_now
+ }
+ end
+ end
+
+ def current_ability
+ Ability.new(current_user)
+ end
+
+ def deny_access(_exception)
+ if doorkeeper_token
+ set_locale
+ report_error t("oauth.permissions.missing"), :forbidden
+ elsif current_user
+ set_locale
+ respond_to do |format|
+ format.html { redirect_to :controller => "/errors", :action => "forbidden" }
+ format.any { report_error t("application.permission_denied"), :forbidden }
+ end
+ elsif request.get?
+ respond_to do |format|
+ format.html { redirect_to login_path(:referer => request.fullpath) }
+ format.any { head :forbidden }
+ end
+ else
+ head :forbidden
+ end
+ end
+
+ def invalid_parameter(_exception)
+ if request.get?
+ respond_to do |format|
+ format.html { redirect_to :controller => "/errors", :action => "bad_request" }
+ format.any { head :bad_request }
+ end
+ else
+ head :bad_request
+ end
+ end
+
+ # clean any referer parameter
+ def safe_referer(referer)
+ begin
+ referer = URI.parse(referer)
+
+ if referer.scheme == "http" || referer.scheme == "https"
+ referer.scheme = nil
+ referer.host = nil
+ referer.port = nil
+ elsif referer.scheme || referer.host || referer.port
+ referer = nil
+ end
+
+ referer = nil if referer&.path&.first != "/"
+ rescue URI::InvalidURIError
+ referer = nil
+ end
+
+ referer&.to_s
+ end
+
+ def scope_enabled?(scope)
+ doorkeeper_token&.includes_scope?(scope)
+ end