1 class ApiController < ApplicationController
2 skip_before_action :verify_authenticity_token
6 # Set format to xml unless client requires a specific format
9 request.format = "xml" unless request.format.symbol == :json
13 def authorize(realm = "Web Password", errormessage = "Couldn't authenticate you")
14 # make the current_user object from any auth sources we have
17 # handle authenticate pass/fail
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
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))
31 ApiAbility.new(current_user)
35 def deny_access(_exception)
38 report_error t("oauth.permissions.missing"), :forbidden
42 realm = "Web Password"
43 errormessage = "Couldn't authenticate you"
44 response.headers["WWW-Authenticate"] = "Basic realm=\"#{realm}\""
45 render :plain => errormessage, :status => :unauthorized
50 status = database_status
51 status = "offline" if status == "online" && Settings.status == "gpx_offline"
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
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
69 User.authenticate(:username => username, :password => passwd) # basic auth
73 # have we identified the user?
75 # check if the user has been banned
76 user_block = current_user.blocks.active.take
77 unless user_block.nil?
79 if user_block.zero_hour?
80 report_error t("application.setup_user_auth.blocked_zero_hour"), :forbidden
82 report_error t("application.setup_user_auth.blocked"), :forbidden
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?
91 report_error t("application.setup_user_auth.need_to_see_terms"), :forbidden