# -*- coding: utf-8 -*- """ Copyright (C) 2017 Sebastian Golasch (plugin.video.netflix) Copyright (C) 2018 Caphm (original implementation module) Copyright (C) 2019 Stefano Gottardo - @CastagnaIT Handle the authentication access SPDX-License-Identifier: MIT See LICENSES/MIT.md for more information. """ import re import resources.lib.utils.website as website import resources.lib.common as common import resources.lib.utils.cookies as cookies import resources.lib.kodi.ui as ui from resources.lib.common.exceptions import (LoginValidateError, NotConnected, NotLoggedInError, MbrStatusNeverMemberError, MbrStatusFormerMemberError, LoginError, MissingCredentialsError, MbrStatusAnonymousError, WebsiteParsingError) from resources.lib.globals import G from resources.lib.services.nfsession.session.cookie import SessionCookie from resources.lib.services.nfsession.session.http_requests import SessionHTTPRequests from resources.lib.utils.logging import LOG, measure_exec_time_decorator class SessionAccess(SessionCookie, SessionHTTPRequests): """Handle the authentication access""" @measure_exec_time_decorator(is_immediate=True) def prefetch_login(self): """Check if we have stored credentials. If so, do the login before the user requests it""" from requests import exceptions try: common.get_credentials() if not self.is_logged_in(): self.login() return True except MissingCredentialsError: pass except exceptions.RequestException as exc: # It was not possible to connect to the web service, no connection, network problem, etc import traceback LOG.error('Login prefetch: request exception {}', exc) LOG.debug(traceback.format_exc()) except Exception as exc: # pylint: disable=broad-except LOG.warn('Login prefetch: failed {}', exc) return False def assert_logged_in(self): """Raise an exception when login cannot be established or maintained""" if not common.is_internet_connected(): raise NotConnected('Internet connection not available') if not self.is_logged_in(): raise NotLoggedInError def is_logged_in(self): """Check if there are valid login data""" return self._load_cookies() and self._verify_session_cookies() def get_safe(self, endpoint, **kwargs): """ Before execute a GET request to the designated endpoint, check the connection and the validity of the login """ self.assert_logged_in() return self.get(endpoint, **kwargs) def post_safe(self, endpoint, **kwargs): """ Before execute a POST request to the designated endpoint, check the connection and the validity of the login """ self.assert_logged_in() return self.post(endpoint, **kwargs) @measure_exec_time_decorator(is_immediate=True) 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[^<]+>([^<]+@[^