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