This commit is contained in:
Ashwin Bharambe 2025-05-20 13:51:28 -07:00
parent c4d32600f2
commit b43cdaaed5
3 changed files with 167 additions and 213 deletions

View file

@ -10,27 +10,26 @@ from typing import Protocol, runtime_checkable
from pydantic import BaseModel, Field
from llama_stack.schema_utils import json_schema_type, webmethod
from llama_stack.schema_utils import webmethod
class CredentialTokenType(str, Enum):
"""The type of credential token.
:cvar oauth2: OAuth2 token
:cvar api_key: API key
:cvar oauth2_authorization_code: OAuth2 authorization code. Used to exchange for an access token (and optionally, refresh token).
This should be provided once the client receives the OAuth2 callback.
:cvar access_token: An opaque OAuth2 / JWT access token. Often what is vended as a "API key".
"""
oauth2 = "oauth2"
api_key = "api_key"
oauth2_authorization_code = "oauth2_authorization_code"
access_token = "access_token"
@json_schema_type
class ProviderCredential(BaseModel):
class CredentialListItem(BaseModel):
credential_id: str
provider_id: str
token_type: CredentialTokenType
token: str
expires_at: datetime = Field(description="The time at which the credential expires. In UTC.")
@ -39,13 +38,13 @@ class Credentials(Protocol):
"""
Create, update and delete ephemeral provider-specific credentials.
Each provider may need optional authentication. This might be a persistent API key, or
a short-lived OAuth2 token. There is a single credential for each provider instance.
Each provider may need optional authentication. This might be a persistent API key, a short-lived OAuth2
access token or a refreshable OAuth2 token. There is a single credential for each provider instance.
Credentials are ephemeral -- they may be purged after the specified TTL.
Credentials are associated with the same ABAC access attributes and permissions as other
resources in the system.
Credentials are associated with the logged in user. If no user is logged in, the credentials
are associated with the anonymous user.
It is recommended to store these credentials using Envelope Encryption. The storage could
be a regular KVStore, but you should use a secure Key Management Service for encrypting
@ -54,26 +53,24 @@ class Credentials(Protocol):
@webmethod(route="/credentials", method="POST")
async def create_credential(
self, provider_id: str, token_type: CredentialTokenType, token: str, ttl_seconds: int = 3600
) -> ProviderCredential:
self,
provider_id: str,
token_type: CredentialTokenType,
token: str,
nonce: str | None = None,
ttl_seconds: int = 3600,
) -> str:
"""Create a new set of credentials for a given provider.
:param provider_id: The ID of the provider to create credentials for.
:param token_type: The type of token to create. This is provided in the API to serve as lightweight
documentation / metadata for the token.
:param token: The token itself.
:param nonce: The nonce is required when the token type is oauth2_authorization_code.
:param ttl_seconds: The time to live for the credential in seconds. Defaults to 3600 seconds.
:returns: created ProviderCredential object
"""
...
@webmethod(route="/credentials/{credential_id}", method="PUT")
async def update_credential(self, credential_id: str, token: str) -> ProviderCredential:
"""Update an existing set of credentials for a given provider.
:param credential_id: The ID of the credential to update.
:param token: The new token to set for the credential.
:returns: updated ProviderCredential object
When token_type is oauth2_authorization_code, the TTL is ignored and is obtained
from the provider when exchanging the code for an access token.
:returns: The ID of the created credential.
"""
...
@ -84,3 +81,11 @@ class Credentials(Protocol):
:param credential_id: The ID of the credential to delete.
"""
...
@webmethod(route="/credentials", method="GET")
async def get_credentials(self) -> list[CredentialListItem]:
"""Get all credentials for the current user.
:returns: A list of all credentials for the current user.
"""
...