<commit_before>module Userbin
  class Authentication

    CLOSING_HEAD_TAG = %r{</head>}
    CLOSING_BODY_TAG = %r{</body>}

    def initialize(app, options = {})
      @app = app
    end

    def call(env)
      request = Rack::Request.new(env)

      begin
        if env["REQUEST_PATH"] == "/userbin" &&
           env["REQUEST_METHOD"] == "POST"
          signature, data = Userbin.authenticate_events!(request)

          MultiJson.decode(data)['events'].each do |event|
            Userbin::Events.trigger(event)
          end

          [ 200, { 'Content-Type' => 'text/html',
            'Content-Length' => '2' }, ['OK'] ]
        else
          signature, data = Userbin.authenticate!(request)

          if restrict && env["REQUEST_PATH"].start_with?(restrict) &&
             !Userbin.authenticated?
            return render_gateway(env["REQUEST_PATH"])
          end

          generate_response(env, signature, data)
        end
      rescue Userbin::SecurityError
        message =
          'Userbin::SecurityError: Invalid signature. Refresh to try again.'
        headers = {
          'Content-Type' => 'text/text',
          'Content-Length' => message.length.to_s<commit_msg>Look at PATH_INFO instead of REQUEST_PATH to work in all environments<commit_after>module Userbin
  class Authentication

    CLOSING_HEAD_TAG = %r{</head>}
    CLOSING_BODY_TAG = %r{</body>}

    def initialize(app, options = {})
      @app = app
    end

    def call(env)
      request = Rack::Request.new(env)

      begin
        if env["PATH_INFO"] == "/userbin" &&
           env["REQUEST_METHOD"] == "POST"
          signature, data = Userbin.authenticate_events!(request)

          MultiJson.decode(data)['events'].each do |event|
            Userbin::Events.trigger(event)
          end

          [ 200, { 'Content-Type' => 'text/html',
            'Content-Length' => '2' }, ['OK'] ]
        else
          signature, data = Userbin.authenticate!(request)

          if restrict && env["PATH_INFO"].start_with?(restrict) &&
             !Userbin.authenticated?
            return render_gateway(env["REQUEST_PATH"])
          end

          generate_response(env, signature, data)
        end
      rescue Userbin::SecurityError
        message =
          'Userbin::SecurityError: Invalid signature. Refresh to try again.'
        headers = {
          'Content-Type' => 'text/text',
          'Content-Length' => message.length.to_s
{'fraction_non_alphanumeric': 0.12929623567921442, 'fraction_numerical': 0.0032733224222585926, 'mean_word_length': 2.2044560943643514, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, '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': '7531528', 'n_tokens_mistral': 728, 'n_tokens_neox': 701, 'n_words': 145}