コード例 #1
0
	def ExtractData(filepath):
		ls_lines=FileUtil.ReadLines(filepath)
		ls_record=[]
		for line in ls_lines:
			#print(line[:-1].split(';'))
			ls_record.append(line[:-1].split(';'))

		return ls_record
コード例 #2
0
    def is_valid_access_request(req_args):
        #Get account address
        addr_client = req_args.json['host_address']
        url_rule = req_args.json['url_rule']

        #Define ls_time_exec to save executing time to log
        ls_time_exec = []

        # define branch control flag
        query_src = 0  # smart contract:0, local cache:1
        is_cachetoken = 0  # cache data:1, not cache data:0

        #get token data
        start_time = time.time()

        if (query_src == 0):
            # ----------a) query token from smart contract ------------
            token_data = CapPolicy.get_token(addr_client)
            #print(token_data)

            if (is_cachetoken == 1):
                # 2) Save token data to local token.dat
                FileUtil.AddLine('ACtoken.dat',
                                 TypesUtil.json_to_string(token_data))
        else:
            # ----------b) read authToken from local cached file ------------
            read_token = FileUtil.ReadLines('ACtoken.dat')
            token_data = TypesUtil.string_to_json(read_token[0])
            #print(token_data)

        exec_time = time.time() - start_time
        ls_time_exec.append(format(exec_time * 1000, '.3f'))
        print("Execution time of get_token is:%2.6f" % (exec_time))

        #extract access action from request
        access_data = {}
        access_data['url_rule'] = url_rule
        access_data['method'] = req_args.method
        #print(access_data)

        start_time = time.time()
        if (not CapPolicy.is_token_valid(token_data)):
            print('token valid fail')
            return False
        exec_time = time.time() - start_time
        ls_time_exec.append(format(exec_time * 1000, '.3f'))
        print("Execution time of is_token_valid is:%2.6f" % (exec_time))

        start_time = time.time()
        if (not CapPolicy.is_access_valid(token_data, access_data)):
            print('access valid fail')
            return False
        exec_time = time.time() - start_time
        ls_time_exec.append(format(exec_time * 1000, '.3f'))
        print("Execution time of is_access_valid is:%2.6f" % (exec_time))

        #transfer list to string
        str_time_exec = " ".join(ls_time_exec)
        #print(str_time_exec)
        FileUtil.AddLine('capac_exec_time_server.log', str_time_exec)

        return True
コード例 #3
0
    def verify_AuthToken(req_args):
        # extract client address from req_args
        #addr_client = req_args['host_address']
        addr_client = req_args.json['host_address']
        #print(addr_client)

        # Define ls_time_exec to save executing time to log
        ls_time_exec = []

        # define branch control flag
        query_src = 0  # smart contract:0, local cache:1
        is_cachetoken = 0  # cache data:1, not cache data:0

        # mark the start time
        start_time = time.time()

        if (query_src == 0):
            # ----------a) query token from smart contract ------------
            # 1) get host Vnode data in contract
            accounts = myAuthToken.getAccounts()
            json_VNode_host = AuthPolicy.get_VNodeInfo(accounts[0])

            #2) get client Vnode in contract
            json_VNode_client = AuthPolicy.get_VNodeInfo(addr_client)
            #print(json_VNode_host)
            #print(json_VNode_client)

            if (is_cachetoken == 1):
                json_authToken = {}
                json_authToken['host'] = json_VNode_host
                json_authToken['client'] = json_VNode_client
                #print(json_authToken)

                # 2) Save token data to local token.dat
                FileUtil.AddLine('authToken.dat',
                                 TypesUtil.json_to_string(json_authToken))
        else:
            # ----------b) read authToken from local cached file ------------
            # 3) read token from local data, low overload
            read_token = FileUtil.ReadLines('authToken.dat')
            token_data = TypesUtil.string_to_json(read_token[0])
            json_VNode_host = token_data['host']
            json_VNode_client = token_data['client']

        print("localhost: %s | client: %s" %
              (json_VNode_host, json_VNode_client))

        #3) authicate identity based on token
        # compare
        ret_indexAuth = (
            json_VNode_host['VZoneID'] == json_VNode_client['VZoneID'])

        # calculate computational cost
        exec_time = time.time() - start_time
        ls_time_exec.append(format(exec_time * 1000, '.3f'))
        print("Execution time of %s authentication is:%2.6f" %
              (addr_client, exec_time))

        #transfer list to string
        str_time_exec = " ".join(ls_time_exec)
        #print(str_time_exec)
        FileUtil.AddLine('auth_exec_time_server.log', str_time_exec)

        #return index authentication result
        return ret_indexAuth