credentials

package
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 24, 2026 License: MIT Imports: 16 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCredentialsNotFound is returned when no credentials exist for an agent
	ErrCredentialsNotFound = errors.New("credentials not found")

	// ErrCredentialsInvalid is returned when credentials are invalid
	ErrCredentialsInvalid = errors.New("credentials invalid")

	// ErrProviderUnavailable is returned when a provider cannot be used
	ErrProviderUnavailable = errors.New("provider unavailable")
)

Common errors

Functions

func DeriveKeyFromSystem

func DeriveKeyFromSystem() ([]byte, error)

DeriveKeyFromSystem is a placeholder for system keyring integration

Phase 4 will implement actual keyring access using github.com/zalando/go-keyring or similar library.

func GenerateKeyFromPassphrase

func GenerateKeyFromPassphrase(passphrase []byte) ([]byte, error)

GenerateKeyFromPassphrase creates an encryption key from a passphrase using PBKDF2 with a high iteration count

func GenerateRandomKey

func GenerateRandomKey() ([]byte, error)

GenerateRandomKey generates a new random encryption key

func IsErrCredentialsNotFound

func IsErrCredentialsNotFound(err error) bool

IsErrCredentialsNotFound checks if err is or wraps ErrCredentialsNotFound

func KeyFileExists

func KeyFileExists() bool

KeyFileExists checks if the encryption key file exists

func LoadKey

func LoadKey(keyFile string) ([]byte, error)

LoadKey loads an encryption key from a file

func MigrateCredentials added in v0.2.0

func MigrateCredentials(oldDir, newDir string) (int, error)

MigrateCredentials migrates per-agent credential files from an old directory structure to a new one.

Old layout: oldDir/AGENT_ID/credentials.json New layout: newDir/AGENT_ID/credentials.json

Existing credentials at the new location are not overwritten. Returns the number of credentials migrated.

func SaveKey

func SaveKey(key []byte, keyFile string) error

SaveKey saves an encryption key to a file with secure permissions

func ValidateKey

func ValidateKey(key []byte) error

ValidateKey checks if a key is valid for AES-256 (must be 32 bytes)

Types

type Credentials

type Credentials struct {
	Username string
	Password string // Permanent password from registration (64-char hex string)
	Empire   string // Faction/empire for registration (e.g., "voidborn")
}

Credentials represents authentication credentials for an agent

type Encryptor

type Encryptor struct {
	// contains filtered or unexported fields
}

Encryptor handles AES-256-GCM encryption for credential storage

func LoadOrGenerateKey

func LoadOrGenerateKey() (*Encryptor, error)

LoadOrGenerateKey attempts to load an existing encryption key from multiple sources

Key sources (tried in order): 1. System keyring (future implementation) 2. Key file (~/.spacemolt/key) 3. Environment variable (SPACEMOLT_ENCRYPTION_KEY) 4. Generate new random key (saved to keyring or file)

func NewEncryptor

func NewEncryptor(key []byte) (*Encryptor, error)

NewEncryptor creates an encryptor with a 32-byte key

func NewEncryptorFromPassphrase

func NewEncryptorFromPassphrase(passphrase []byte) (*Encryptor, error)

NewEncryptorFromPassphrase derives an encryption key from a passphrase

Uses PBKDF2 with 100,000 iterations to derive a 32-byte key from the passphrase.

func (*Encryptor) Decrypt

func (e *Encryptor) Decrypt(ciphertext string) (string, error)

Decrypt decrypts ciphertext using AES-256-GCM

Expects base64-encoded ciphertext with nonce prepended: nonce:ciphertext

func (*Encryptor) Encrypt

func (e *Encryptor) Encrypt(plaintext string) (string, error)

Encrypt encrypts plaintext using AES-256-GCM

Returns base64-encoded ciphertext with nonce prepended: nonce:ciphertext

type EnvProvider

type EnvProvider struct {
	// contains filtered or unexported fields
}

EnvProvider loads credentials from environment variables

Environment variable naming convention:

SPACEMOLT_AGENT_<AGENT_ID>_USERNAME
SPACEMOLT_AGENT_<AGENT_ID>_TOKEN
SPACEMOLT_AGENT_<AGENT_ID>_EMPIRE

Example for agent "explorer-7":

SPACEMOLT_AGENT_EXPLORER_7_USERNAME="explorer-7"
SPACEMOLT_AGENT_EXPLORER_7_TOKEN="abc123"
SPACEMOLT_AGENT_EXPLORER_7_EMPIRE="voidborn"

Note: Agent ID is uppercased and hyphens are replaced with underscores to create valid environment variable names.

func NewEnvProvider

func NewEnvProvider(prefix string) *EnvProvider

NewEnvProvider creates a provider that loads credentials from environment variables

func (*EnvProvider) GetCredentials

func (p *EnvProvider) GetCredentials(ctx context.Context, agentID string) (*Credentials, error)

GetCredentials retrieves credentials from environment variables for an agent

func (*EnvProvider) ListAgents

func (p *EnvProvider) ListAgents(ctx context.Context) ([]string, error)

ListAgents returns all agent IDs that have credentials in environment variables

func (*EnvProvider) RemoveCredentials

func (p *EnvProvider) RemoveCredentials(ctx context.Context, agentID string) error

RemoveCredentials is a no-op for environment provider

func (*EnvProvider) StoreCredentials

func (p *EnvProvider) StoreCredentials(ctx context.Context, agentID string, creds *Credentials) error

StoreCredentials is a no-op for environment provider

Environment variables cannot be set from within a running process for security reasons. Returns an error explaining this limitation.

type FallbackProvider

type FallbackProvider struct {
	// contains filtered or unexported fields
}

FallbackProvider tries multiple providers in order until one succeeds

Providers are tried in the order they were added. The first provider that successfully returns credentials is used. This allows for flexible credential sourcing with automatic fallback.

Common pattern:

  1. Try agent-specific file (most specific)
  2. Try SQLite database (centralized storage)
  3. Try environment variables (container-friendly)
  4. Try legacy single-credential file (backward compatibility)

func NewFallbackProvider

func NewFallbackProvider(providers ...Provider) *FallbackProvider

NewFallbackProvider creates a provider that tries multiple sources

func (*FallbackProvider) AddProvider

func (p *FallbackProvider) AddProvider(provider Provider)

AddProvider appends a provider to the fallback chain

Providers are tried in the order they were added.

func (*FallbackProvider) GetCredentials

func (p *FallbackProvider) GetCredentials(ctx context.Context, agentID string) (*Credentials, error)

GetCredentials tries each provider in order until credentials are found

Returns the first successful result from any provider. Returns ErrCredentialsNotFound only if all providers fail.

func (*FallbackProvider) ListAgents

func (p *FallbackProvider) ListAgents(ctx context.Context) ([]string, error)

ListAgents aggregates agent lists from all providers

Returns a deduplicated list of all agent IDs across all providers.

func (*FallbackProvider) RemoveCredentials

func (p *FallbackProvider) RemoveCredentials(ctx context.Context, agentID string) error

RemoveCredentials removes credentials from all providers

Attempts to remove from all providers, even if some fail. Returns the first error encountered, but continues trying.

func (*FallbackProvider) StoreCredentials

func (p *FallbackProvider) StoreCredentials(ctx context.Context, agentID string, creds *Credentials) error

StoreCredentials stores credentials in the first available provider

Tries providers in order until one successfully stores the credentials. Returns an error if all providers fail.

type FileCredentials

type FileCredentials struct {
	Username string `json:"username"`
	Password string `json:"password"`        // New field (v0.38.0+)
	Token    string `json:"token,omitempty"` // Legacy field for backward compatibility
	Empire   string `json:"empire"`
}

FileCredentials represents the JSON format of credential files Supports both 'password' (new) and 'token' (legacy) for migration

type FileProvider

type FileProvider struct {
	// contains filtered or unexported fields
}

FileProvider loads credentials from agent-specific files

Each agent has its own credentials file in the agent's consolidated directory:

data/agents/<agent-id>/credentials.json

This directory also contains:

  • personality.json (agent personality and traits)
  • DIARY.md (future: agent's learned knowledge)

File format:

{
  "username": "agent-name",
  "password": "auth-token",
  "empire": "voidborn"
}

Legacy format with "token" instead of "password" is automatically migrated.

func NewFileProvider

func NewFileProvider(agentsDir string) *FileProvider

NewFileProvider creates a new file-based credential provider

agentsDir is the base directory containing agent subdirectories. Each subdirectory should be named with the agent ID and contain:

  • personality.json (required)
  • credentials.json (auto-generated)

Example: agentsDir = "data/agents" will look for credentials at:

data/agents/<agent-id>/credentials.json

func (*FileProvider) GetCredentials

func (p *FileProvider) GetCredentials(ctx context.Context, agentID string) (*Credentials, error)

GetCredentials retrieves credentials for an agent from its credential file

func (*FileProvider) ListAgents

func (p *FileProvider) ListAgents(ctx context.Context) ([]string, error)

ListAgents returns all agent IDs that have credential files

func (*FileProvider) RemoveCredentials

func (p *FileProvider) RemoveCredentials(ctx context.Context, agentID string) error

RemoveCredentials deletes an agent's credential file

func (*FileProvider) SetPermissions

func (p *FileProvider) SetPermissions(agentID string) error

SetPermissions sets secure permissions on credential files

func (*FileProvider) StoreCredentials

func (p *FileProvider) StoreCredentials(ctx context.Context, agentID string, creds *Credentials) error

StoreCredentials saves credentials for an agent to its credential file

func (*FileProvider) ValidatePermissions

func (p *FileProvider) ValidatePermissions(agentID string) error

ValidatePermissions checks that credential files have secure permissions (0600)

type KeyringProvider

type KeyringProvider struct {
	// contains filtered or unexported fields
}

KeyringProvider loads credentials from the system keyring

This provider uses the OS's secure credential storage: - Linux: gnome-keyring, kwallet - macOS: Keychain - Windows: Credential Manager

NOTE: Phase 4 implementation (enhanced providers). Phase 1 provides a stub that returns ErrProviderUnavailable.

func NewKeyringProvider

func NewKeyringProvider(service string) *KeyringProvider

NewKeyringProvider creates a new keyring credential provider

service is the keyring service name to use for storing credentials.

func (*KeyringProvider) GetCredentials

func (p *KeyringProvider) GetCredentials(ctx context.Context, agentID string) (*Credentials, error)

GetCredentials retrieves credentials from the system keyring

Phase 4: Implement actual keyring access Phase 1: Returns ErrProviderUnavailable

func (*KeyringProvider) IsAvailable

func (p *KeyringProvider) IsAvailable() bool

IsAvailable checks if the keyring provider is available

Phase 4: Will detect if keyring is accessible

func (*KeyringProvider) ListAgents

func (p *KeyringProvider) ListAgents(ctx context.Context) ([]string, error)

ListAgents returns all agent IDs with stored credentials

func (*KeyringProvider) RemoveCredentials

func (p *KeyringProvider) RemoveCredentials(ctx context.Context, agentID string) error

RemoveCredentials deletes credentials from the system keyring

func (*KeyringProvider) StoreCredentials

func (p *KeyringProvider) StoreCredentials(ctx context.Context, agentID string, creds *Credentials) error

StoreCredentials saves credentials to the system keyring

type Provider

type Provider interface {
	// GetCredentials retrieves credentials for an agent
	//
	// Returns ErrCredentialsNotFound if no credentials exist for the agent.
	// Returns ErrProviderUnavailable if the provider cannot be used.
	GetCredentials(ctx context.Context, agentID string) (*Credentials, error)

	// StoreCredentials saves credentials for an agent
	//
	// If credentials already exist, they should be overwritten.
	StoreCredentials(ctx context.Context, agentID string, creds *Credentials) error

	// RemoveCredentials deletes credentials for an agent
	//
	// Should return ErrCredentialsNotFound if credentials don't exist.
	RemoveCredentials(ctx context.Context, agentID string) error

	// ListAgents returns all agent IDs that have stored credentials
	//
	// Returns an empty slice if no credentials are stored.
	ListAgents(ctx context.Context) ([]string, error)
}

Provider provides credentials for agents

The Provider interface allows flexibility in where and how credentials are stored, supporting multiple backends (file, SQLite, keyring, etc.)

type SQLiteProvider

type SQLiteProvider struct {
	// contains filtered or unexported fields
}

SQLiteProvider loads credentials from a SQLite database

This provider stores credentials in the agent_credentials table, with encrypted tokens for security using AES-256-GCM.

func NewSQLiteProvider

func NewSQLiteProvider(dbPath string, encryptor *Encryptor) (*SQLiteProvider, error)

NewSQLiteProvider creates a new SQLite credential provider

The database must have the agent_credentials table created. See knowledge/sqlite_migrations.go for schema. The encryptor is used to encrypt/decrypt tokens in the database.

func NewSQLiteProviderWithDB

func NewSQLiteProviderWithDB(db *sql.DB, encryptor *Encryptor) *SQLiteProvider

NewSQLiteProviderWithDB creates a provider using an existing DB connection

Useful when sharing a database connection with the knowledge base. The encryptor is used to encrypt/decrypt tokens in the database.

func (*SQLiteProvider) Close

func (p *SQLiteProvider) Close() error

Close closes the database connection

Only call this if the provider was created with NewSQLiteProvider. Do not call if created with NewSQLiteProviderWithDB (connection is shared).

func (*SQLiteProvider) GetCredentials

func (p *SQLiteProvider) GetCredentials(ctx context.Context, agentID string) (*Credentials, error)

GetCredentials retrieves credentials from the database

func (*SQLiteProvider) ListAgents

func (p *SQLiteProvider) ListAgents(ctx context.Context) ([]string, error)

ListAgents returns all agent IDs with stored credentials

func (*SQLiteProvider) RemoveCredentials

func (p *SQLiteProvider) RemoveCredentials(ctx context.Context, agentID string) error

RemoveCredentials deletes credentials from the database

func (*SQLiteProvider) StoreCredentials

func (p *SQLiteProvider) StoreCredentials(ctx context.Context, agentID string, creds *Credentials) error

StoreCredentials saves credentials to the database

type StaticProvider

type StaticProvider struct {
	// contains filtered or unexported fields
}

StaticProvider returns a fixed set of credentials for all agents

This is useful for testing or single-credential scenarios.

func NewStaticProvider

func NewStaticProvider(username, password, empire string) *StaticProvider

NewStaticProvider creates a provider that always returns the same credentials

func (*StaticProvider) GetCredentials

func (p *StaticProvider) GetCredentials(ctx context.Context, agentID string) (*Credentials, error)

GetCredentials returns the static credentials for any agent ID

func (*StaticProvider) ListAgents

func (p *StaticProvider) ListAgents(ctx context.Context) ([]string, error)

ListAgents returns nil since static provider doesn't track specific agents

func (*StaticProvider) RemoveCredentials

func (p *StaticProvider) RemoveCredentials(ctx context.Context, agentID string) error

RemoveCredentials is a no-op for static provider

func (*StaticProvider) StoreCredentials

func (p *StaticProvider) StoreCredentials(ctx context.Context, agentID string, creds *Credentials) error

StoreCredentials is a no-op for static provider (credentials cannot be changed)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL