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

Code before:
            del kwargs['fatal']
        query = kwargs.get('query', {}).copy()
        for _ in range(2):
            query['client_id'] = self._CLIENT_ID
            kwargs['query'] = query
            try:
                return super()._download_json(*args, **kwargs)
            except ExtractorError as e:
                if isinstance(e.cause, HTTPError) and e.cause.status in (401, 403):
                    self._store_client_id(None)
                    self._update_client_id()
                    continue
                elif non_fatal:
                    self.report_warning(error_to_compat_str(e))
                    return False
                raise

    def _initialize_pre_login(self):
        self._CLIENT_ID = self.cache.load('soundcloud', 'client_id') or 'a3e059563d7fd3372b49b37f00a00bcf'

    def _verify_oauth_token(self, token):
        if self._request_webpage(
                self._API_VERIFY_AUTH_TOKEN % (self._API_AUTH_QUERY_TEMPLATE % self._CLIENT_ID),
                None, note='Verifying login token...', fatal=False,
                data=json.dumps({'session': {'access_token': token}}).encode()):
            self._HEADERS = {'Authorization': f'OAuth {token}'}
            self.report_login()
        else:
            self.report_warning('Provided authorization token seems to be invalid. Continuing as guest')

    def _real_initialize(self):
        if self._HEADERS:
            return
        if token := try_call(lambda: self._get_cookies(self._BASE_URL)['oauth_token'].value):
            self._verify_oauth_token(token)

    def _perform_login(self, username, password):
        if username != 'oauth':
            raise ExtractorError(
                'Login using username and password is not currently supported. '
                + 'Use "--username oauth --password <oauth_token>" to login using an oauth token, '
                + f'or else {self._login_hint(method="cookies")}', expected=True)
        self._verify_oauth_token(password)

        r'''
        def genDevId():
            def genNumBlock():
                return ''.join([str(random.randrange(10)) for i in range(6)])
            return '-'.join([genNumBlock() for i in range(4)])

        payload = {

Review comment (security):
This will re-trigger verification for each subclass. Is that the intention? Otherwise, making `_HEADERS` a class variable and `self._HEADERS['Authorization'] = f'OAuth {token}'` will share it.

Code after:
            query['client_id'] = self._CLIENT_ID
            kwargs['query'] = query
            try:
                return super()._download_json(*args, **kwargs)
            except ExtractorError as e:
                if isinstance(e.cause, HTTPError) and e.cause.status in (401, 403):
                    self._store_client_id(None)
                    self._update_client_id()
                    continue
                elif non_fatal:
                    self.report_warning(error_to_compat_str(e))
                    return False
                raise

    def _initialize_pre_login(self):
        self._CLIENT_ID = self.cache.load('soundcloud', 'client_id') or 'a3e059563d7fd3372b49b37f00a00bcf'

    def _verify_oauth_token(self, token):
        if self._request_webpage(
                self._API_VERIFY_AUTH_TOKEN % (self._API_AUTH_QUERY_TEMPLATE % self._CLIENT_ID),
                None, note='Verifying login token...', fatal=False,
                data=json.dumps({'session': {'access_token': token}}).encode()):
            self._HEADERS['Authorization'] = f'OAuth {token}'
            self.report_login()
        else:
            self.report_warning('Provided authorization token is invalid. Continuing as guest')

    def _real_initialize(self):
        if self._HEADERS:
            return
        if token := try_call(lambda: self._get_cookies(self._BASE_URL)['oauth_token'].value):
            self._verify_oauth_token(token)

    def _perform_login(self, username, password):
        if username != 'oauth':
            raise ExtractorError(
                'Login using username and password is not currently supported. '
                'Use "--username oauth --password <oauth_token>" to login using an oauth token, '
                f'or else {self._login_hint(method="cookies")}', expected=True)
        if self._HEADERS:
            return
        self._verify_oauth_token(password)

        r'''
        def genDevId():
            def genNumBlock():
                return ''.join([str(random.randrange(10)) for i in range(6)])
            return '-'.join([genNumBlock() for i in range(4)])

        payload = {
            'client_id': self._CLIENT_ID,