]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api_controller.rb
Use a lambda in order to pass parameters in before_actions
[rails.git] / app / controllers / api_controller.rb
1 class ApiController < ApplicationController
2   skip_before_action :verify_authenticity_token
3
4   def authorize(realm = "Web Password", errormessage = "Couldn't authenticate you")
5     # make the current_user object from any auth sources we have
6     setup_user_auth
7
8     # handle authenticate pass/fail
9     unless current_user
10       # no auth, the user does not exist or the password was wrong
11       response.headers["WWW-Authenticate"] = "Basic realm=\"#{realm}\""
12       render :plain => errormessage, :status => :unauthorized
13       return false
14     end
15   end
16
17   def deny_access(_exception)
18     if current_token
19       set_locale
20       report_error t("oauth.permissions.missing"), :forbidden
21     elsif current_user
22       head :forbidden
23     else
24       realm = "Web Password"
25       errormessage = "Couldn't authenticate you"
26       response.headers["WWW-Authenticate"] = "Basic realm=\"#{realm}\""
27       render :plain => errormessage, :status => :unauthorized
28     end
29   end
30 end