Authentication can now timeout and be denied from printer side

CURA-49
This commit is contained in:
Jaime van Kessel 2016-06-29 10:55:45 +02:00
parent c8ba60fad4
commit 3a767c72f0

View file

@ -77,13 +77,28 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice):
self._camera_image_id = 0 self._camera_image_id = 0
self._authentication_counter = 0
self._max_authentication_counter = 30 # Number of attempts before authentication timed out.
self._authentication_timer = QTimer()
self._authentication_timer.setInterval(1000) # TODO; Add preference for update interval
self._authentication_timer.setSingleShot(False)
self._authentication_timer.timeout.connect(self._onAuthenticationTimer)
self._authentication_state = AuthState.NotAuthenticated self._authentication_state = AuthState.NotAuthenticated
self._authentication_id = None self._authentication_id = None
self._authentication_key = None self._authentication_key = None
self._authentication_requested_message = Message(i18n_catalog.i18nc("@info:status", "Requested access. Please aprove the request on the printer"), lifetime = 0, dismissable = False) self._authentication_requested_message = Message(i18n_catalog.i18nc("@info:status", "Requested access. Please aprove the request on the printer"), lifetime = 0, dismissable = False, progress = 0)
self._camera_image = QImage() self._camera_image = QImage()
def _onAuthenticationTimer(self):
self._authentication_counter += 1
self._authentication_requested_message.setProgress(self._authentication_counter / self._max_authentication_counter * 100)
if self._authentication_counter > self._max_authentication_counter:
self._authentication_timer.stop()
self.setAuthenticationState(AuthState.AuthenticationDenied)
def _onAuthenticationRequired(self, reply, authenticator): def _onAuthenticationRequired(self, reply, authenticator):
if self._authentication_id is not None and self._authentication_key is not None: if self._authentication_id is not None and self._authentication_key is not None:
authenticator.setUser(self._authentication_id) authenticator.setUser(self._authentication_id)
@ -122,6 +137,7 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice):
def setAuthenticationState(self, auth_state): def setAuthenticationState(self, auth_state):
if auth_state == AuthState.AuthenticationRequested: if auth_state == AuthState.AuthenticationRequested:
self._authentication_requested_message.show() self._authentication_requested_message.show()
self._authentication_timer.start() # Start timer so auth will fail after a while.
elif auth_state == AuthState.Authenticated: elif auth_state == AuthState.Authenticated:
self._authentication_requested_message.hide() self._authentication_requested_message.hide()
elif auth_state == AuthState.AuthenticationDenied: elif auth_state == AuthState.AuthenticationDenied:
@ -130,8 +146,10 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice):
self._authentication_state = auth_state self._authentication_state = auth_state
def _update(self): def _update(self):
if self._authentication_state in [AuthState.NotAuthenticated, AuthState.AuthenticationRequested]: if self._authentication_state == AuthState.NotAuthenticated:
self._checkAuthentication() self._verifyAuthentication() # We don't know if we are authenticated; check if we have correct auth.
elif self._authentication_state == AuthState.AuthenticationRequested:
self._checkAuthentication() # We requested authentication at some point. Check if we got permission.
## Request 'general' printer data ## Request 'general' printer data
url = QUrl("http://" + self._address + self._api_prefix + "printer") url = QUrl("http://" + self._address + self._api_prefix + "printer")
printer_request = QNetworkRequest(url) printer_request = QNetworkRequest(url)
@ -170,7 +188,7 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice):
# Check if cartridges are loaded at all (Error) # Check if cartridges are loaded at all (Error)
#self._json_printer_state["heads"][0]["extruders"][0]["hotend"]["id"] != "" #self._json_printer_state["heads"][0]["extruders"][0]["hotend"]["id"] != ""
# Check if there is material loaded at all (Error) # Check if there is material loaded at all (Error)self.authentication_requested_message.setProgress(self._authentication_counter / self._max_authentication_counter)
#self._json_printer_state["heads"][0]["extruders"][0]["active_material"]["GUID"] != "" #self._json_printer_state["heads"][0]["extruders"][0]["active_material"]["GUID"] != ""
# Check if there is enough material (Warning) # Check if there is enough material (Warning)
@ -255,11 +273,14 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice):
Logger.log("e", "An exception occurred in network connection: %s" % str(e)) Logger.log("e", "An exception occurred in network connection: %s" % str(e))
## Verify if we are authenticated to make requests. ## Verify if we are authenticated to make requests.
def _checkAuthentication(self): def _verifyAuthentication(self):
url = QUrl("http://" + self._address + self._api_prefix + "auth/verify") url = QUrl("http://" + self._address + self._api_prefix + "auth/verify")
request = QNetworkRequest(url) request = QNetworkRequest(url)
self._manager.get(request) self._manager.get(request)
def _checkAuthentication(self):
self._manager.get(QNetworkRequest(QUrl("http://" + self._address + self._api_prefix + "auth/check/" + str(self._authentication_id))))
## Request a authentication key from the printer so we can be authenticated ## Request a authentication key from the printer so we can be authenticated
def _requestAuthentication(self): def _requestAuthentication(self):
url = QUrl("http://" + self._address + self._api_prefix + "auth/request") url = QUrl("http://" + self._address + self._api_prefix + "auth/request")
@ -306,7 +327,7 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice):
Logger.log("i", "Not authenticated. Attempting to request authentication") Logger.log("i", "Not authenticated. Attempting to request authentication")
self._requestAuthentication() self._requestAuthentication()
elif reply.attribute(QNetworkRequest.HttpStatusCodeAttribute) == 403: elif reply.attribute(QNetworkRequest.HttpStatusCodeAttribute) == 403:
print("403") pass
else: else:
self.setAuthenticationState(AuthState.Authenticated) self.setAuthenticationState(AuthState.Authenticated)
Logger.log("i", "Authentication succeeded") Logger.log("i", "Authentication succeeded")
@ -316,6 +337,9 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice):
if data.get("message", "") == "authorized": if data.get("message", "") == "authorized":
Logger.log("i", "Authentication completed.") Logger.log("i", "Authentication completed.")
self.setAuthenticationState(AuthState.Authenticated) self.setAuthenticationState(AuthState.Authenticated)
elif data.get("message", "") == "unauthorized":
Logger.log("i", "Authentication was denied.")
self.setAuthenticationState(AuthState.AuthenticationDenied)
else: else:
pass pass
#Logger.log("i", "Authentication was denied.") #Logger.log("i", "Authentication was denied.")
@ -329,8 +353,8 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice):
self._authentication_id = data["id"] self._authentication_id = data["id"]
Logger.log("i", "Got a new authentication ID. Waiting for authorization: %s", self._authentication_id ) Logger.log("i", "Got a new authentication ID. Waiting for authorization: %s", self._authentication_id )
# Continue with handshaking; send request to printer so it can be authenticated. # Check if the authentication is accepted.
self._manager.get(QNetworkRequest(QUrl("http://" + self._address + self._api_prefix + "auth/check/" + str(self._authentication_id)))) self._checkAuthentication()
else: else:
reply.uploadProgress.disconnect(self._onUploadProgress) reply.uploadProgress.disconnect(self._onUploadProgress)
self._progress_message.hide() self._progress_message.hide()