def login(self, credentials=None): """Perform account login""" try: # First we get the authentication url without logging in, required for login API call react_context = website.extract_json(self.get('login'), 'reactContext') auth_url = website.extract_api_data(react_context)['auth_url'] LOG.debug('Logging in...') login_response = self.post( 'login', headers={'Accept-Language': _get_accept_language_string(react_context)}, data=_login_payload(credentials or common.get_credentials(), auth_url, react_context)) website.extract_session_data(login_response, validate=True, update_profiles=True) if credentials: # Save credentials only when login has succeeded common.set_credentials(credentials) LOG.info('Login successful') ui.show_notification(common.get_local_string(30109)) cookies.save(self.account_hash, self.session.cookies) return True except LoginValidateError as exc: self.session.cookies.clear() common.purge_credentials() raise_from(LoginError(unicode(exc)), exc) except (MbrStatusNeverMemberError, MbrStatusFormerMemberError) as exc: self.session.cookies.clear() LOG.warn('Membership status {} not valid for login', exc) raise_from(LoginError(common.get_local_string(30180)), exc) except Exception: # pylint: disable=broad-except self.session.cookies.clear() import traceback LOG.error(G.py2_decode(traceback.format_exc(), 'latin-1')) raise
def login_auth_data(self, data=None, password=None): """Perform account login with authentication data""" from requests import exceptions LOG.debug('Logging in with authentication data') # Add the cookies to the session self.session.cookies.clear() for cookie in data['cookies']: self.session.cookies.set(cookie[0], cookie[1], **cookie[2]) cookies.log_cookie(self.session.cookies) # Try access to website try: website.extract_session_data(self.get('browse'), validate=True, update_profiles=True) except MbrStatusAnonymousError: # Access not valid return False # Get the account e-mail page_response = self.get('your_account').decode('utf-8') email_match = re.search(r'account-email[^<]+>([^<]+@[^</]+)</', page_response) email = email_match.group(1).strip() if email_match else None if not email: raise WebsiteParsingError('E-mail field not found') # Verify the password (with parental control api) try: response = self.post_safe('profile_hub', data={ 'destination': 'contentRestrictions', 'guid': G.LOCAL_DB.get_active_profile_guid(), 'password': password, 'task': 'auth' }) if response.get('status') != 'ok': raise LoginError(common.get_local_string( 12344)) # 12344=Passwords entered did not match. except exceptions.HTTPError as exc: if exc.response.status_code == 500: # This endpoint raise HTTP error 500 when the password is wrong raise LoginError(common.get_local_string(12344)) from exc raise common.set_credentials({'email': email, 'password': password}) LOG.info('Login successful') ui.show_notification(common.get_local_string(30109)) cookies.save(self.session.cookies) return True
def login_auth_data(self, data=None, password=None): """Perform account login with authentication data""" LOG.debug('Logging in with authentication data') # Add the cookies to the session self.session.cookies.clear() for cookie in data['cookies']: # The code below has been adapted from httpx.Cookies.set() method kwargs = { 'version': 0, 'name': cookie['name'], 'value': cookie['value'], 'port': None, 'port_specified': False, 'domain': cookie['domain'], 'domain_specified': bool(cookie['domain']), 'domain_initial_dot': cookie['domain'].startswith('.'), 'path': cookie['path'], 'path_specified': bool(cookie['path']), 'secure': cookie['secure'], 'expires': cookie['expires'], 'discard': True, 'comment': None, 'comment_url': None, 'rest': cookie['rest'], 'rfc2109': False, } cookie = Cookie(**kwargs) self.session.cookies.jar.set_cookie(cookie) cookies.log_cookie(self.session.cookies.jar) # Try access to website try: website.extract_session_data(self.get('browse'), validate=True, update_profiles=True) except MbrStatusAnonymousError: # Access not valid return False # Get the account e-mail page_response = self.get('your_account').decode('utf-8') email_match = re.search(r'account-email[^<]+>([^<]+@[^</]+)</', page_response) email = email_match.group(1).strip() if email_match else None if not email: raise WebsiteParsingError('E-mail field not found') # Verify the password (with parental control api) try: response = self.post_safe('profile_hub', data={ 'destination': 'contentRestrictions', 'guid': G.LOCAL_DB.get_active_profile_guid(), 'password': password, 'task': 'auth' }) if response.get('status') != 'ok': raise LoginError(common.get_local_string( 12344)) # 12344=Passwords entered did not match. except httpx.HTTPStatusError as exc: if exc.response.status_code == 500: # This endpoint raise HTTP error 500 when the password is wrong raise LoginError(common.get_local_string(12344)) from exc raise common.set_credentials({'email': email, 'password': password}) LOG.info('Login successful') ui.show_notification(common.get_local_string(30109)) cookies.save(self.session.cookies.jar) return True