Exemple #1
0
def valid_signature(student=None):
    signature = request.headers.get('signature')
    if not signature:
        return False
    # Get the type of request and URI ex. GET /k12/nb2406
    message = request.method + ' ' + request.script_root + request.path
    if student:
        for (key, value) in student.iteritems():
            message = message + " " + str(value)
    hash_value = encrypt(message)
    if hash_value != int(signature):
        return False
    return True
Exemple #2
0
def send_request(request):

    #Look for the actual URL in input string.
    if "//" in request:
        index = request.find("//")
        index = index + 2
        route = request[index:]
        index = route.find("/")
        route = route[index:]

    else:
        index = request.find("/")
        route = request[index:]

    #Op should be either get, post, delete, or put.
    req_list = request.split(" ")
    op = req_list[0]

    #Must have 'http://' in url to make request using requests
    #module.
    url = req_list[1]
    if url.find("http") < 0:
        url = "http://" + url

    get = "GET"
    post = "POST"
    put = "PUT"
    delete = "DELETE"

    hash_input = op.upper() + " " + str(route)

    #Make get requests.
    if (op.lower() == get.lower()):
        hash_value = encrypt(hash_input)

        signature = "\"signature\": \"" + str(hash_value) + "\""

        header = "{" + signature + "}"
        header = json.loads(header)

        response = requests.get(url, headers=header)
        return response

    #Make delete requests.
    if (op.lower() == delete.lower()):
        hash_value = encrypt(hash_input)

        signature = "\"signature\": \"" + str(hash_value) + "\""
        header = "{" + signature + "}"
        header = json.loads(header)

        response = requests.delete(url, headers=header)
        return response

    #Extract data from post and put requests.
    url_list = request.split("\"")
    data = url_list[1]
    data = "{\"" + data
    data = data.replace('=', '\":\"').replace('&', '\", \"')
    data = data + "\"}"
    data = json.loads(data)

    index = request.find("\"")

    after_quote = request[index:]
    after_quote = after_quote[1:]

    index = after_quote.find("\"")
    index = index + 1

    #Recompute the URL based on something besides spaces.
    url = after_quote[index:]
    url = url.strip()

    #Must have 'http://' in url to make request using requests
    #module.
    if url.find("http") < 0:
        url = "http://" + url

    #Make post requests.
    if (op.lower() == post.lower()):
        hash_data = data

        for (key, value) in hash_data.iteritems():
            hash_input = hash_input + " " + str(value)

        hash_value = encrypt(hash_input)

        signature = "\"signature\": \"" + str(hash_value) + "\""

        header = "{" + signature + "}"
        header = json.loads(header)

        response = requests.post(url, data=data, headers=header)
        return response

    #Make put requests.
    if (op.lower() == put.lower()):
        hash_data = data

        for (key, value) in hash_data.iteritems():
            hash_input = hash_input + " " + str(value)

        hash_value = encrypt(hash_input)

        signature = "\"signature\": \"" + str(hash_value) + "\""

        header = "{" + signature + "}"
        header = json.loads(header)

        response = requests.put(url, data=data, headers=header)
        return response

    return "Error: Request is not get, post, put, or delete."
Exemple #3
0
def send_request(request):

	#Look for the actual URL in input string.
	if "//" in request:
		index = request.find("//")
		index = index+2
		route = request[index:]
		index = route.find("/")
		route = route[index:]
		
	else:
		index = request.find("/")
		route = request[index:]

	
	#Op should be either get, post, delete, or put.
	req_list = request.split(" ")
	op = req_list[0]

	#Must have 'http://' in url to make request using requests
	#module.
	url = req_list[1]
	if url.find("http") < 0:
		url = "http://" + url
	
	get = "GET"
	post = "POST"
	put = "PUT"
	delete = "DELETE"

	hash_input = op.upper() + " " + str(route)


	#Make get requests.
	if(op.lower() == get.lower()):
		hash_value = encrypt(hash_input)

		signature = "\"signature\": \"" + str(hash_value) + "\""
		
		header = "{" + signature + "}"	
		header = json.loads(header)


		response = requests.get(url, headers=header)
		return response


	#Make delete requests.
	if(op.lower() == delete.lower()):
		hash_value = encrypt(hash_input)

		signature = "\"signature\": \"" + str(hash_value) + "\""
		header = "{" + signature + "}"	
		header = json.loads(header)

		response = requests.delete(url, headers=header)
		return response


	#Extract data from post and put requests.
	url_list = request.split("\"")
	data = url_list[1]
	data = "{\"" + data
	data = data.replace('=', '\":\"').replace('&', '\", \"')
	data = data + "\"}"
	data = json.loads(data)

	index = request.find("\"")
	
	after_quote = request[index:]
	after_quote = after_quote[1:]
		
	index = after_quote.find("\"")
	index = index+1
	
	#Recompute the URL based on something besides spaces.
	url = after_quote[index:]
	url = url.strip()

	#Must have 'http://' in url to make request using requests
	#module.
	if url.find("http") < 0:
		url = "http://" + url
	

	#Make post requests.
	if(op.lower() == post.lower()):
		hash_data = data
		
		for (key, value) in hash_data.iteritems():
			hash_input = hash_input + " " + str(value)

		hash_value = encrypt(hash_input)

		signature = "\"signature\": \"" + str(hash_value) + "\""
		
		header = "{" + signature + "}"	
		header = json.loads(header)

		response = requests.post(url, data=data, headers=header)
		return response


	#Make put requests.
	if(op.lower() == put.lower()):
		hash_data = data
		
		for (key, value) in hash_data.iteritems():
			hash_input = hash_input + " " + str(value)

		hash_value = encrypt(hash_input)
		
		signature = "\"signature\": \"" + str(hash_value) + "\""
		
		header = "{" + signature + "}"	
		header = json.loads(header)

		response = requests.put(url, data=data, headers=header)
		return response


	return "Error: Request is not get, post, put, or delete."