Documentation
¶
Overview ¶
Package anyllmplatform provides a client for the ANY LLM platform API.
Security Notes ¶
This package implements X25519 sealed box encryption for secure key exchange. The cryptographic primitives used are:
- X25519 (Curve25519) for Elliptic Curve Diffie-Hellman key agreement
- XChaCha20-Poly1305 for authenticated encryption (AEAD)
- SHA-512 for deterministic nonce derivation
The sealed box format follows the NaCl/libsodium convention and provides:
- Forward secrecy through ephemeral keypairs
- Authenticated encryption preventing tampering
- Deterministic nonces preventing nonce reuse attacks
Private keys should be handled with care and never logged or exposed.
Index ¶
- Constants
- Variables
- func DecryptData(encryptedDataBase64 string, privateKey []byte) (string, error)
- func ExtractPublicKey(privateKey []byte) (string, error)
- func LoadPrivateKey(base64PrivateKey string) ([]byte, error)
- type ChallengeCreationError
- type Client
- func (c *Client) CreateChallenge(ctx context.Context, publicKey string) (*challengeResponse, error)
- func (c *Client) DecryptProviderKeyValue(encryptedKey string, privateKey []byte) (string, error)
- func (c *Client) FetchProviderKey(ctx context.Context, provider, accessToken string) (*providerKeyResponse, error)
- func (c *Client) GetAccessToken(ctx context.Context, anyLLMKey string) (string, error)
- func (c *Client) GetDecryptedProviderKey(ctx context.Context, anyLLMKey, provider string) (*DecryptedProviderKey, error)
- func (c *Client) GetPublicKey(anyLLMKey string) (string, error)
- func (c *Client) GetSolvedChallenge(ctx context.Context, anyLLMKey string) (uuid.UUID, error)
- func (c *Client) RefreshAccessToken(ctx context.Context, anyLLMKey string) (string, error)
- func (c *Client) RequestAccessToken(ctx context.Context, solvedChallenge uuid.UUID) (string, error)
- func (c *Client) SolveChallenge(encryptedChallenge string, privateKey []byte) (uuid.UUID, error)
- type DecryptedProviderKey
- type DecryptionError
- type InvalidKeyError
- type KeyComponents
- type Option
- type ProviderKeyFetchError
Constants ¶
const ( // DefaultPlatformURL is the default URL for the ANY LLM platform API. DefaultPlatformURL = "http://localhost:8000/api/v1" // TokenValidityDuration is the validity duration for access tokens (23 hours for safety margin). TokenValidityDuration = 23 * time.Hour )
const ( // X25519KeySize is the size of an X25519 key in bytes (256 bits). // This provides approximately 128 bits of security. X25519KeySize = 32 // XChaCha20NonceSize is the size of an XChaCha20 nonce in bytes (192 bits). // The extended nonce size eliminates practical nonce collision concerns. XChaCha20NonceSize = 24 )
Variables ¶
var ( // ErrChallengeCreation indicates authentication challenge creation failed. ErrChallengeCreation = errors.New("challenge creation failed") // ErrProviderKeyFetch indicates fetching a provider API key failed. ErrProviderKeyFetch = errors.New("provider key fetch failed") // ErrInvalidKey indicates the ANY_LLM_KEY format is invalid. ErrInvalidKey = errors.New("invalid ANY_LLM_KEY format") // ErrDecryption indicates a decryption operation failed. ErrDecryption = errors.New("decryption failed") // ErrAuthentication indicates an authentication failure. ErrAuthentication = errors.New("authentication failed") )
Sentinel errors for common error conditions.
Functions ¶
func DecryptData ¶
DecryptData decrypts data using X25519 sealed box format.
The sealed box format is:
- First 32 bytes: ephemeral public key
- Remaining bytes: XChaCha20-Poly1305 ciphertext with 16-byte auth tag
The shared secret is computed using X25519 ECDH, and the nonce is derived deterministically from SHA512(ephemeral_public_key || recipient_public_key)[:24].
Security properties:
- Forward secrecy: Compromising the recipient's private key does not compromise past messages encrypted with ephemeral keypairs.
- Authenticated encryption: The Poly1305 MAC ensures message integrity and prevents tampering.
- Nonce uniqueness: Deterministic derivation from public keys guarantees unique nonces for each sender-recipient pair.
func ExtractPublicKey ¶
ExtractPublicKey derives the public key from an X25519 private key.
Returns the base64-encoded public key.
func LoadPrivateKey ¶
LoadPrivateKey loads an X25519 private key from a base64-encoded string.
Returns the 32-byte private key or an error if decoding fails or the key is invalid.
Types ¶
type ChallengeCreationError ¶
ChallengeCreationError represents an error during authentication challenge creation.
func (*ChallengeCreationError) Error ¶
func (e *ChallengeCreationError) Error() string
func (*ChallengeCreationError) Is ¶
func (e *ChallengeCreationError) Is(target error) bool
Is implements errors.Is for ChallengeCreationError.
func (*ChallengeCreationError) Unwrap ¶
func (e *ChallengeCreationError) Unwrap() error
type Client ¶
type Client struct {
// PlatformURL is the base URL for the ANY LLM platform API.
PlatformURL string
// HTTPClient is the HTTP client to use for requests.
HTTPClient *http.Client
// contains filtered or unexported fields
}
Client is the HTTP client for communicating with the ANY LLM backend.
func (*Client) CreateChallenge ¶
CreateChallenge creates an authentication challenge using the provided public key.
func (*Client) DecryptProviderKeyValue ¶
DecryptProviderKeyValue decrypts the provider API key.
func (*Client) FetchProviderKey ¶
func (c *Client) FetchProviderKey(ctx context.Context, provider, accessToken string) (*providerKeyResponse, error)
FetchProviderKey fetches the encrypted provider API key from the server.
func (*Client) GetAccessToken ¶
GetAccessToken returns a valid access token, refreshing if necessary. This is useful for making authenticated requests to the platform API.
func (*Client) GetDecryptedProviderKey ¶
func (c *Client) GetDecryptedProviderKey(ctx context.Context, anyLLMKey, provider string) (*DecryptedProviderKey, error)
GetDecryptedProviderKey gets a decrypted provider API key using the complete authentication flow.
func (*Client) GetPublicKey ¶
GetPublicKey extracts the public key from an ANY_LLM_KEY.
func (*Client) GetSolvedChallenge ¶
GetSolvedChallenge gets a solved authentication challenge from an ANY_LLM_KEY.
func (*Client) RefreshAccessToken ¶
RefreshAccessToken refreshes the access token using the ANY_LLM_KEY.
func (*Client) RequestAccessToken ¶
RequestAccessToken requests an access token by submitting the solved challenge.
type DecryptedProviderKey ¶
type DecryptedProviderKey struct {
// APIKey is the decrypted API key for the provider.
APIKey string
// ProviderKeyID is the unique identifier for the provider key.
ProviderKeyID uuid.UUID
// ProjectID is the unique identifier for the project.
ProjectID uuid.UUID
// Provider is the provider name (e.g., "openai", "anthropic").
Provider string
// CreatedAt is when the provider key was created.
CreatedAt time.Time
// UpdatedAt is when the provider key was last updated (may be zero).
UpdatedAt time.Time
}
DecryptedProviderKey contains the decrypted provider key and metadata.
type DecryptionError ¶
type DecryptionError struct {
Message string
}
DecryptionError represents a decryption failure.
func (*DecryptionError) Error ¶
func (e *DecryptionError) Error() string
func (*DecryptionError) Is ¶
func (e *DecryptionError) Is(target error) bool
Is implements errors.Is for DecryptionError.
func (*DecryptionError) Unwrap ¶
func (e *DecryptionError) Unwrap() error
type InvalidKeyError ¶
type InvalidKeyError struct {
Message string
}
InvalidKeyError represents an error when parsing an ANY_LLM_KEY.
func (*InvalidKeyError) Error ¶
func (e *InvalidKeyError) Error() string
func (*InvalidKeyError) Is ¶
func (e *InvalidKeyError) Is(target error) bool
Is implements errors.Is for InvalidKeyError.
func (*InvalidKeyError) Unwrap ¶
func (e *InvalidKeyError) Unwrap() error
type KeyComponents ¶
type KeyComponents struct {
// KeyID is the unique key identifier.
KeyID string
// PublicKeyFingerprint is the fingerprint of the public key.
PublicKeyFingerprint string
// Base64EncodedPrivateKey is the base64-encoded X25519 private key.
Base64EncodedPrivateKey string
}
KeyComponents represents the parsed components of an ANY_LLM_KEY.
func ParseAnyLLMKey ¶
func ParseAnyLLMKey(anyLLMKey string) (*KeyComponents, error)
ParseAnyLLMKey parses an ANY_LLM_KEY string into its components.
The expected format is: ANY.v1.<key_id>.<fingerprint>-<base64_key>
Returns an error if the key format is invalid.
type Option ¶
type Option func(*Client)
Option is a functional option for configuring the Client.
func WithHTTPClient ¶
WithHTTPClient sets a custom HTTP client.
func WithPlatformURL ¶
WithPlatformURL sets a custom platform URL.
type ProviderKeyFetchError ¶
ProviderKeyFetchError represents an error when fetching a provider API key.
func (*ProviderKeyFetchError) Error ¶
func (e *ProviderKeyFetchError) Error() string
func (*ProviderKeyFetchError) Is ¶
func (e *ProviderKeyFetchError) Is(target error) bool
Is implements errors.Is for ProviderKeyFetchError.
func (*ProviderKeyFetchError) Unwrap ¶
func (e *ProviderKeyFetchError) Unwrap() error
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
any-llm
command
|
|
|
examples
|
|
|
basic
command
Package main demonstrates basic usage of the any-llm-platform-client-go library.
|
Package main demonstrates basic usage of the any-llm-platform-client-go library. |
|
internal
|
|
|
testutil
Package testutil provides test utilities and fixtures for the any-llm-platform-client-go package.
|
Package testutil provides test utilities and fixtures for the any-llm-platform-client-go package. |