Skip to content

SignatureSerializer

authx._internal._signature.SignatureSerializer

SignatureSerializer(secret_key, expired_in=0)

A class that implements a URL-safe timed serializer.

Initialize the serializer with a secret key and an optional expiration time.

PARAMETER DESCRIPTION
secret_key

TYPE: str

expired_in

TYPE: int DEFAULT: 0

Source code in authx/_internal/_signature.py
def __init__(self, secret_key: str, expired_in: int = 0) -> None:
    """
    Initialize the serializer with a secret key and an optional expiration time.
    """
    self.ser = URLSafeTimedSerializer(secret_key)
    self.expired_in = expired_in

ser instance-attribute

ser = URLSafeTimedSerializer(secret_key)

expired_in instance-attribute

expired_in = expired_in

encode

encode(dict_obj)
PARAMETER DESCRIPTION
dict_obj

TYPE: Dict[str, Any]

Source code in authx/_internal/_signature.py
def encode(self, dict_obj: Dict[str, Any]) -> str:
    return self.ser.dumps(dict_obj)

decode

decode(token)
PARAMETER DESCRIPTION
token

TYPE: str

Source code in authx/_internal/_signature.py
def decode(self, token: str) -> Tuple[Optional[Dict[str, Any]], Optional[str]]:
    if token is None:
        return None, "NoTokenSpecified"
    try:
        if self.expired_in == 0:
            decoded_obj = self.ser.loads(token)
        else:
            decoded_obj = self.ser.loads(token, max_age=self.expired_in)
    except SignatureExpired:
        return None, "SignatureExpired"
    except BadTimeSignature:
        return None, "InvalidSignature"
    except Exception:
        return None, "BadSignature"  # Catch-all for other signature errors
    return decoded_obj, None