class documentation

Representation of a bearer token with optional expiration.

Behaviour notes

  • str(Token) returns a sanitized representation using nebius.base.token_sanitizer.TokenSanitizer to avoid logging secrets in plaintext. When expiration is present, the ISO timestamp and a relative expires_in delta are included in the representation.
  • to_dict serializes the token into a mapping with token and expires_at keys, where expires_at is an integer POSIX timestamp (or None when no expiration was set).
Parameters
tokenThe raw token string. May be empty for an "empty" token.
expirationOptional UTC-aware expiration datetime. If provided, is_expired checks against this value.
Class Method empty Create an empty token.
Class Method from_dict Create a Token from a mapping produced by to_dict.
Method __eq__ Equality comparison.
Method __init__ Initialize a token with these token string and expiration
Method __str__ Return a short, sanitized string representation.
Method is_empty Return True when the token string is empty.
Method is_expired Return True when the token is expired.
Method to_dict Serialize the token to a mapping.
Property expiration The UTC-aware expiration datetime, or None if not set.
Property token The raw token string.
Instance Variable _exp Undocumented
Instance Variable _tok Undocumented
@classmethod
def empty(cls) -> Token: (source)

Create an empty token.

Returns
TokenA Token instance representing the absence of a credential (empty token string).
@classmethod
def from_dict(cls, data: Any) -> Token: (source)

Create a Token from a mapping produced by to_dict.

Parameters
data:typing.AnyMapping with at least the token key and optional expires_at integer timestamp.
Returns
TokenA Token instance parsed from the mapping.
Raises
ValueErrorWhen the input type is invalid or contains incompatible types.
def __eq__(self, value: object) -> bool: (source)

Equality comparison.

Two tokens are equal when both their token strings and expiration datetimes are equal.

Parameters
value:objectOther object to compare.
Returns
boolTrue when equal, otherwise False.
def __init__(self, token: str, expiration: datetime | None = None): (source)

Initialize a token with these token string and expiration

def __str__(self) -> str: (source)

Return a short, sanitized string representation.

Returns
strA one-line safe representation of the token for debugging.
def is_empty(self) -> bool: (source)

Return True when the token string is empty.

Returns
boolTrue when this token represents no credential.
def is_expired(self) -> bool: (source)

Return True when the token is expired.

When expiration is None, tokens are considered non-expiring and this method returns False.

Returns
boolTrue if the current UTC time is equal to or after the expiration instant.
def to_dict(self) -> dict[str, Any]: (source)

Serialize the token to a mapping.

The mapping contains token and expires_at. expires_at is an integer POSIX timestamp when an expiration is set, otherwise None.

Returns
dictA dictionary serializable to JSON.

The UTC-aware expiration datetime, or None if not set.

The raw token string.

Undocumented

Undocumented