# Path: craft_ai/errors.py
# class CraftAiTokenError(CraftAiError):
#     """An invalid token error occured."""


import base64
import binascii
import json
from craft_ai.errors import CraftAiTokenError
        header_segment, payload_segment = signing_input.split(b".", 1)
    except ValueError:
        raise CraftAiTokenError("Not enough segments")

    try:
        header_data = base64url_decode(header_segment)
    except (TypeError, binascii.Error):
        raise CraftAiTokenError("Invalid header padding")

    try:
        header = json.loads(header_data.decode("utf-8"))
    except ValueError as e:
        raise CraftAiTokenError("Invalid header string '%s'" % e)

    try:
        payload_data = base64url_decode(payload_segment)
    except (TypeError, binascii.Error):
        raise CraftAiTokenError("Unable to decode the payload segment of the token")

    try:
        payload = json.loads(payload_data.decode("utf-8"))
    except ValueError:
        # Unable to load the payload as a json
        payload = payload_data

    try:
        signature = base64url_decode(crypto_segment)
    except (TypeError, binascii.Error):
        raise CraftAiTokenError("Unable to decode the crypto segment of the token")


# Path: craft_ai/errors.py
# class CraftAiTokenError(CraftAiError):
#     """An invalid token error occured."""


# Path: craft_ai/jwt_decode.py
import base64
import binascii
import json
from craft_ai.errors import CraftAiTokenError

        header_segment, payload_segment = signing_input.split(b".", 1)
    except ValueError:
        raise CraftAiTokenError("Not enough segments")

    try:
        header_data = base64url_decode(header_segment)
    except (TypeError, binascii.Error):
        raise CraftAiTokenError("Invalid header padding")

    try:
        header = json.loads(header_data.decode("utf-8"))
    except ValueError as e:
        raise CraftAiTokenError("Invalid header string '%s'" % e)

    try:
        payload_data = base64url_decode(payload_segment)
    except (TypeError, binascii.Error):
        raise CraftAiTokenError("Unable to decode the payload segment of the token")

    try:
        payload = json.loads(payload_data.decode("utf-8"))
    except ValueError:
        # Unable to load the payload as a json
        payload = payload_data

    try:
        signature = base64url_decode(crypto_segment)
    except (TypeError, binascii.Error):
        raise CraftAiTokenError("Unable to decode the crypto segment of the token")


    return (payload, signing_input, header, signature)