def __init__( self, settings_repository, auth_token=None, refresh_token=None, username=None, password=None, ): self._settings_repository = settings_repository settings = self._settings_repository.get_settings() if auth_token and refresh_token: self._access_token = auth_token self._refresh_token = refresh_token elif username and password: r = requests.post( "{backend}/galileo/landing_zone/v1/oauth/token".format( backend=settings.backend ), json={ "username": username, "password": password, "grant_type": "password", }, ) if r.status_code != 200: raise ValueError( "Could not get Auth0 authentication tokens for this username and password combination" ) r = r.json() self._access_token = r["access_token"] self._refresh_token = r["refresh_token"]
def __init__( self, settings_repository, auth_token=None, refresh_token=None, username=None, password=None, ): """ Authenticates a user with a username and password or access token. :param settings_repository: Settings repository :type settings_repository: SettingsRepository :param auth_token: Access token, default None :type auth_token: str, optional :param refresh_token: Refresh token, default None :type refresh_token: str, optional :param username: Username, default None :type username: str, optional :param password: Password, default None :type password: str, optional """ self._settings_repository = settings_repository settings = self._settings_repository.get_settings() if auth_token and refresh_token: self._access_token = auth_token self._refresh_token = refresh_token elif username and password: r = requests.post( "{backend}/galileo/landing_zone/v1/oauth/token".format( backend=settings.backend), json={ "username": username, "password": password, "grant_type": "password", }, ) if r.status_code != 200: raise ValueError( "Could not get Auth0 authentication tokens for this username and password combination" ) r = r.json() self._access_token = r["access_token"] self._refresh_token = r["refresh_token"]
def _get_new_access_token(self, refresh_token, refresh_token_file): response = requests.post( "{domain}/oauth/token".format(domain=self.domain), headers=self.headers_default, data=urlencode( { "client_id": self.client_id, "grant_type": "refresh_token", "refresh_token": refresh_token, }, doseq=True, ), ) response.raise_for_status() r = response.json() self._store_token_info(refresh_token, r["expires_in"], refresh_token_file) return r["access_token"]
def _poll_for_tokens(self, interval, device_code): """ :param interval: string: time in seconds between polling for tokens. :param device_code: string: device code. :return: Tuple containing the access_token, refresh_token, and expiration. """ interval = interval * 2 url_str = urlencode( { "grant_type": "urn:ietf:params:oauth:grant-type:device_code", "device_code": device_code, "client_id": self.client_id, }, doseq=True, ) while True: try: r = requests.post( "{domain}/oauth/token".format(domain=self.domain), headers=self.headers_default, data=urlencode( { "grant_type": "urn:ietf:params:oauth:grant-type:device_code", "device_code": device_code, "client_id": self.client_id, }, doseq=True, ), ) except Exception as e: if hasattr(e, "code") and e.code == 429: interval = interval * 2 continue if r.status_code == 200: break time.sleep(interval) r = r.json() return r["access_token"], r["refresh_token"], r["expires_in"]
def _request_device_authorization(self): r = requests.post( "{domain}/oauth/device/code".format(domain=self.domain), headers=self.headers_default, data=urlencode( { "client_id": self.client_id, "audience": self.audience, "scope": "email profile openid offline_access", }, doseq=True, ), ) r = r.json() user_url = r["verification_uri"] user_code = r["user_code"] device_code = r["device_code"] interval = r["interval"] user_url_complete = r["verification_uri_complete"] return user_url, user_code, device_code, interval, user_url_complete