<commit_before>module WeixinRailsMiddleware
  class WeixinController < ApplicationController
    include WeixinMessageHelper

    skip_before_filter :verify_authenticity_token
    before_action :check_weixin_params, only: [:index, :reply]
    before_action :set_weixin_public_account, :set_weixin_message, only: :reply

    def index
      render text: params[:echostr]
    end

    def reply
    end

    protected

      def check_weixin_params
        if check_weixin_token_valid?
          if !is_hexdigest?
            render text: "Forbidden", status: 403
          end
        end
      end

      # check the token from Weixin Service is exist in local store.
      def check_weixin_token_valid?
        if WeixinRailsMiddleware.config.token_string.blank?
          if token_model_instance.blank?
            render text: "Forbidden", status: 403
            return false
          end
        else
          if current_weixin_token != WeixinRailsMiddleware.config.token_string
            render text: "Forbidden", status: 403
            return false
          end
        end
        true
      end

      def is_hexdigest?
        signature = params[:signature] || ''
        timestamp = params[:timestamp] || ''
        nonce     = params[:nonce] || ''
        current_signature = Digest::SHA1.hexdigest([current_weixin_token, timestamp, nonce].sort.join)
        return true if current_signature == signature
        false
      end

      def current_weixin_token
        @weixin_token = params[:weixin_token]
      end

      def token_model_instance
        token_model  = WeixinRailsMiddleware.config.token_model_class
        token_column = WeixinRailsMiddleware.config.token_column
        token_model_instance = token_model.where("#{token_column}" => current_weixin_token).first
        token_model_instance
      end

      # e.g. will generate +@weixin_public_account+
      def set_weixin_public_account
        return nil if WeixinRailsMiddleware.config.token_string.blank?
        @weixin_public_account ||= token_model_instance
      end

      def set_weixin_message
        # Get the current_message
        @weixin_message ||= current_weixin_message
      end

      # take the weixin params
      def current_weixin_params
        request.body.read
      end

      # return a message class with current_weixin_params
      def current_weixin_message
        Message.factory(current_weixin_params)
      end

  end
end
<commit_msg>fixed bug if it don't config token model<commit_after>module WeixinRailsMiddleware
  class WeixinController < ApplicationController
    include WeixinMessageHelper

    skip_before_filter :verify_authenticity_token
    before_action :check_weixin_params, only: [:index, :reply]
    before_action :set_weixin_public_account, :set_weixin_message, only: :reply

    def index
      render text: params[:echostr]
    end

    def reply
    end

    protected

      def check_weixin_params
        if check_weixin_token_valid?
          if !is_hexdigest?
            render text: "Forbidden", status: 403
          end
        end
      end

      # check the token from Weixin Service is exist in local store.
      def check_weixin_token_valid?
        if WeixinRailsMiddleware.config.token_string.blank?
          if token_model_instance.blank?
            render text: "Forbidden", status: 403
            return false
          end
        else
          if current_weixin_token != WeixinRailsMiddleware.config.token_string
            render text: "Forbidden", status: 403
            return false
          end
        end
        true
      end

      def is_hexdigest?
        signature = params[:signature] || ''
        timestamp = params[:timestamp] || ''
        nonce     = params[:nonce] || ''
        current_signature = Digest::SHA1.hexdigest([current_weixin_token, timestamp, nonce].sort.join)
        return true if current_signature == signature
        false
      end

      def current_weixin_token
        @weixin_token = params[:weixin_token]
      end

      def token_model_instance
        token_model  = WeixinRailsMiddleware.config.token_model_class
        token_column = WeixinRailsMiddleware.config.token_column
        token_model_instance = token_model.where("#{token_column}" => current_weixin_token).first
        token_model_instance
      end

      # e.g. will generate +@weixin_public_account+
      def set_weixin_public_account
        return nil if WeixinRailsMiddleware.config.token_string.present?
        @weixin_public_account ||= token_model_instance
      end

      def set_weixin_message
        # Get the current_message
        @weixin_message ||= current_weixin_message
      end

      # take the weixin params
      def current_weixin_params
        request.body.read
      end

      # return a message class with current_weixin_params
      def current_weixin_message
        Message.factory(current_weixin_params)
      end

  end
end

{'fraction_non_alphanumeric': 0.05754088431253786, 'fraction_numerical': 0.004037956793862305, 'mean_word_length': 2.474053295932679, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2274082', 'n_tokens_mistral': 1470, 'n_tokens_neox': 1432, 'n_words': 325}