def set_height(btc_addr, height): logger.info("CALLED /api/height/{0}/{1}".format(btc_addr, height)) error_msg = "Set height failed: {0}" try: user = Farmer(btc_addr) user.authenticate(dict(request.headers)) if height < app.config["HEIGHT_LIMIT"]: user.set_height(height) return make_response("Height accepted.", 200) else: msg = "Height limit exceeded." logger.warning(msg) raise OverflowError(msg) except OverflowError: msg = "Height limit exceeded." logger.warning(msg) return make_response(msg, 413) except ValueError: msg = "Invalid Bitcoin address." logger.warning(msg) return make_response(msg, 400) except LookupError: msg = "Farmer not found." logger.warning(msg) return make_response(msg, 404) except storjcore.auth.AuthError: msg = "Invalid authentication headers." logger.warning(msg) return make_response(error_msg.format(msg), 401)
def callback(): blockchain = BtcTxStore() wif = blockchain.create_key() address = blockchain.get_address(wif) farmer = Farmer(address) header_date = formatdate(timeval=mktime(datetime.now().timetuple()), localtime=True, usegmt=True) header_authorization = blockchain.sign_unicode(wif, "lalala-wrong") farmer.authenticate(header_authorization, header_date)
def callback(): blockchain = BtcTxStore() wif = blockchain.create_key() address = blockchain.get_address(wif) farmer = Farmer(address) header_date = formatdate(timeval=mktime(datetime.now().timetuple()) , localtime=True, usegmt=True) message = farmer.get_server_address() + " " + header_date header_authorization = blockchain.sign_unicode(wif, message) headers = {"Date": None, "Authorization": header_authorization} farmer.authenticate(headers)
def callback(): blockchain = BtcTxStore() wif = blockchain.create_key() address = blockchain.get_address(wif) farmer = Farmer(address) timeout = farmer.get_server_authentication_timeout() date = datetime.now() - timedelta(seconds=timeout) header_date = formatdate(timeval=mktime(date.timetuple()), localtime=True, usegmt=True) message = farmer.get_server_address() + " " + header_date header_authorization = blockchain.sign_unicode(wif, message) farmer.authenticate(header_authorization, header_date)
def callback(): blockchain = BtcTxStore() wif = blockchain.create_key() address = blockchain.get_address(wif) farmer = Farmer(address) header_date = formatdate(timeval=mktime( datetime.now().timetuple()), localtime=True, usegmt=True) message = farmer.get_server_address() + " " + header_date header_authorization = blockchain.sign_unicode(wif, message) headers = {"Date": None, "Authorization": header_authorization} farmer.authenticate(headers)
def set_height(btc_addr, height): # create Farmer object to represent user user = Farmer(btc_addr) user.authenticate(request.headers.get("Authorization"), request.headers.get("Date")) # attempt to set height try: user.set_height(height) return make_response("Height accepted.", 200) except ValueError: msg = "Invalid Bitcoin address." return make_response(msg, 400) except LookupError: msg = "Farmer not found." return make_response(msg, 404)
def set_height(btc_addr, height): # create Farmer object to represent user user = Farmer(btc_addr) user.authenticate(request.headers.get('Authorization'), request.headers.get('Date')) # attempt to set height try: user.set_height(height) return make_response("Height accepted.", 200) except ValueError: msg = "Invalid Bitcoin address." return make_response(msg, 400) except LookupError: msg = "Farmer not found." return make_response(msg, 404)
def ping(btc_addr): # create Farmer object to represent user user = Farmer(btc_addr) user.authenticate(request.headers.get("Authorization"), request.headers.get("Date")) # error template error_msg = "Ping Failed: {0}" # attempt to ping the farmer/farming address try: user.ping() return make_response("Ping accepted.", 200) except ValueError: msg = "Invalid Bitcoin address." return make_response(error_msg.format(msg), 400) except LookupError: msg = "Farmer not found." return make_response(error_msg.format(msg), 404)
def ping(btc_addr): # create Farmer object to represent user user = Farmer(btc_addr) user.authenticate(request.headers.get('Authorization'), request.headers.get('Date')) # error template error_msg = "Ping Failed: {0}" # attempt to ping the farmer/farming address try: user.ping() return make_response("Ping accepted.", 200) except ValueError: msg = "Invalid Bitcoin address." return make_response(error_msg.format(msg), 400) except LookupError: msg = "Farmer not found." return make_response(error_msg.format(msg), 404)
def test_authentication_success(self): blockchain = BtcTxStore() wif = blockchain.create_key() address = blockchain.get_address(wif) farmer = Farmer(address) header_date = formatdate(timeval=mktime(datetime.now().timetuple()), localtime=True, usegmt=True) message = farmer.get_server_address() + " " + header_date header_authorization = blockchain.sign_unicode(wif, message) self.assertTrue(farmer.authenticate(header_authorization, header_date))
def register_with_payout(btc_addr, payout_addr): logger.info("CALLED /api/register/{0}/{1}".format(btc_addr, payout_addr)) error_msg = "Registration Failed: {0}" try: user = Farmer(btc_addr) user.authenticate(dict(request.headers)) user.register(payout_addr) return make_response(user.to_json(), 200) except ValueError: msg = "Invalid Bitcoin address." logger.warning(msg) return make_response(error_msg.format(msg), 400) except LookupError: msg = "Address already is registered." logger.warning(msg) return make_response(error_msg.format(msg), 409) except storjcore.auth.AuthError: msg = "Invalid authentication headers." logger.warning(msg) return make_response(error_msg.format(msg), 401)
def register(btc_addr): date = request.headers.get("Date") authorization = request.headers.get("Authorization") # create Farmer object to represent user user = Farmer(btc_addr) user.authenticate(request.headers.get("Authorization"), request.headers.get("Date")) # error template error_msg = "Registration Failed: {0}" # attempt to register the farmer/farming address try: user.register() return make_response("User registered.", 200) except ValueError: msg = "Invalid Bitcoin address." return make_response(error_msg.format(msg), 400) except LookupError: msg = "Address already is registered." return make_response(error_msg.format(msg), 409)
def register(btc_addr): date = request.headers.get('Date') authorization = request.headers.get('Authorization') # create Farmer object to represent user user = Farmer(btc_addr) user.authenticate(request.headers.get('Authorization'), request.headers.get('Date')) # error template error_msg = "Registration Failed: {0}" # attempt to register the farmer/farming address try: user.register() return make_response("User registered.", 200) except ValueError: msg = "Invalid Bitcoin address." return make_response(error_msg.format(msg), 400) except LookupError: msg = "Address already is registered." return make_response(error_msg.format(msg), 409)
def test_authentication_timeout_future_success(self): blockchain = BtcTxStore() wif = blockchain.create_key() address = blockchain.get_address(wif) farmer = Farmer(address) timeout = farmer.get_server_authentication_timeout() - 5 date = datetime.now() + timedelta(seconds=timeout) header_date = formatdate(timeval=mktime(date.timetuple()), localtime=True, usegmt=True) message = farmer.get_server_address() + " " + header_date header_authorization = blockchain.sign_unicode(wif, message) headers = {"Date": header_date, "Authorization": header_authorization} self.assertTrue(farmer.authenticate(headers))