<commit_before>require 'scalarm/service_core/scalarm_authentication'

require 'exceptions/resource_not_found'

class ApplicationController < ActionController::Base
  include Scalarm::ServiceCore::ScalarmAuthentication

  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :null_session

  before_filter :authenticate, :except => [:status]

  rescue_from ResourceNotFound, with: :resource_not_found_handler

  def resource_not_found
    raise ResourceNotFound.new('Resource with given id not found')
  end

  def authentication_failed
    Rails.logger.debug('[authentication] failed -> 401')
    @user_session.destroy unless @user_session.nil?
    headers['WWW-Authenticate'] = %(Basic realm="Scalarm")

    respond_to do |format|
      format.html do
        render html: 'Authentication failed', status: :unauthorized
      end

      format.json do
        render json: {status: 'error', reason: 'Authentication failed'}, status: :unauthorized
      end
    end
  end

  def resource_not_found_handler(exception)
    respond_to do |format|
      format.json do
        render json: {status: 404, reason: exception.to_s}, status: 404
      end
      format.html do
        flash[:error] = exception.to_s
        redirect_to action: :index
      end
    end
  end

  def add_cors_header
    response['Access-Control-Allow-Origin'] = request.env['HTTP_ORIGIN']
    response['Access-Control-Allow-Credentials'] = 'true'
  end

  protected :authentication_failed, :add_cors_header
  private :resource_not_found_handler

end<commit_msg>SCAL-727: Better CORS support (pre-flight)<commit_after>require 'scalarm/service_core/scalarm_authentication'

require 'exceptions/resource_not_found'

class ApplicationController < ActionController::Base
  include Scalarm::ServiceCore::ScalarmAuthentication

  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :null_session

  before_filter :cors_preflight_check
  before_filter :authenticate, :except => [:status]

  rescue_from ResourceNotFound, with: :resource_not_found_handler

  def resource_not_found
    raise ResourceNotFound.new('Resource with given id not found')
  end

  def authentication_failed
    Rails.logger.debug('[authentication] failed -> 401')
    @user_session.destroy unless @user_session.nil?
    headers['WWW-Authenticate'] = %(Basic realm="Scalarm")

    respond_to do |format|
      format.html do
        render html: 'Authentication failed', status: :unauthorized
      end

      format.json do
        render json: {status: 'error', reason: 'Authentication failed'}, status: :unauthorized
      end
    end
  end

  def resource_not_found_handler(exception)
    respond_to do |format|
      format.json do
        render json: {status: 404, reason: exception.to_s}, status: 404
      end
      format.html do
        flash[:error] = exception.to_s
        redirect_to action: :index
      end
    end
  end

  def add_cors_header
    # TODO: list of allowed origins from config
    headers['Access-Control-Allow-Origin'] = request.env['HTTP_ORIGIN']
    headers['Access-Control-Allow-Credentials'] = 'true'
    headers['Access-Control-Allow-Methods'] = 'GET, OPTIONS'
    headers['Access-Control-Allow-Headers'] = 'Origin, Content-Type, Accept, Authorization, Token'
    headers['Access-Control-Max-Age'] = '1728000'
  end

  def cors_preflight_check
    if request.method == 'OPTIONS'
      # TODO: list of allowed origins from config
      headers['Access-Control-Allow-Origin'] = request.env['HTTP_ORIGIN']
      headers['Access-Control-Allow-Credentials'] = 'true'
      headers['Access-Control-Allow-Methods'] = 'OPTIONS'
      headers['Access-Control-Allow-Headers'] = 'X-Requested-With, X-Prototype-Version, Token'
      headers['Access-Control-Max-Age'] = '1728000'

      render :text => '', :content_type => 'text/plain'
    end
  end

  protected :authentication_failed, :add_cors_header, :cors_preflight_check
  private :resource_not_found_handler

end
{'fraction_non_alphanumeric': 0.09858467545143973, 'fraction_numerical': 0.00854075158613958, 'mean_word_length': 3.9148681055155876, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4428251', 'n_tokens_mistral': 1247, 'n_tokens_neox': 1191, 'n_words': 298}