sinch.core.token_manager module

class sinch.core.token_manager.TokenState(value)[source]

Bases: Enum

Lifecycle state of the cached OAuth token.

VALID = 'VALID'

A usable token is currently cached.

INVALID = 'INVALID'

No token has been obtained yet.

EXPIRED = 'EXPIRED'

Deprecated since version 2.1: Kept for backward compatibility; will be removed in 3.0. No longer used by the SDK’s own renewal path (see TokenManager.refresh_auth_token()).

class sinch.core.token_manager.TokenManagerBase(sinch)[source]

Bases: ABC

Base class for OAuth token managers.

Holds the cached access token together with the lock that guards every token mutation.

token: OAuthToken | None
token_state: TokenState
abstractmethod get_auth_token() OAuthToken[source]
Return type:

OAuthToken

refresh_auth_token(
used_token: str,
) OAuthToken[source]

Renews the token after an expired-token 401, deduping concurrent renewals.

Parameters:

used_token (str) – The access token used by the request that received the 401.

Returns:

A valid token.

Return type:

OAuthToken

invalidate_expired_token() None[source]

Deprecated since version 2.1: Token renewal is handled by refresh_auth_token(); this method will be removed in 3.0.

Clears the cached token so the next call fetches a new one.

Return type:

None

handle_invalid_token(http_response) None[source]

Deprecated since version 2.1: Expired-token handling now lives in the HTTP transport’s request loop; this method will be removed in 3.0.

Invalidates the cached token if the response signals an expired token.

Return type:

None

set_auth_token(token: dict)[source]

Sets the OAuth token and marks the token_state as VALID.

Parameters:

token (dict) – The token fields.

Raises:

ValidationException – If the fields do not match the OAuthToken structure.

class sinch.core.token_manager.TokenManager(sinch)[source]

Bases: TokenManagerBase

Thread-safe synchronous OAuth token manager.

get_auth_token() OAuthToken[source]

Returns a valid token, fetching and caching one if none is cached yet.

This function is not a pure getter (not idempotent): on the first call it requests a token from the OAuth endpoint and stores it on the instance. Subsequent calls return the cached token. Caching is guarded by double-checked locking, so concurrent callers share a single token request instead of each issuing their own.

Returns:

A valid OAuth token.

Return type:

OAuthToken