]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api_controller.rb
JSON output, handle Accept header
[rails.git] / app / controllers / api_controller.rb
1 class ApiController < ApplicationController
2   skip_before_action :verify_authenticity_token
3
4   private
5
6   # Set format to xml unless client requires a specific format
7   def default_format_xml
8     unless params[:format]
9       request.format = "xml" unless request.format.symbol == :json
10     end
11   end
12
13   def authorize(realm = "Web Password", errormessage = "Couldn't authenticate you")
14     # make the current_user object from any auth sources we have
15     setup_user_auth
16
17     # handle authenticate pass/fail
18     unless current_user
19       # no auth, the user does not exist or the password was wrong
20       response.headers["WWW-Authenticate"] = "Basic realm=\"#{realm}\""
21       render :plain => errormessage, :status => :unauthorized
22       false
23     end
24   end
25
26   def current_ability
27     # Use capabilities from the oauth token if it exists and is a valid access token
28     if Authenticator.new(self, [:token]).allow?
29       ApiAbility.new(nil).merge(ApiCapability.new(current_token))
30     else
31       ApiAbility.new(current_user)
32     end
33   end
34
35   def deny_access(_exception)
36     if current_token
37       set_locale
38       report_error t("oauth.permissions.missing"), :forbidden
39     elsif current_user
40       head :forbidden
41     else
42       realm = "Web Password"
43       errormessage = "Couldn't authenticate you"
44       response.headers["WWW-Authenticate"] = "Basic realm=\"#{realm}\""
45       render :plain => errormessage, :status => :unauthorized
46     end
47   end
48
49   def gpx_status
50     status = database_status
51     status = "offline" if status == "online" && Settings.status == "gpx_offline"
52     status
53   end
54
55   ##
56   # sets up the current_user for use by other methods. this is mostly called
57   # from the authorize method, but can be called elsewhere if authorisation
58   # is optional.
59   def setup_user_auth
60     # try and setup using OAuth
61     unless Authenticator.new(self, [:token]).allow?
62       username, passwd = get_auth_data # parse from headers
63       # authenticate per-scheme
64       self.current_user = if username.nil?
65                             nil # no authentication provided - perhaps first connect (client should retry after 401)
66                           elsif username == "token"
67                             User.authenticate(:token => passwd) # preferred - random token for user from db, passed in basic auth
68                           else
69                             User.authenticate(:username => username, :password => passwd) # basic auth
70                           end
71     end
72
73     # have we identified the user?
74     if current_user
75       # check if the user has been banned
76       user_block = current_user.blocks.active.take
77       unless user_block.nil?
78         set_locale
79         if user_block.zero_hour?
80           report_error t("application.setup_user_auth.blocked_zero_hour"), :forbidden
81         else
82           report_error t("application.setup_user_auth.blocked"), :forbidden
83         end
84       end
85
86       # if the user hasn't seen the contributor terms then don't
87       # allow editing - they have to go to the web site and see
88       # (but can decline) the CTs to continue.
89       if !current_user.terms_seen && flash[:skip_terms].nil?
90         set_locale
91         report_error t("application.setup_user_auth.need_to_see_terms"), :forbidden
92       end
93     end
94   end
95 end