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

Code before:
                with contextlib.suppress(IndexError):
                    yield obj.group(key)
                    return

            if not isinstance(key, str):
                return

            yield next((v for k, v in obj.groupdict().items() if casefold(k) == key), None)

        else:
            if is_user_input:
                key = (int_or_none(key) if ':' not in key
                       else slice(*map(int_or_none, key.split(':'))))

            if not isinstance(key, (int, slice)):
                return

            if not is_sequence(obj):
                if not traverse_string:
                    return
                obj = str(obj)

            with contextlib.suppress(IndexError):
                yield obj[key]

    def apply_path(start_obj, path):
        objs = (start_obj,)
        has_branched = False

        for key in variadic(path):
            if is_user_input and key == ':':
                key = ...

            if not casesense and isinstance(key, str):
                key = key.casefold()

            if key is ... or isinstance(key, (list, tuple)) or callable(key):
                has_branched = True

            key_func = functools.partial(apply_key, key)
            objs = itertools.chain.from_iterable(map(key_func, objs))

        return has_branched, objs

    def _traverse_obj(obj, path, use_list=True):
        has_branched, results = apply_path(obj, path)
        results = LazyList(x for x in map(type_test, results) if x is not None)

        if get_all and has_branched:
            return results.exhaust() if results or use_list else None


Review comment (suggestion):
```suggestion
    payload_data = json.loads(base64.urlsafe_b64decode(f'{payload_b64}==='))
```
should work afair

Code after:


# create a JSON Web Signature (jws) with HS256 algorithm
# the resulting format is in JWS Compact Serialization
# implemented following JWT https://www.rfc-editor.org/rfc/rfc7519.html
# implemented following JWS https://www.rfc-editor.org/rfc/rfc7515.html
def jwt_encode_hs256(payload_data, key, headers={}):
    header_data = {
        'alg': 'HS256',
        'typ': 'JWT',
    }
    if headers:
        header_data.update(headers)
    header_b64 = base64.b64encode(json.dumps(header_data).encode())
    payload_b64 = base64.b64encode(json.dumps(payload_data).encode())
    h = hmac.new(key.encode(), header_b64 + b'.' + payload_b64, hashlib.sha256)
    signature_b64 = base64.b64encode(h.digest())
    token = header_b64 + b'.' + payload_b64 + b'.' + signature_b64
    return token


# can be extended in future to verify the signature and parse header and return the algorithm used if it's not HS256
def jwt_decode_hs256(jwt):
    header_b64, payload_b64, signature_b64 = jwt.split('.')
    # add trailing ='s that may have been stripped, superfluous ='s are ignored
    payload_data = json.loads(base64.urlsafe_b64decode(f'{payload_b64}==='))
    return payload_data


WINDOWS_VT_MODE = False if compat_os_name == 'nt' else None


@functools.cache
def supports_terminal_sequences(stream):
    if compat_os_name == 'nt':
        if not WINDOWS_VT_MODE:
            return False
    elif not os.getenv('TERM'):
        return False
    try:
        return stream.isatty()
    except BaseException:
        return False


def windows_enable_vt_mode():  # TODO: Do this the proper way https://bugs.python.org/issue30075
    if get_windows_version() < (10, 0, 10586):
        return
    global WINDOWS_VT_MODE
    try:
        Popen.run('', shell=True)