Skip to content

Albert Client Credentials

albert.AlbertClientCredentials

Bases: BaseAlbertModel, AuthManager

Client credentials manager for programmatic OAuth2 access to the Albert API.

This class implements the OAuth2 Client Credentials flow, allowing automated (non-interactive) systems to authenticate securely using a client ID and secret.

Attributes:

Name Type Description
id str

The client ID used for authentication.

secret SecretStr

The client secret used for authentication.

base_url str

The base URL of the Albert API.

Usage
creds = AlbertClientCredentials(
    id="your-client-id",
    secret=SecretStr("your-client-secret"),
    base_url="https://app.albertinvent.com",
)
client = Albert(auth_manager=creds)
client.roles.get_all()
Show JSON schema:
{
  "description": "Client credentials manager for programmatic OAuth2 access to the Albert API.\n\nThis class implements the OAuth2 Client Credentials flow, allowing automated\n(non-interactive) systems to authenticate securely using a client ID and secret.\n\nAttributes\n----------\nid : str\n    The client ID used for authentication.\nsecret : SecretStr\n    The client secret used for authentication.\nbase_url : str\n    The base URL of the Albert API.\n\nUsage\n-----\n```\ncreds = AlbertClientCredentials(\n    id=\"your-client-id\",\n    secret=SecretStr(\"your-client-secret\"),\n    base_url=\"https://app.albertinvent.com\",\n)\nclient = Albert(auth_manager=creds)\nclient.roles.get_all()\n```",
  "properties": {
    "id": {
      "title": "Id",
      "type": "string"
    },
    "secret": {
      "format": "password",
      "title": "Secret",
      "type": "string",
      "writeOnly": true
    },
    "base_url": {
      "title": "Base Url",
      "type": "string"
    }
  },
  "required": [
    "id",
    "secret"
  ],
  "title": "AlbertClientCredentials",
  "type": "object"
}

Fields:

id

id: str

secret

secret: SecretStr

base_url

base_url: str

oauth_token_url

oauth_token_url: str

Return the full URL to the OAuth token endpoint.

from_env

from_env(
    *,
    base_url_env: str = "ALBERT_BASE_URL",
    client_id_env: str = "ALBERT_CLIENT_ID",
    client_secret_env: str = "ALBERT_CLIENT_SECRET",
) -> AlbertClientCredentials | None

Create AlbertClientCredentials from environment variables.

Returns None if any of the required environment variables are missing.

Parameters:

Name Type Description Default
base_url_env str

Name of the environment variable containing the base URL (defaults to "ALBERT_BASE_URL").

'ALBERT_BASE_URL'
client_id_env str

Name of the environment variable containing the client ID (defaults to "ALBERT_CLIENT_ID").

'ALBERT_CLIENT_ID'
client_secret_env str

Name of the environment variable containing the client secret (defaults to "ALBERT_CLIENT_SECRET").

'ALBERT_CLIENT_SECRET'

Returns:

Type Description
AlbertClientCredentials | None

The credentials instance if all environment variables are present; otherwise, None.

Source code in src/albert/core/auth/credentials.py
@classmethod
def from_env(
    cls,
    *,
    base_url_env: str = "ALBERT_BASE_URL",
    client_id_env: str = "ALBERT_CLIENT_ID",
    client_secret_env: str = "ALBERT_CLIENT_SECRET",
) -> AlbertClientCredentials | None:
    """
    Create `AlbertClientCredentials` from environment variables.

    Returns None if any of the required environment variables are missing.

    Parameters
    ----------
    base_url_env : str
        Name of the environment variable containing the base URL
        (defaults to "ALBERT_BASE_URL").
    client_id_env : str
        Name of the environment variable containing the client ID
        (defaults to "ALBERT_CLIENT_ID").
    client_secret_env : str
        Name of the environment variable containing the client secret
        (defaults to "ALBERT_CLIENT_SECRET").

    Returns
    -------
    AlbertClientCredentials | None
        The credentials instance if all environment variables are present;
        otherwise, None.
    """
    base_url = os.getenv(base_url_env)
    client_id = os.getenv(client_id_env)
    client_secret = os.getenv(client_secret_env)

    if client_id and client_secret and base_url:
        return cls(
            id=client_id,
            secret=SecretStr(client_secret),
            base_url=base_url,
        )

get_access_token

get_access_token() -> str

Return a valid access token, refreshing it if needed.

Source code in src/albert/core/auth/credentials.py
def get_access_token(self) -> str:
    """Return a valid access token, refreshing it if needed."""
    if self._requires_refresh():
        self._request_access_token()
    return self._token_info.access_token