LocalAuthorizationServer.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # Copyright (c) 2019 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import threading
  4. from typing import Optional, Callable, Any, TYPE_CHECKING
  5. from UM.Logger import Logger
  6. from cura.OAuth2.AuthorizationRequestServer import AuthorizationRequestServer
  7. from cura.OAuth2.AuthorizationRequestHandler import AuthorizationRequestHandler
  8. if TYPE_CHECKING:
  9. from cura.OAuth2.Models import AuthenticationResponse
  10. from cura.OAuth2.AuthorizationHelpers import AuthorizationHelpers
  11. class LocalAuthorizationServer:
  12. ## The local LocalAuthorizationServer takes care of the oauth2 callbacks.
  13. # Once the flow is completed, this server should be closed down again by
  14. # calling stop()
  15. # \param auth_helpers An instance of the authorization helpers class.
  16. # \param auth_state_changed_callback A callback function to be called when
  17. # the authorization state changes.
  18. # \param daemon Whether the server thread should be run in daemon mode.
  19. # Note: Daemon threads are abruptly stopped at shutdown. Their resources
  20. # (e.g. open files) may never be released.
  21. def __init__(self, auth_helpers: "AuthorizationHelpers",
  22. auth_state_changed_callback: Callable[["AuthenticationResponse"], Any],
  23. daemon: bool) -> None:
  24. self._web_server = None # type: Optional[AuthorizationRequestServer]
  25. self._web_server_thread = None # type: Optional[threading.Thread]
  26. self._web_server_port = auth_helpers.settings.CALLBACK_PORT
  27. self._auth_helpers = auth_helpers
  28. self._auth_state_changed_callback = auth_state_changed_callback
  29. self._daemon = daemon
  30. ## Starts the local web server to handle the authorization callback.
  31. # \param verification_code The verification code part of the OAuth2 client identification.
  32. def start(self, verification_code: str) -> None:
  33. if self._web_server:
  34. # If the server is already running (because of a previously aborted auth flow), we don't have to start it.
  35. # We still inject the new verification code though.
  36. self._web_server.setVerificationCode(verification_code)
  37. return
  38. if self._web_server_port is None:
  39. raise Exception("Unable to start server without specifying the port.")
  40. Logger.log("d", "Starting local web server to handle authorization callback on port %s", self._web_server_port)
  41. # Create the server and inject the callback and code.
  42. self._web_server = AuthorizationRequestServer(("0.0.0.0", self._web_server_port), AuthorizationRequestHandler)
  43. self._web_server.setAuthorizationHelpers(self._auth_helpers)
  44. self._web_server.setAuthorizationCallback(self._auth_state_changed_callback)
  45. self._web_server.setVerificationCode(verification_code)
  46. # Start the server on a new thread.
  47. self._web_server_thread = threading.Thread(None, self._web_server.serve_forever, daemon = self._daemon)
  48. self._web_server_thread.start()
  49. ## Stops the web server if it was running. It also does some cleanup.
  50. def stop(self) -> None:
  51. Logger.log("d", "Stopping local oauth2 web server...")
  52. if self._web_server:
  53. try:
  54. self._web_server.server_close()
  55. except OSError:
  56. # OS error can happen if the socket was already closed. We really don't care about that case.
  57. pass
  58. self._web_server = None
  59. self._web_server_thread = None