# 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



# Initial code retrieved from PyJWT
# cf. https://github.com/jpadilla/pyjwt/blob/ceff941/jwt/utils.py#L33-L42
def base64url_decode(base64_input):
    if isinstance(base64_input, str):
        base64_input = base64_input.encode("ascii")

    rem = len(base64_input) % 4

    if rem > 0:
        base64_input += b"=" * (4 - rem)

    return base64.urlsafe_b64decode(base64_input)


# Code inspired by PyJWT
# cf. https://github.com/jpadilla/pyjwt/blob/ceff941/jwt/api_jws.py#L144-L181
def jwt_decode(jwt):
    if isinstance(jwt, str):
        jwt = jwt.encode("utf-8")

    if not issubclass(type(jwt), bytes):

# 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




# Initial code retrieved from PyJWT
# cf. https://github.com/jpadilla/pyjwt/blob/ceff941/jwt/utils.py#L33-L42
def base64url_decode(base64_input):
    if isinstance(base64_input, str):
        base64_input = base64_input.encode("ascii")

    rem = len(base64_input) % 4

    if rem > 0:
        base64_input += b"=" * (4 - rem)

    return base64.urlsafe_b64decode(base64_input)


# Code inspired by PyJWT
# cf. https://github.com/jpadilla/pyjwt/blob/ceff941/jwt/api_jws.py#L144-L181
def jwt_decode(jwt):
    if isinstance(jwt, str):
        jwt = jwt.encode("utf-8")

    if not issubclass(type(jwt), bytes):

        raise CraftAiTokenError(