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

expired_in

DEFAULT: 0

Source code in authx/_internal/_signature.py
def __init__(self, secret_key, expired_in=0):
    """
    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

Source code in authx/_internal/_signature.py
def encode(self, dict_obj):
    return self.ser.dumps(dict_obj)

decode

decode(token)
PARAMETER DESCRIPTION
token

Source code in authx/_internal/_signature.py
def decode(self, token):
    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