Sha256: 55eddcdcbaad4bc03b4f64160d31eecbc039876fcd494d06c733570392eaa963

Contents?: true

Size: 1.12 KB

Versions: 17

Compression:

Stored size: 1.12 KB

Contents

class ApplicationController < ActionController::API
  include ActionController::HttpAuthentication::Token::ControllerMethods

  before_action :set_current_request_details
  before_action :authenticate
  <%- if options.sudoable? %>
  def require_sudo
    unless Current.session.sudo?
      render json: { error: "Enter your password to continue" }, status: :forbidden
    end
  end
  <%- end -%>
  <%- if options.lockable? %>
  def require_lock(wait: 1.hour, attempts: 10)
    counter = Kredis.counter("require_lock:#{request.remote_ip}:#{params[:controller]}:#{params[:action]}", expires_in: wait)
    counter.increment

    if counter.value > attempts
      render json: { error: "You've exceeded the maximum number of attempts" }, status: :too_many_requests
    end
  end
  <%- end -%>

  private
    def authenticate
      if session = authenticate_with_http_token { |token, _| Session.find_signed(token) }
        Current.session = session
      else
        request_http_token_authentication
      end
    end

    def set_current_request_details
      Current.user_agent = request.user_agent
      Current.ip_address = request.ip
    end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
authentication-zero-2.16.5 lib/generators/authentication/templates/controllers/api/application_controller.rb.tt
authentication-zero-2.16.4 lib/generators/authentication/templates/controllers/api/application_controller.rb.tt
authentication-zero-2.16.3 lib/generators/authentication/templates/controllers/api/application_controller.rb.tt
authentication-zero-2.16.2 lib/generators/authentication/templates/controllers/api/application_controller.rb.tt
authentication-zero-2.16.1 lib/generators/authentication/templates/controllers/api/application_controller.rb.tt
authentication-zero-2.16.0 lib/generators/authentication/templates/controllers/api/application_controller.rb.tt
authentication-zero-2.15.9 lib/generators/authentication/templates/controllers/api/application_controller.rb.tt
authentication-zero-2.15.8 lib/generators/authentication/templates/controllers/api/application_controller.rb.tt
authentication-zero-2.15.7 lib/generators/authentication/templates/controllers/api/application_controller.rb.tt
authentication-zero-2.15.6 lib/generators/authentication/templates/controllers/api/application_controller.rb.tt
authentication-zero-2.15.5 lib/generators/authentication/templates/controllers/api/application_controller.rb.tt
authentication-zero-2.15.4 lib/generators/authentication/templates/controllers/api/application_controller.rb.tt
authentication-zero-2.15.3 lib/generators/authentication/templates/controllers/api/application_controller.rb.tt
authentication-zero-2.15.2 lib/generators/authentication/templates/controllers/api/application_controller.rb.tt
authentication-zero-2.15.1 lib/generators/authentication/templates/controllers/api/application_controller.rb.tt
authentication-zero-2.15.0 lib/generators/authentication/templates/controllers/api/application_controller.rb.tt
authentication-zero-2.14.0 lib/generators/authentication/templates/controllers/api/application_controller.rb.tt