1 class ApiController < ApplicationController
2 skip_before_action :verify_authenticity_token
6 # Set format to xml unless client requires a specific format
8 request.format = "xml" unless params[:format]
11 def authorize(realm = "Web Password", errormessage = "Couldn't authenticate you")
12 # make the current_user object from any auth sources we have
15 # handle authenticate pass/fail
17 # no auth, the user does not exist or the password was wrong
18 response.headers["WWW-Authenticate"] = "Basic realm=\"#{realm}\""
19 render :plain => errormessage, :status => :unauthorized
25 # Use capabilities from the oauth token if it exists and is a valid access token
26 if Authenticator.new(self, [:token]).allow?
27 ApiAbility.new(nil).merge(ApiCapability.new(current_token))
29 ApiAbility.new(current_user)
33 def deny_access(_exception)
36 report_error t("oauth.permissions.missing"), :forbidden
40 realm = "Web Password"
41 errormessage = "Couldn't authenticate you"
42 response.headers["WWW-Authenticate"] = "Basic realm=\"#{realm}\""
43 render :plain => errormessage, :status => :unauthorized
48 status = database_status
49 status = "offline" if status == "online" && Settings.status == "gpx_offline"
54 # sets up the current_user for use by other methods. this is mostly called
55 # from the authorize method, but can be called elsewhere if authorisation
58 # try and setup using OAuth
59 unless Authenticator.new(self, [:token]).allow?
60 username, passwd = get_auth_data # parse from headers
61 # authenticate per-scheme
62 self.current_user = if username.nil?
63 nil # no authentication provided - perhaps first connect (client should retry after 401)
64 elsif username == "token"
65 User.authenticate(:token => passwd) # preferred - random token for user from db, passed in basic auth
67 User.authenticate(:username => username, :password => passwd) # basic auth
71 # have we identified the user?
73 # check if the user has been banned
74 user_block = current_user.blocks.active.take
75 unless user_block.nil?
77 if user_block.zero_hour?
78 report_error t("application.setup_user_auth.blocked_zero_hour"), :forbidden
80 report_error t("application.setup_user_auth.blocked"), :forbidden
84 # if the user hasn't seen the contributor terms then don't
85 # allow editing - they have to go to the web site and see
86 # (but can decline) the CTs to continue.
87 if !current_user.terms_seen && flash[:skip_terms].nil?
89 report_error t("application.setup_user_auth.need_to_see_terms"), :forbidden