def _web_login(self): """ Executes a login and returns the JSON Web Token. :rtype str """ # Yes, we have accepted the cookies util.SESSION.cookies.clear() util.SESSION.cookies.set('authId', str(uuid4())) # Start login flow util.http_get('https://vtm.be/vtmgo/aanmelden?redirectUrl=https://vtm.be/vtmgo') # Send login credentials try: response = util.http_post('https://login2.vtm.be/login?client_id=vtm-go-web', form={ 'userName': self._username, 'password': self._password, 'jsEnabled': 'true', }) except HTTPError as exc: if exc.response.status_code == 400: raise InvalidLoginException() raise if 'errorBlock-OIDC-004' in response.text: # E-mailadres is niet gekend. raise InvalidLoginException() if 'errorBlock-OIDC-003' in response.text: # Wachtwoord is niet correct. raise InvalidLoginException() if 'OIDC-999' in response.text: # Ongeldige login. raise InvalidLoginException() # Follow login response = util.http_get('https://login2.vtm.be/authorize/continue?client_id=vtm-go-web') # Extract state and code matches_state = re.search(r'name="state" value="([^"]+)', response.text) if matches_state: state = matches_state.group(1) else: raise LoginErrorException(code=101) # Could not extract authentication code matches_code = re.search(r'name="code" value="([^"]+)', response.text) if matches_code: code = matches_code.group(1) else: raise LoginErrorException(code=101) # Could not extract authentication code # Okay, final stage. We now need to POST our state and code to get a valid JWT. util.http_post('https://vtm.be/vtmgo/login-callback', form={ 'state': state, 'code': code, }) # Get JWT from cookies self._account.jwt_token = util.SESSION.cookies.get('lfvp_auth') self._save_cache() return self._account
def http_get(url, params=None, token=None, profile=None, headers=None): """ Make a HTTP GET request for the specified URL. :param str url: The URL to call. :param dict params: The query parameters to include to the URL. :param str token: The token to use in Bearer authentication. :param str profile: The profile to use in authentication. :param dict headers: A dictionary with additional headers. :returns: The HTTP Response object. :rtype: requests.Response """ try: return _request('GET', url=url, params=params, token=token, profile=profile, headers=headers) except HTTPError as exc: if exc.response.status_code == 401: raise InvalidTokenException(exc) if exc.response.status_code == 403: raise InvalidLoginException(exc) if exc.response.status_code == 404: raise UnavailableException(exc) if exc.response.status_code == 429: raise LimitReachedException(exc) raise
def http_put(url, params=None, form=None, data=None, token=None, profile=None, headers=None, proxies=None): """ Make a HTTP PUT request for the specified URL. :param str url: The URL to call. :param dict params: The query parameters to include to the URL. :param dict form: A dictionary with form parameters to POST. :param dict data: A dictionary with json parameters to POST. :param str token: The token to use in Bearer authentication. :param str profile: The profile to use in authentication. :param dict headers: A dictionary with additional headers. :returns: The HTTP Response object. :rtype: requests.Response """ try: return _request('PUT', url=url, params=params, form=form, data=data, token=token, profile=profile, headers=headers, proxies=proxies) except HTTPError as exc: if exc.response.status_code == 401: raise InvalidTokenException(exc) if exc.response.status_code == 403: raise InvalidLoginException(exc) if exc.response.status_code == 404: raise UnavailableException(exc) raise
def _android_login(self): """ Executes an android login and returns the JSON Web Token. :rtype str """ # We should start fresh util.SESSION.cookies.clear() # Start login flow util.http_get('https://login2.vtm.be/authorize', params={ 'client_id': 'vtm-go-android', 'response_type': 'id_token', 'scope': 'openid email profile address phone', 'nonce': 1550073732654, 'sdkVersion': '0.13.1', 'state': 'dnRtLWdvLWFuZHJvaWQ=', # vtm-go-android 'redirect_uri': 'https://login2.vtm.be/continue', }) # Send login credentials try: response = util.http_post('https://login2.vtm.be/login', params={ 'client_id': 'vtm-go-android', }, form={ 'userName': self._username, 'password': self._password, }) except HTTPError as exc: if exc.response.status_code == 400: raise InvalidLoginException() raise if 'errorBlock-OIDC-004' in response.text: # E-mailadres is niet gekend. raise InvalidLoginException() if 'errorBlock-OIDC-003' in response.text: # Wachtwoord is niet correct. raise InvalidLoginException() if 'OIDC-999' in response.text: # Ongeldige login. raise InvalidLoginException() # Extract redirect match = re.search(r"window.location.href = '([^']+)'", response.text) if not match: raise LoginErrorException(code=103) redirect_url = match.group(1) # Follow login response = util.http_get(redirect_url) # We are redirected and our id_token is in the fragment of the redirected url params = parse_qs(urlparse(response.url).fragment) id_token = params['id_token'][0] # Okay, final stage. We now need to authorize our id_token so we get a valid JWT. response = util.http_post('https://lfvp-api.dpgmedia.net/vtmgo/tokens', data={ 'idToken': id_token, }) # Get JWT from reply self._account.jwt_token = json.loads(response.text).get('lfvpToken') self._save_cache() return self._account