<commit_before>require 'net/http'
require 'uri'
require 'net/ping'

class App
  helpers do
    def protected!
      return if !!authorized_user
      respond_as_unauthorized
    end

    def authorized_user
      @auth ||=  Rack::Auth::Basic::Request.new(request.env)
      if @auth.provided? && @auth.basic? && @auth.credentials
        return check_token_and_get_user(
          username: @auth.credentials[0],
          auth_token: @auth.credentials[1]
        )
      else
        return nil
      end
    end

    def check_token_and_get_user(params)
      user = User.where(username: params[:username]).last
      if user && user.valid_token?(params[:auth_token])
        return user
      end
      return nil
    end

    def check_password_and_get_user(params)
      user = User.where(
        username: params[:username],
      ).last
      if user && user.password == params[:password]
        return user
      end
      return nil
    end

    def respond_as_unauthorized
      headers['WWW-Authenticate'] = 'Basic realm="Restricted Area"'
      halt 401, "Not authorized\n"
    end
  end

  get '/' do
    erb :index
  end

  get '/servers' do
    @servers = Server.includes(:services).all;
    content_type :json
    @servers.to_json(:include => :services)
  end

  post '/servers' do
    protected!
    server_params = params["server"]
    respond_as_unauthorized unless server_params
    server = Server.create(host: server_params["host"], label: server_params["label"])
  end

  get '/logs/server/:id' do
    protected!
    id = params[:id]
    @server = Server.find(id);
    content_type :json
    @server.to_json
  end

  get '/servers/new' do
    erb :new_server
  end

  get '/services' do
    protected!
    @services = Service.includes(:servers).all;
    content_type :json
    @services.to_json(:include => :servers)
  end

  post '/services' do
    services_params = params["services"]
    halt(400, "Services Params missing") unless services_params

    service = Service.create(
      name: services_params["name"],
      service_type: services_params["service_type"],
      log_path: services_params["log_path"]
    )
    halt(400, service.errors.to_json) unless service.valid?

    service.servers << services_params["servers"].map do |server_name|
      Server.find_by(host: server_name)
    end
    service.save!
    service.to_json
  end

  get '/service/:id' do
    protected!
    @service = Service.find(params[:id]);
    content_type :json
    @service.to_json
  end

  get '/services/new' do
    @log_paths = YAML.load_file('log_paths.yml')
    erb :new_service
  end

  get '/logs/service/:id' do
    @id = params[:id]
    erb :log
  end

  get '/server/:id/status' do
    protected!
    server = Server.find(params[:id])
    check = Net::Ping::External.new(server["host"])
    status  = {"status" => check.ping}
    content_type :json
    status.to_json
  end

  post '/user/auth' do
    user = check_password_and_get_user(params)
    if user.nil?
      respond_as_unauthorized
    else
      return { auth_token: user.auth_token }.to_json
    end
  end

  post '/user/logout' do
    user = authorized_user
    if user.nil?
      respond_as_unauthorized
    else
      user.regenerate_token
      return
    end
  end
end
<commit_msg>Remove WWW-Authenticate from 401 response<commit_after>require 'net/http'
require 'uri'
require 'net/ping'

class App
  helpers do
    def protected!
      return if !!authorized_user
      respond_as_unauthorized
    end

    def authorized_user
      @auth ||=  Rack::Auth::Basic::Request.new(request.env)
      if @auth.provided? && @auth.basic? && @auth.credentials
        return check_token_and_get_user(
          username: @auth.credentials[0],
          auth_token: @auth.credentials[1]
        )
      else
        return nil
      end
    end

    def check_token_and_get_user(params)
      user = User.where(username: params[:username]).last
      if user && user.valid_token?(params[:auth_token])
        return user
      end
      return nil
    end

    def check_password_and_get_user(params)
      user = User.where(
        username: params[:username],
      ).last
      if user && user.password == params[:password]
        return user
      end
      return nil
    end

    def respond_as_unauthorized
      # headers['WWW-Authenticate'] = 'Basic realm="Restricted Area"'
      halt 401, "Not authorized\n"
    end
  end

  get '/' do
    erb :index
  end

  get '/servers' do
    @servers = Server.includes(:services).all;
    content_type :json
    @servers.to_json(:include => :services)
  end

  post '/servers' do
    protected!
    server_params = params["server"]
    respond_as_unauthorized unless server_params
    server = Server.create(host: server_params["host"], label: server_params["label"])
  end

  get '/logs/server/:id' do
    protected!
    id = params[:id]
    @server = Server.find(id);
    content_type :json
    @server.to_json
  end

  get '/servers/new' do
    erb :new_server
  end

  get '/services' do
    protected!
    @services = Service.includes(:servers).all;
    content_type :json
    @services.to_json(:include => :servers)
  end

  post '/services' do
    services_params = params["services"]
    halt(400, "Services Params missing") unless services_params

    service = Service.create(
      name: services_params["name"],
      service_type: services_params["service_type"],
      log_path: services_params["log_path"]
    )
    halt(400, service.errors.to_json) unless service.valid?

    service.servers << services_params["servers"].map do |server_name|
      Server.find_by(host: server_name)
    end
    service.save!
    service.to_json
  end

  get '/service/:id' do
    protected!
    @service = Service.find(params[:id]);
    content_type :json
    @service.to_json
  end

  get '/services/new' do
    @log_paths = YAML.load_file('log_paths.yml')
    erb :new_service
  end

  get '/logs/service/:id' do
    @id = params[:id]
    erb :log
  end

  get '/server/:id/status' do
    protected!
    server = Server.find(params[:id])
    check = Net::Ping::External.new(server["host"])
    status  = {"status" => check.ping}
    content_type :json
    status.to_json
  end

  post '/user/auth' do
    user = check_password_and_get_user(params)
    if user.nil?
      respond_as_unauthorized
    else
      return { auth_token: user.auth_token }.to_json
    end
  end

  post '/user/logout' do
    user = authorized_user
    if user.nil?
      respond_as_unauthorized
    else
      user.regenerate_token
      return
    end
  end
end

{'fraction_non_alphanumeric': 0.11009732360097324, 'fraction_numerical': 0.0038017031630170318, 'mean_word_length': 2.7053521126760565, 'pattern_counts': {'":': 0, '<': 7, '<?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': '706016', 'n_tokens_mistral': 2142, 'n_tokens_neox': 2102, 'n_words': 486}