<commit_before>module OwaspZap
    class Auth
        attr_accessor :ctx,:base
        def initialize(params = {})
            @ctx = params[:context] || 1 #default context is the1
            @base = params[:base] || "http://127.0.0.1:8080/JSON"
            @target = params[:target]
            @uid = nil
        end

        #
        # define dynamically the methods from: http://127.0.0.1:8080/UI/auth/
        #
        #
        [:login_url, :logout_url, :login_data, :logout_data, :logged_in_indicator, :logged_out_indicator].each do |method|
            define_method method do
                RestClient::get "#{@base}/auth/view/#{to_url(method)}/?zapapiformat=JSON&contextId=#{@ctx}"
            end
        end

        #
        # define methods login, logout 
        #
        #
        [:login,:logout].each do |method|
            define_method method do
               RestClient::get "#{@base}/auth/action/#{to_url(method)}/?zapapiformat=JSON&contextId=#{@ctx}"
            end
        end

        # params:
        # args a hash with the following keys -> values
        # url: url including http:// 
        # post_data: an already encoded string like "email%3Dfoo%2540example.org%26passwd%3Dfoobar" 
        # TODO: offer a way to encode it, giving a hash?
        def import_context(context)
          set_query "#{@base}/json/context/action/importContext/", contextFile: context
          context_name = context.split('.')[0]
          puts set_query "#{@base}/json/context/view/context", contextName: context_name
          @ctx = 2
        end

        def new_context(context_name, set_as_context=true)
            set_query "#{@base}/json/context/action/newContext/", contextName: context_name
            @ctx = 2
        end

        def set_include_in_context(context_name, regrexs)
            regrexs.each do |regrex|
                set_query "#{@base}/json/context/action/includeInContext/", contextName: context_name, regrex: regrex
            end 
        end

        def set_exclude_from_context(context_name, regrexs)
            regrexs.each do |regrex|
                set_query "#{@base}/json/context/action/excludeFromContext/", contextName: context_name, regrex: regrex
            end 
        end

        def new_user(name)
            set_query "#{@base}/json/users/action/newUser/", contextId: @ctx, name: name
            puts self.users(@ctx)
            @uid = 1
        end

        def users(context)
            set_query "#{@base}/json/users/view/usersList", contextId: context
        end

        def contexts
            set_query "#{@base}/json/context/view/contextList/"
        end

        def set_login_url(args)
            set_query "#{@base}/auth/action/setLoginUrl/",:postData=>args[:post_data]
        end

        def set_logout_url(args)
            set_query "#{@base}/auth/action/setLogoutUrl/",:postData=>args[:post_data]
        end

        def set_logged_in_indicator(args)
            set_query "#{@base}/auth/action/setLoggedInIndicator/",:postData=>args[:indicator]
        end

        def set_logged_out_indicator(args)
            set_query "#{@base}/auth/action/setLoggedOutIndicator/", :indicator=>args[:indicator]
        end

        private

        # addr a string like #{@base}/auth/foo/bar<commit_msg>some changes<commit_after>module OwaspZap
    class Auth
        attr_accessor :ctx,:base
        def initialize(params = {})
            @ctx = params[:context] || 1 #default context is the1
            @base = params[:base] || "http://1192.168.127.12:8080/JSON"
            @target = params[:target]
            @uid = nil
        end

        #
        # define dynamically the methods from: http://127.0.0.1:8080/UI/auth/
        #
        #
        [:login_url, :logout_url, :login_data, :logout_data, :logged_in_indicator, :logged_out_indicator].each do |method|
            define_method method do
                RestClient::get "#{@base}/auth/view/#{to_url(method)}/?zapapiformat=JSON&contextId=#{@ctx}"
            end
        end

        #
        # define methods login, logout 
        #
        #
        [:login,:logout].each do |method|
            define_method method do
               RestClient::get "#{@base}/auth/action/#{to_url(method)}/?zapapiformat=JSON&contextId=#{@ctx}"
            end
        end

        # params:
        # args a hash with the following keys -> values
        # url: url including http:// 
        # post_data: an already encoded string like "email%3Dfoo%2540example.org%26passwd%3Dfoobar" 
        # TODO: offer a way to encode it, giving a hash?
        def import_context(context)
          response = eval(set_query "#{@base}/json/context/action/importContext/", contextFile: context)
          #TODO: Parse response from view context call to interpret context id... currently hard-coded
          @ctx = response[:contextId]
        end

        def new_context(context_name, set_as_context=true)
            response = eval(set_query "#{@base}/json/context/action/newContext/", contextName: context_name)
            @ctx = response[:contextId] if set_as_context
        end

        def set_include_in_context(context_name, regrexs)
            regrexs.each do |regrex|
                set_query "#{@base}/json/context/action/includeInContext/", contextName: context_name, regrex: regrex
            end 
        end

        def set_exclude_from_context(context_name, regrexs)
            regrexs.each do |regrex|
                set_query "#{@base}/json/context/action/excludeFromContext/", contextName: context_name, regrex: regrex
            end 
        end

        def new_user(name)
            response = eval(set_query "#{@base}/json/users/action/newUser/", contextId: @ctx, name: name)
            @uid = response[:userId]
        end

        def users(context)
            set_query "#{@base}/json/users/view/usersList", contextId: context
        end

        def contexts
            set_query "#{@base}/json/context/view/contextList/"
        end

        def set_login_url(args)
            set_query "#{@base}/auth/action/setLoginUrl/",:postData=>args[:post_data]
        end

        def set_logout_url(args)
            set_query "#{@base}/auth/action/setLogoutUrl/",:postData=>args[:post_data]
        end

        def set_logged_in_indicator(args)
            set_query "#{@base}/auth/action/setLoggedInIndicator/",:postData=>args[:indicator]
        end

        def set_logged_out_indicator(args)
            set_query "#{@base}/auth/action/setLoggedOutIndicator/", :indicator=>args[:indicator]
        end

        private

        # addr a string like #{@base}/auth/foo/bar
{'fraction_non_alphanumeric': 0.11769126709754998, 'fraction_numerical': 0.010521569216894634, 'mean_word_length': 2.141643059490085, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '4081069', 'n_tokens_mistral': 2083, 'n_tokens_neox': 1978, 'n_words': 397}