Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/vrt.py (Python)

Code before:

        refresh_token = self.cache.load(self._NETRC_MACHINE, 'refresh_token', default=None)
        if refresh_token and not self._is_jwt_token_expired(refresh_token):
            self.write_debug('Restored refresh token from cache')
            self._set_cookie('.www.vrt.be', 'vrtnu-site_profile_rt', refresh_token, path='/vrtmax/sso')
            return refresh_token

    def _fetch_video_token(self):
        video_token = self._get_video_token_from_cookie()
        if video_token and not self._is_jwt_token_expired(video_token):
            return video_token

        if self._get_login_info()[0]:
            video_token = self.cache.load(self._NETRC_MACHINE, 'video_token', default=None)
            if video_token and not self._is_jwt_token_expired(video_token):
                self.write_debug('Restored video token from cache')
                self._set_cookie('.www.vrt.be', 'vrtnu-site_profile_vt', video_token)
                return video_token

        refresh_token = self._fetch_refresh_token()
        if not refresh_token:
            return None

        self._download_webpage(
            'https://www.vrt.be/vrtmax/sso/login', None,
            'Refreshing video token', query={'scope': 'openid,mid'})

        video_token = self._get_video_token_from_cookie()
        if not video_token:
            self.cache.store(self._NETRC_MACHINE, 'refresh_token', None)
            self.report_warning('Refreshing of video token failed')
            return None
        if self._get_login_info()[0]:
            self.cache.store(self._NETRC_MACHINE, 'video_token', video_token)
        return video_token

    def _get_video_token_from_cookie(self):
        return try_call(lambda: self._get_cookies('https://www.vrt.be')['vrtnu-site_profile_vt'].value)

    def _get_refresh_token_from_cookie(self):
        return try_call(lambda: self._get_cookies('https://www.vrt.be/vrtmax/sso')['vrtnu-site_profile_rt'].value)

    def _perform_login(self, username, password):
        if self._fetch_refresh_token():
            self.write_debug('Refresh token already present, skipping login')
            return

        self._request_webpage(
            'https://www.vrt.be/vrtmax/sso/login', None,
            note='Getting session cookies', errnote='Failed to get session cookies')


Review comment (bug):
I noticed this doesn't actually work. Should be
```suggestion
        self._download_json(
            'https://www.vrt.be/vrtmax/sso/refresh', None,
            note='Refreshing video token', errnote='Failed to refresh video token')
```

Code after:
    '''

    def _fetch_tokens(self):
        has_credentials = self._get_login_info()[0]
        access_token = self._get_vrt_cookie(self._ACCESS_TOKEN_COOKIE_NAME)
        video_token = self._get_vrt_cookie(self._VIDEO_TOKEN_COOKIE_NAME)

        if (access_token and not self._is_jwt_token_expired(access_token)
                and video_token and not self._is_jwt_token_expired(video_token)):
            return access_token, video_token

        if has_credentials:
            access_token, video_token = self.cache.load(self._NETRC_MACHINE, 'token_data', default=(None, None))

            if (access_token and not self._is_jwt_token_expired(access_token)
                    and video_token and not self._is_jwt_token_expired(video_token)):
                self.write_debug('Restored tokens from cache')
                self._set_cookie(self._TOKEN_COOKIE_DOMAIN, self._ACCESS_TOKEN_COOKIE_NAME, access_token)
                self._set_cookie(self._TOKEN_COOKIE_DOMAIN, self._VIDEO_TOKEN_COOKIE_NAME, video_token)
                return access_token, video_token

        if not self._get_vrt_cookie(self._REFRESH_TOKEN_COOKIE_NAME):
            return None, None

        self._request_webpage(
            'https://www.vrt.be/vrtmax/sso/refresh', None,
            note='Refreshing tokens', errnote='Failed to refresh tokens', fatal=False)

        access_token = self._get_vrt_cookie(self._ACCESS_TOKEN_COOKIE_NAME)
        video_token = self._get_vrt_cookie(self._VIDEO_TOKEN_COOKIE_NAME)

        if not access_token or not video_token:
            self.cache.store(self._NETRC_MACHINE, 'refresh_token', None)
            self.cookiejar.clear(self._TOKEN_COOKIE_DOMAIN, '/vrtmax/sso', self._REFRESH_TOKEN_COOKIE_NAME)
            msg = 'Refreshing of tokens failed'
            if not has_credentials:
                self.report_warning(msg)
                return None, None
            self.report_warning(f'{msg}. Re-logging in')
            return self._perform_login(*self._get_login_info())

        if has_credentials:
            self.cache.store(self._NETRC_MACHINE, 'token_data', (access_token, video_token))

        return access_token, video_token

    def _get_vrt_cookie(self, cookie_name):
        # Refresh token cookie is scoped to /vrtmax/sso, others are scoped to /
        return try_call(lambda: self._get_cookies('https://www.vrt.be/vrtmax/sso')[cookie_name].value)

    @staticmethod