AuthorizationService.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import json
  4. import webbrowser
  5. from typing import Optional, TYPE_CHECKING
  6. from urllib.parse import urlencode
  7. from UM.Logger import Logger
  8. from UM.Signal import Signal
  9. from cura.OAuth2.LocalAuthorizationServer import LocalAuthorizationServer
  10. from cura.OAuth2.AuthorizationHelpers import AuthorizationHelpers
  11. from cura.OAuth2.Models import AuthenticationResponse
  12. if TYPE_CHECKING:
  13. from cura.OAuth2.Models import UserProfile, OAuth2Settings
  14. from UM.Preferences import Preferences
  15. class AuthorizationService:
  16. """
  17. The authorization service is responsible for handling the login flow,
  18. storing user credentials and providing account information.
  19. """
  20. # Emit signal when authentication is completed.
  21. onAuthStateChanged = Signal()
  22. # Emit signal when authentication failed.
  23. onAuthenticationError = Signal()
  24. def __init__(self, settings: "OAuth2Settings", preferences: Optional["Preferences"] = None) -> None:
  25. self._settings = settings
  26. self._auth_helpers = AuthorizationHelpers(settings)
  27. self._auth_url = "{}/authorize".format(self._settings.OAUTH_SERVER_URL)
  28. self._auth_data = None # type: Optional[AuthenticationResponse]
  29. self._user_profile = None # type: Optional["UserProfile"]
  30. self._preferences = preferences
  31. self._server = LocalAuthorizationServer(self._auth_helpers, self._onAuthStateChanged, daemon=True)
  32. def initialize(self, preferences: Optional["Preferences"] = None) -> None:
  33. if preferences is not None:
  34. self._preferences = preferences
  35. if self._preferences:
  36. self._preferences.addPreference(self._settings.AUTH_DATA_PREFERENCE_KEY, "{}")
  37. # Get the user profile as obtained from the JWT (JSON Web Token).
  38. # If the JWT is not yet parsed, calling this will take care of that.
  39. # \return UserProfile if a user is logged in, None otherwise.
  40. # \sa _parseJWT
  41. def getUserProfile(self) -> Optional["UserProfile"]:
  42. if not self._user_profile:
  43. # If no user profile was stored locally, we try to get it from JWT.
  44. self._user_profile = self._parseJWT()
  45. if not self._user_profile:
  46. # If there is still no user profile from the JWT, we have to log in again.
  47. return None
  48. return self._user_profile
  49. # Tries to parse the JWT (JSON Web Token) data, which it does if all the needed data is there.
  50. # \return UserProfile if it was able to parse, None otherwise.
  51. def _parseJWT(self) -> Optional["UserProfile"]:
  52. if not self._auth_data or self._auth_data.access_token is None:
  53. # If no auth data exists, we should always log in again.
  54. return None
  55. user_data = self._auth_helpers.parseJWT(self._auth_data.access_token)
  56. if user_data:
  57. # If the profile was found, we return it immediately.
  58. return user_data
  59. # The JWT was expired or invalid and we should request a new one.
  60. if self._auth_data.refresh_token is None:
  61. return None
  62. self._auth_data = self._auth_helpers.getAccessTokenUsingRefreshToken(self._auth_data.refresh_token)
  63. if not self._auth_data or self._auth_data.access_token is None:
  64. # The token could not be refreshed using the refresh token. We should login again.
  65. return None
  66. return self._auth_helpers.parseJWT(self._auth_data.access_token)
  67. # Get the access token as provided by the repsonse data.
  68. def getAccessToken(self) -> Optional[str]:
  69. if not self.getUserProfile():
  70. # We check if we can get the user profile.
  71. # If we can't get it, that means the access token (JWT) was invalid or expired.
  72. return None
  73. if self._auth_data is None:
  74. return None
  75. return self._auth_data.access_token
  76. # Try to refresh the access token. This should be used when it has expired.
  77. def refreshAccessToken(self) -> None:
  78. if self._auth_data is None or self._auth_data.refresh_token is None:
  79. Logger.log("w", "Unable to refresh access token, since there is no refresh token.")
  80. return
  81. self._storeAuthData(self._auth_helpers.getAccessTokenUsingRefreshToken(self._auth_data.refresh_token))
  82. self.onAuthStateChanged.emit(logged_in=True)
  83. # Delete the authentication data that we have stored locally (eg; logout)
  84. def deleteAuthData(self) -> None:
  85. if self._auth_data is not None:
  86. self._storeAuthData()
  87. self.onAuthStateChanged.emit(logged_in=False)
  88. # Start the flow to become authenticated. This will start a new webbrowser tap, prompting the user to login.
  89. def startAuthorizationFlow(self) -> None:
  90. Logger.log("d", "Starting new OAuth2 flow...")
  91. # Create the tokens needed for the code challenge (PKCE) extension for OAuth2.
  92. # This is needed because the CuraDrivePlugin is a untrusted (open source) client.
  93. # More details can be found at https://tools.ietf.org/html/rfc7636.
  94. verification_code = self._auth_helpers.generateVerificationCode()
  95. challenge_code = self._auth_helpers.generateVerificationCodeChallenge(verification_code)
  96. # Create the query string needed for the OAuth2 flow.
  97. query_string = urlencode({
  98. "client_id": self._settings.CLIENT_ID,
  99. "redirect_uri": self._settings.CALLBACK_URL,
  100. "scope": self._settings.CLIENT_SCOPES,
  101. "response_type": "code",
  102. "state": "CuraDriveIsAwesome",
  103. "code_challenge": challenge_code,
  104. "code_challenge_method": "S512"
  105. })
  106. # Open the authorization page in a new browser window.
  107. webbrowser.open_new("{}?{}".format(self._auth_url, query_string))
  108. # Start a local web server to receive the callback URL on.
  109. self._server.start(verification_code)
  110. # Callback method for the authentication flow.
  111. def _onAuthStateChanged(self, auth_response: AuthenticationResponse) -> None:
  112. if auth_response.success:
  113. self._storeAuthData(auth_response)
  114. self.onAuthStateChanged.emit(logged_in=True)
  115. else:
  116. self.onAuthenticationError.emit(logged_in=False, error_message=auth_response.err_message)
  117. self._server.stop() # Stop the web server at all times.
  118. # Load authentication data from preferences.
  119. def loadAuthDataFromPreferences(self) -> None:
  120. if self._preferences is None:
  121. Logger.log("e", "Unable to load authentication data, since no preference has been set!")
  122. return
  123. try:
  124. preferences_data = json.loads(self._preferences.getValue(self._settings.AUTH_DATA_PREFERENCE_KEY))
  125. if preferences_data:
  126. self._auth_data = AuthenticationResponse(**preferences_data)
  127. self.onAuthStateChanged.emit(logged_in=True)
  128. except ValueError:
  129. Logger.logException("w", "Could not load auth data from preferences")
  130. # Store authentication data in preferences.
  131. def _storeAuthData(self, auth_data: Optional[AuthenticationResponse] = None) -> None:
  132. if self._preferences is None:
  133. Logger.log("e", "Unable to save authentication data, since no preference has been set!")
  134. return
  135. self._auth_data = auth_data
  136. if auth_data:
  137. self._user_profile = self.getUserProfile()
  138. self._preferences.setValue(self._settings.AUTH_DATA_PREFERENCE_KEY, json.dumps(vars(auth_data)))
  139. else:
  140. self._user_profile = None
  141. self._preferences.resetPreference(self._settings.AUTH_DATA_PREFERENCE_KEY)