123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- import sys
- import threading
- from typing import Any, Callable, Optional, TYPE_CHECKING
- from UM.Logger import Logger
- got_server_type = False
- try:
- from cura.OAuth2.AuthorizationRequestServer import AuthorizationRequestServer
- from cura.OAuth2.AuthorizationRequestHandler import AuthorizationRequestHandler
- got_server_type = True
- except PermissionError:
- Logger.error("Can't start a server due to a PermissionError when starting the http.server.")
- if TYPE_CHECKING:
- from cura.OAuth2.Models import AuthenticationResponse
- from cura.OAuth2.AuthorizationHelpers import AuthorizationHelpers
- class LocalAuthorizationServer:
- def __init__(self, auth_helpers: "AuthorizationHelpers",
- auth_state_changed_callback: Callable[["AuthenticationResponse"], Any],
- daemon: bool) -> None:
- """The local LocalAuthorizationServer takes care of the oauth2 callbacks.
- Once the flow is completed, this server should be closed down again by calling
- :py:meth:`cura.OAuth2.LocalAuthorizationServer.LocalAuthorizationServer.stop()`
- :param auth_helpers: An instance of the authorization helpers class.
- :param auth_state_changed_callback: A callback function to be called when the authorization state changes.
- :param daemon: Whether the server thread should be run in daemon mode.
- .. note::
- Daemon threads are abruptly stopped at shutdown. Their resources (e.g. open files) may never be released.
- """
- self._web_server = None
- self._web_server_thread = None
- self._web_server_port = auth_helpers.settings.CALLBACK_PORT
- self._auth_helpers = auth_helpers
- self._auth_state_changed_callback = auth_state_changed_callback
- self._daemon = daemon
- def start(self, verification_code: str, state: str) -> None:
- """Starts the local web server to handle the authorization callback.
- :param verification_code: The verification code part of the OAuth2 client identification.
- :param state: The unique state code (to ensure that the request we get back is really from the server.
- """
- if self._web_server:
-
-
- Logger.log("d", "Auth web server was already running. Updating the verification code")
- self._web_server.setVerificationCode(verification_code)
- return
- if self._web_server_port is None:
- raise Exception("Unable to start server without specifying the port.")
- Logger.log("d", "Starting local web server to handle authorization callback on port %s", self._web_server_port)
-
- if got_server_type:
- self._web_server = AuthorizationRequestServer(("0.0.0.0", self._web_server_port), AuthorizationRequestHandler)
- self._web_server.setAuthorizationHelpers(self._auth_helpers)
- self._web_server.setAuthorizationCallback(self._auth_state_changed_callback)
- self._web_server.setVerificationCode(verification_code)
- self._web_server.setState(state)
-
- self._web_server_thread = threading.Thread(None, self._serve_forever, daemon = self._daemon)
- self._web_server_thread.start()
- def stop(self) -> None:
- """Stops the web server if it was running. It also does some cleanup."""
- Logger.log("d", "Stopping local oauth2 web server...")
- if self._web_server:
- try:
- self._web_server.shutdown()
- except OSError:
-
- pass
- Logger.log("d", "Local oauth2 web server was shut down")
- self._web_server = None
- self._web_server_thread = None
- def _serve_forever(self) -> None:
- """
- If the platform is windows, this function calls the serve_forever function of the _web_server, catching any
- OSErrors that may occur in the thread, thus making the reported message more log-friendly.
- If it is any other platform, it just calls the serve_forever function immediately.
- :return: None
- """
- Logger.log("d", "Local web server for authorization has started")
- if self._web_server:
- if sys.platform == "win32":
- try:
- self._web_server.serve_forever()
- except OSError:
- Logger.logException("w", "An exception happened while serving the auth server")
- else:
-
- self._web_server.serve_forever()
|