Handle authentication denied messages

CURA-49
This commit is contained in:
Jaime van Kessel 2016-06-28 17:01:33 +02:00
parent 6244d74c1d
commit c13a6966d0

View file

@ -15,6 +15,13 @@ import json
i18n_catalog = i18nCatalog("cura") i18n_catalog = i18nCatalog("cura")
from enum import IntEnum
class AuthState(IntEnum):
NotAuthenticated = 1
AuthenticationRequested = 2
Authenticated = 3
AuthenticationDenied = 4
## Network connected (wifi / lan) printer that uses the Ultimaker API ## Network connected (wifi / lan) printer that uses the Ultimaker API
@signalemitter @signalemitter
@ -70,7 +77,7 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice):
self._camera_image_id = 0 self._camera_image_id = 0
self._authenticated = False self._authentication_state = AuthState.NotAuthenticated
self._authentication_id = None self._authentication_id = None
self._authentication_key = None self._authentication_key = None
@ -112,7 +119,7 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice):
self._manager.get(image_request) self._manager.get(image_request)
def _update(self): def _update(self):
if not self._authenticated: if self._authentication_state in [AuthState.NotAuthenticated, AuthState.AuthenticationRequested]:
self._checkAuthentication() self._checkAuthentication()
## 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")
@ -247,6 +254,7 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice):
url = QUrl("http://" + self._address + self._api_prefix + "auth/request") url = QUrl("http://" + self._address + self._api_prefix + "auth/request")
request = QNetworkRequest(url) request = QNetworkRequest(url)
request.setHeader(QNetworkRequest.ContentTypeHeader, "application/json") request.setHeader(QNetworkRequest.ContentTypeHeader, "application/json")
self._authentication_state = AuthState.AuthenticationRequested
self._manager.post(request, json.dumps({"application": "Cura", "user":"test"}).encode()) self._manager.post(request, json.dumps({"application": "Cura", "user":"test"}).encode())
## Handler for all requests that have finished. ## Handler for all requests that have finished.
@ -285,8 +293,16 @@ 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()
else: else:
self._authenticated = True self._authenticated = AuthState.Authenticated
Logger.log("i", "Authentication succeeded") Logger.log("i", "Authentication succeeded")
elif "auth/check" in reply.url().toString(): # Check if we are authenticated (user can refuse this!)
data = json.loads(bytes(reply.readAll()).decode("utf-8"))
if data.get("message", "") == "authorized":
Logger.log("i", "Authentication completed.")
self._authentication_state = AuthState.Authenticated
else:
Logger.log("i", "Authentication was denied.")
self._authentication_state = AuthState.AuthenticationDenied
elif reply.operation() == QNetworkAccessManager.PostOperation: elif reply.operation() == QNetworkAccessManager.PostOperation:
if "/auth/request" in reply.url().toString(): if "/auth/request" in reply.url().toString():