API Reference: Token Data#

Complete reference for the TokenData class.

class bfabric.rest.token_data.TokenData(*, jobId: int, applicationId: int, entityClassName: str, entityId: int, user: str, userWsPassword: SecretStr, expiryDateTime: datetime, webServiceUser: Annotated[bool, WrapValidator(func=_parse_boolean_string, json_schema_input_type=PydanticUndefined)], caller: str, environment: str)#

Bases: BaseModel

Parsed token data from the B-Fabric token validation endpoint.

application_id: int#

ID of the B-Fabric application which created the token.

caller: str#

The B-Fabric instance where the token originates from.

entity_class: str#

Target entity class name

entity_id: int#

Target entity ID

environment: str#
job_id: int#

ID of the job associated with the token.

load_entity(client: Bfabric) Entity | None#

Loads the entity associated with this token.

model_config = {'populate_by_name': True, 'str_strip_whitespace': True, 'validate_by_alias': True, 'validate_by_name': True}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

model_dump(**kwargs: Any) dict[str, Any]#

Dump the token data to a dictionary, converting datetime fields to ISO format.

Parameters:

kwargs – Additional keyword arguments to pass to parent model_dump

Returns:

A dictionary representation of the token data with ISO-formatted datetime fields

token_expires: datetime#

Expiration datetime of the token.

user: str#

User/login

user_ws_password: SecretStr#

Webservice password of the user.

web_service_user: BooleanString#

Indicates whether the user has permission to use webservices API

Overview#

TokenData represents token information received from B-Fabric webapp authentication.

Properties#

Property

Type

Description

user

str

B-Fabric user login

user_ws_password

str

Web service password for the user

application_id

int

Application ID

entity_class

str

Entity class name

entity_id

int

Entity ID

job_id

int

External job ID

caller

str

B-Fabric instance URL

token_expires

datetime

Token expiration time

web_service_user

bool

Whether user has web service permissions

Quick Examples#

Basic Token Access#

from bfabric import Bfabric
from bfabric.experimental.webapp_integration_settings import TokenValidationSettings

settings = TokenValidationSettings(
    validation_bfabric_instance="https://fgcz-bfabric.uzh.ch/bfabric/",
    supported_bfabric_instances=["https://fgcz-bfabric.uzh.ch/bfabric/"],
)

client, token_data = Bfabric.connect_token(token=token, settings=settings)

print(f"Authenticated as: {token_data.user}")
print(f"Application ID: {token_data.application_id}")
print(f"Entity: {token_data.entity_class}#{token_data.entity_id}")

Check Token Expiration#

from datetime import datetime, timezone, timedelta

client, token_data = Bfabric.connect_token(token=token, settings=settings)

# Check if token expires soon
time_until_expiry = token_data.token_expires - datetime.now(timezone.utc)
if time_until_expiry < timedelta(hours=1):
    print("Warning: Token expires in less than 1 hour!")

Check Web Service Permissions#

client, token_data = Bfabric.connect_token(token=token, settings=settings)

if not token_data.web_service_user:
    raise PermissionError("User does not have web service permissions")

Load Associated Entity#

client, token_data = Bfabric.connect_token(token=token, settings=settings)

# Load entity from token data
entity = token_data.load_entity(client=client)

print(f"Loaded entity: {entity}")

See Also#