import base64
import json


def retrieve_jwt_expiration_timestamp(jwt_value):
    # type: (str) -> int
    """
    Retrieves the expiration value from the JWT.

    :param str jwt_value: The JWT value.
    :returns: int
    """
    if not jwt_value:
        raise ValueError("jwt_value must be a non-empty string.")

    parts = jwt_value.split(".")

    if len(parts) < 3:
        raise ValueError("Invalid JWT structure. Expected a JWS Compact Serialization formatted value.")

    try:
        # JWT prefers no padding (see https://tools.ietf.org/id/draft-jones-json-web-token-02.html#base64urlnotes).
        # We pad the value with the max padding of === to keep our logic simple and allow the base64 decoder to handle
        # the value properly. b64decode will properly trim the padding appropriately, but apparently doesn't want to
        # handle the addition of padding.
        padded_base64_payload = base64.b64decode(parts[1] + "===").decode('utf-8')
        payload = json.loads(padded_base64_payload)
    except ValueError:
        raise ValueError("Unable to decode the JWT.")

    try:
        exp = payload['exp']
    except KeyError:
        raise ValueError("Invalid JWT payload structure. No expiration.")

    return int(exp)