1 class ApiController < ApplicationController
2 skip_before_action :verify_authenticity_token
6 def authorize(realm = "Web Password", errormessage = "Couldn't authenticate you")
7 # make the current_user object from any auth sources we have
10 # handle authenticate pass/fail
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
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))
24 ApiAbility.new(current_user)
28 def deny_access(_exception)
31 report_error t("oauth.permissions.missing"), :forbidden
35 realm = "Web Password"
36 errormessage = "Couldn't authenticate you"
37 response.headers["WWW-Authenticate"] = "Basic realm=\"#{realm}\""
38 render :plain => errormessage, :status => :unauthorized
43 status = database_status
44 status = "offline" if status == "online" && Settings.status == "gpx_offline"
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
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
62 User.authenticate(:username => username, :password => passwd) # basic auth
66 # have we identified the user?
68 # check if the user has been banned
69 user_block = current_user.blocks.active.take
70 unless user_block.nil?
72 if user_block.zero_hour?
73 report_error t("application.setup_user_auth.blocked_zero_hour"), :forbidden
75 report_error t("application.setup_user_auth.blocked"), :forbidden
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?
84 report_error t("application.setup_user_auth.need_to_see_terms"), :forbidden