SAM Broker
The SAM broker of the SmarterAuthToken kind: Smarter api keys, applied from manifests.
- smarter.lib.drf.manifest.brokers.auth_token.LOGGED_FIELDS = ('key_id', 'name', 'description', 'user', 'is_active', 'created', 'expiry', 'last_used_at', 'tags')
The SmarterAuthToken fields that log messages may show. Not
digest, the primary key, nortoken_key, the first characters of the token itself.( 'key_id', 'name', 'description', 'user', 'is_active', 'created', 'expiry', 'last_used_at', 'tags', )
- class smarter.lib.drf.manifest.brokers.auth_token.SAMSmarterAuthTokenBroker(*args, **kwargs)[source]
Bases:
AbstractBroker[SmarterAuthToken,SmarterAuthToken]Smarter API SmarterAuthToken Manifest Broker.
This class is responsible for:
loading, validating and parsing the Smarter Api yaml SmarterAuthToken manifests
using the manifest to initialize the corresponding Pydantic model
This Broker class interacts with the collection of Django ORM models that represent the Smarter API SAMSmarterAuthToken manifests. The Broker class is responsible for creating, updating, deleting and querying the Django ORM models, as well as transforming the Django ORM models into Pydantic models for serialization and deserialization.
- property ORMMetaModelClass: Type[SmarterAuthToken]
Return the Django ORM meta model class for the broker.
- Returns:
The Django ORM meta model class definition for the broker.
- property ORMModelClass: Type[SmarterAuthToken]
The Django model of this broker’s resource,
SmarterAuthToken.
- property SerializerClass: Type[ModelSerializer]
Return the Serializer class for the SmarterAuthToken model.
This is used to serialize and deserialize the SmarterAuthToken model for API responses and requests.
- __init__(*args, **kwargs)[source]
Initialize the broker, with no token retrieved yet.
- Parameters:
args – Passed to
AbstractBroker.kwargs – Passed to
AbstractBroker.
- apply(request, *args, **kwargs)[source]
Apply the manifest: create or update the SmarterAuthToken, and save it.
This method copies the manifest data to the Django ORM model and saves the model to the database. Note that there are fields included in the manifest that are not editable and are therefore removed from the Django ORM model dict prior to attempting the save() command. These fields are defined in the readonly_fields list. After saving, it invalidates the token’s caches.
- Parameters:
request (
HttpRequest) – The request.args – Positional arguments, unused.
kwargs – Keyword arguments from the cli api url.
- Return type:
- Returns:
The journaled response, with the command result.
- Raises:
SAMSmarterAuthTokenBrokerError – If the requesting user doesn’t administer the account, or may not manage keys for the manifest’s user (see
check_api_key_user()).SAMBrokerErrorNotReady – If the broker has no manifest.
SAMBrokerError – If the token can’t be built or saved.
- cache_invalidations()[source]
Invalidate the cached SmarterAuthToken of the broker’s user and name, then the base class’s caches.
Called after the manifest or the SmarterAuthToken changes.
- Return type:
- check_api_key_user(api_key_user, command)[source]
Refuse an api key of a user whose key this broker’s user may not manage.
administers_accountlets this broker’s user manage api keys in their account. A superuser may manage anyone’s. Anyone else, e.g. a department’s account admin, only those of the account’s own users, and never of a user whose role is higher than theirs: a superuser’s, or a staff user’s unless they are staff.- Parameters:
api_key_user (
User) – The user of the api key, from the manifest’sspec.config.username.command (
SmarterJournalCliCommands) – The cli command, for the error.
- Raises:
SAMSmarterAuthTokenBrokerError – If the key’s user is outside the account, or holds a higher role.
- Return type:
- delete(request, *args, **kwargs)[source]
Delete the named SmarterAuthToken from the database; only staff users may.
- Parameters:
request (
HttpRequest) – The request.args – Positional arguments, unused.
kwargs – Keyword arguments from the cli api url.
- Return type:
- Returns:
The journaled response, with empty data.
- Raises:
SAMSmarterAuthTokenBrokerError – If the user is not staff, or the delete fails.
SAMBrokerErrorNotReady – If there is no such token.
- dependencies()[source]
Return brokers for the LLMClients that use this auth token.
- Return type:
- Returns:
A broker for each LLMClient that lists this auth token in its
spec.apiKey.
- deploy(request, *args, **kwargs)[source]
Activate the named SmarterAuthToken, saving it if it was inactive.
- Parameters:
request (
HttpRequest) – The request.args – Positional arguments, unused.
kwargs – Keyword arguments from the cli api url.
- Return type:
- Returns:
The journaled response, with the command result.
- Raises:
SAMBrokerErrorNotReady – If there is no such token.
- deployable: ClassVar[bool] = True
True when the broker implements
deploy().The cli api’s
resourcescommand reports it as each kind’sDeployable, and the smarter cli offersdeployandundeploycommands for those kinds. A broker that sets it must overridedeploy();undeploy()may still raiseSAMBrokerErrorNotImplemented, as CustomDomain’s and LLMHostCompute’s do.
- describe(request, *args, **kwargs)[source]
Return the named SmarterAuthToken as a manifest.
- Parameters:
request (
HttpRequest) – The request.args – Positional arguments, unused.
kwargs – Keyword arguments from the cli api url.
- Return type:
- Returns:
The journaled response, with the manifest as its data.
- Raises:
SAMSmarterAuthTokenBrokerError – If the manifest can’t be built.
SAMBrokerErrorNotFound – If there is no such token.
- django_orm_to_manifest_dict()[source]
Transform the SmarterAuthToken into a SAMSmarterAuthToken manifest dict, with camelCase keys.
- Return type:
- Returns:
The manifest’s model dump.
- Raises:
SAMSmarterAuthTokenBrokerError – If the broker has no SmarterAuthToken.
SAMBrokerErrorNotFound – If the broker has no manifest.
- duplicate_names_message(username)[source]
Return the error message for a user who has more than one API key with the broker’s name.
- example_manifest(request, *args, **kwargs)[source]
Return an example SmarterAuthToken manifest.
- Parameters:
request (
HttpRequest) – The request.args – Positional arguments, unused.
kwargs – Keyword arguments from the cli api url.
- Return type:
- Returns:
The journaled response, with the example manifest as its data.
- property formatted_class_name: str
Return the broker’s dotted class name and instance id, formatted for log messages.
- Returns:
The formatted name, e.g.
smarter.lib.drf.manifest.brokers.auth_token.SAMSmarterAuthTokenBroker()[4401].
- get(request, *args, **kwargs)[source]
List the user’s SmarterAuthTokens, or the one named in
kwargs, filtered by annotation selector.- Parameters:
request (
HttpRequest) – The request.args – Positional arguments, unused.
kwargs – Keyword arguments from the cli api url.
- Return type:
- Returns:
The journaled response, with the column titles and one item per token.
- Raises:
SAMSmarterAuthTokenBrokerError – If a token can’t be serialized.
- logs(request, *args, **kwargs)[source]
Not implemented: a SmarterAuthToken has no logs.
- Parameters:
request (
HttpRequest) – The request.args – Positional arguments, unused.
kwargs – Keyword arguments from the cli api url.
- Raises:
SAMBrokerErrorNotImplemented – Always.
- Return type:
- property manifest: SAMSmarterAuthToken | None
The SAMSmarterAuthToken manifest, a Pydantic model, built from the manifest data or the SmarterAuthToken.
The Pydantic model is initialized with the data from the client’s manifest data, which is generally passed to the model constructor as
**data. However, this top-level manifest model has to be explicitly initialized, whereas its child models are automatically cascade-initialized by the Pydantic model, implicitly passing**datato each child’s constructor.
- manifest_to_django_orm()[source]
Transform the SAMSmarterAuthToken manifest into a dict of SmarterAuthToken model fields.
- Return type:
- Returns:
The manifest’s metadata and
spec.config, with snake_case keys.- Raises:
SAMSmarterAuthTokenBrokerError – If the manifest is not a SAMSmarterAuthToken, or its config does not dump to a dict.
- property orm_instance: SmarterAuthToken | None
Return the Django ORM model instance for the broker.
- Returns:
The SmarterAuthToken, or None if there is no manifest or no such token.
- Raises:
SAMSmarterAuthTokenBrokerError – If more than one of the owner’s API keys has the name.
- orm_meta_instance_setter()[source]
Set the ORM meta instance to the SmarterAuthToken of the token’s owner and the broker’s name.
It reuses the ORM instance if that is already set, and leaves the meta instance unset if there is no owner’s username or no name, or if the token does not exist.
- Return type:
- prompt(request, *args, **kwargs)[source]
Not implemented: a SmarterAuthToken can’t be prompted.
- Parameters:
request (
HttpRequest) – The request.args – Positional arguments, unused.
kwargs – Keyword arguments from the cli api url.
- Raises:
SAMBrokerErrorNotImplemented – Always.
- Return type:
- property smarter_auth_token: SmarterAuthToken | None
The SmarterAuthToken of this broker, a Django model derived from knox’s
AuthToken.The SmarterAuthToken object is used to store the authentication hash and Smarter metadata for the Smarter API. The SmarterAuthToken object is retrieved from the database, if it exists, or created from the manifest if it does not.
- property token_key: str | None
The token_key is the actual API key that is used to authenticate with the Smarter API.
The token_key is generated by the SmarterAuthToken object when it is created and it is only available immediately after the object is created.
- property token_username: str | None
The username of the SmarterAuthToken’s owner.
This is the manifest’s spec.config.username when there is a manifest. Otherwise, for the cli commands that only pass a name (describe, delete, deploy, undeploy), it is the user who made the request.
- undeploy(request, *args, **kwargs)[source]
Deactivate the named SmarterAuthToken, saving it if it was active.
- Parameters:
request (
HttpRequest) – The request.args – Positional arguments, unused.
kwargs – Keyword arguments from the cli api url.
- Return type:
- Returns:
The journaled response, with the command result.
- Raises:
SAMBrokerErrorNotReady – If there is no such token.
- exception smarter.lib.drf.manifest.brokers.auth_token.SAMSmarterAuthTokenBrokerError(message=None, thing=None, command=None, stack_trace=None)[source]
Bases:
SAMBrokerErrorBase exception for Smarter API SmarterAuthToken Broker handling.
- property get_formatted_err_message
The error message prefix of this broker’s errors.
- class smarter.lib.drf.manifest.brokers.auth_token.SmarterAuthTokenMiniSerializer(*args, **kwargs)[source]
Bases:
ModelSerializerAPI key serializer for smarter api that excludes SAM ownership information.
- smarter.lib.drf.manifest.brokers.auth_token.loggable_fields(smarter_auth_token)[source]
Return the fields of a SmarterAuthToken that a log message may show.
- Parameters:
smarter_auth_token (
SmarterAuthToken|None) – The SmarterAuthToken.- Return type:
- Returns:
Its
LOGGED_FIELDS, as strings, or None if there is no token.