module API
  module V1
    module Helpers
      def require_no_authentication
        # TODO: Disable authentication temporarily. Fix later.
        # env['api.endpoint'].namespace == '/tokens'
        true
      end

      def authenticate_account_from_token!
        conditions = params.slice(Devise::TokenAuthenticatable.token_authentication_key)
        account = Account.find_for_token_authentication(conditions)
        if account
          env['current_account'] = account
        else
          # Insert random sleep to make token prediction more difficult by timing attack.
          sleep((200 + rand(200)) / 1000.0)
          error!('Unauthorized', 401)
        end
      end

      def authorize!(*_args)
        # TODO: Disable authorization temporarily. Fix later.
        # current_ability.authorize!(*args)
        true
      end

      def can?(*_args)
        # TODO: Disable authorization temporarily. Fix later.
        # current_ability.can?(*args)
        true
      end

      def cannot?(*_args)
        # TODO: Disable authorization temporarily. Fix later.
        # current_ability.cannot?(*args)
        false
      end

      def current_ability
        ::Ability.new(current_account)
      end

      def current_account
        env['current_account']
      end

      def declared_params
        declared(params, include_missing: false).to_hash
      end

      def permitted_params(*args)
        ActionController::Parameters.new(params.slice(*args)).permit(*args)
      end
    end
  end
end

module API
  module V1
    module Helpers
      def require_no_authentication
        # TODO: Disable authentication temporarily. Fix later.
        # env['api.endpoint'].namespace == '/tokens'
        true
      end

      def authenticate_account_from_token!
        conditions = params.slice(Devise::TokenAuthenticatable.token_authentication_key)
        account = Account.find_for_token_authentication(conditions)
        if account
          env['current_account'] = account
        else
          # Insert random sleep to make token prediction more difficult by timing attack.
          sleep((200 + rand(200)) / 1000.0)
          error!('Unauthorized', 401)
        end
      end

      def authorize!(*_args)
        # TODO: Disable authorization temporarily. Fix later.
        # current_ability.authorize!(*args)
        true
      end

      def can?(*_args)
        # TODO: Disable authorization temporarily. Fix later.
        # current_ability.can?(*args)
        true
      end

      def cannot?(*_args)
        # TODO: Disable authorization temporarily. Fix later.
        # current_ability.cannot?(*args)
        false
      end

      def current_ability
        ::Ability.new(current_account)
      end

      def current_account
        env['current_account']
      end

      def declared_params
        declared(params, include_missing: false).to_hash.with_indifferent_access
      end

      def permitted_params(*args)
        ActionController::Parameters.new(params.slice(*args)).permit(*args)
      end
    end
  end
end

Allow access to declared_params with key as symbol
Allow access to declared_params with key as symbol
