Documentation
¶
Overview ¶
Package omnitoken provides a token management SDK that bridges goauth credentials with vault-based storage via omnivault.
Overview ¶
omnitoken enables applications (particularly MCP servers via omniskill) to:
- Store and retrieve goauth Credentials in various vault backends
- Automatically manage OAuth2 token lifecycle (acquisition, refresh, caching)
- Implement goauth's TokenSet interface for vault-backed token storage
- Support multiple credential types (OAuth2, JWT, Basic Auth, API keys)
Architecture ¶
omnitoken sits between three libraries:
- goauth: Provides the Credentials struct and token acquisition logic
- omnivault: Provides vault abstractions for 30+ backends (1Password, HashiCorp Vault, etc.)
- omniskill: MCP server framework that uses omnitoken for credential management
Basic Usage ¶
import (
"github.com/plexusone/omnitoken"
"github.com/plexusone/omnivault/providers/memory"
)
// Create vault backend (use real vault in production)
vault := memory.New()
// Create token manager
mgr, err := omnitoken.New(omnitoken.Config{
Vault: vault,
})
if err != nil {
log.Fatal(err)
}
defer mgr.Close()
// Store credentials
creds := &goauth.Credentials{
Service: "github",
Type: goauth.TypeOAuth2,
OAuth2: &goauth.CredentialsOAuth2{
ClientID: "...",
ClientSecret: "...",
GrantType: "client_credentials",
},
}
mgr.SetCredentials(ctx, "my-github-app", creds)
// Get authenticated HTTP client
client, err := mgr.GetClient(ctx, "my-github-app")
Vault Integration ¶
omnitoken uses omnivault for storage, supporting backends like:
- 1Password (op://)
- HashiCorp Vault (vault://)
- OpenBao (openbao://)
- Bitwarden (bw://)
- AWS Secrets Manager (aws-sm://)
- Azure Key Vault (azure-kv://)
- GCP Secret Manager (gcp-sm://)
- macOS Keychain (keychain://)
- Environment variables (env://)
- Files (file://)
Token Lifecycle ¶
The TokenManager handles the complete token lifecycle:
- Retrieves credentials from vault
- Checks for cached/stored valid token
- Refreshes expired tokens using refresh_token if available
- Obtains new tokens when refresh isn't possible
- Stores tokens in vault for persistence across restarts
goauth Integration ¶
omnitoken implements goauth's TokenSet interface via VaultTokenSet, enabling integration with goauth's multi-service token management:
tokenSet := mgr.TokenSet() // Use with goauth's NewClientWithTokenSet, OAuth2Manager, etc.
Package omnitoken provides a token management SDK that bridges goauth credentials with vault-based storage via omnivault. It enables MCP servers and other applications to manage OAuth2 tokens and service credentials generically across multiple vault backends.
Key features:
- Store and retrieve goauth Credentials in vaults (1Password, HashiCorp Vault, etc.)
- Implement goauth's TokenSet interface for vault-backed token storage
- Automatic token refresh with configurable buffer time
- Support for multiple credential types (OAuth2, JWT, Basic Auth, etc.)
Basic usage:
import (
"github.com/plexusone/omnitoken"
"github.com/plexusone/omnivault/providers/memory"
)
// Create vault backend
vault := memory.New()
// Create token manager
mgr, err := omnitoken.New(omnitoken.Config{
Vault: vault,
})
// Get an authenticated HTTP client
client, err := mgr.GetClient(ctx, "my-app")
Index ¶
- Variables
- func CredentialsDir() (string, error)
- type Config
- type CredentialsStore
- func (cs *CredentialsStore) Delete(ctx context.Context, name string) error
- func (cs *CredentialsStore) Exists(ctx context.Context, name string) (bool, error)
- func (cs *CredentialsStore) Get(ctx context.Context, name string) (*goauth.Credentials, error)
- func (cs *CredentialsStore) GetWithTimeout(name string, timeout time.Duration) (*goauth.Credentials, error)
- func (cs *CredentialsStore) List(ctx context.Context) ([]string, error)
- func (cs *CredentialsStore) LoadCredentialsSet(ctx context.Context) (*goauth.CredentialsSet, error)
- func (cs *CredentialsStore) SaveCredentialsSet(ctx context.Context, set *goauth.CredentialsSet) error
- func (cs *CredentialsStore) Set(ctx context.Context, name string, creds *goauth.Credentials) error
- type TokenManager
- func New(cfg Config) (*TokenManager, error)
- func NewAuto() (*TokenManager, error)
- func NewFromCredentials(name string, creds *goauth.Credentials) (*TokenManager, error)
- func NewFromCredentialsFile(credentialsFile string) (*TokenManager, error)
- func NewFromCredentialsSet(set *goauth.CredentialsSet) (*TokenManager, error)
- func NewFromDirectory(directory string) (*TokenManager, error)
- func NewFromEnv(prefix string) (*TokenManager, error)
- func NewFromVaultURI(uri string) (*TokenManager, error)
- func NewWithVault(v vault.Vault) (*TokenManager, error)
- func (tm *TokenManager) Close() error
- func (tm *TokenManager) CredentialsStore() *CredentialsStore
- func (tm *TokenManager) DeleteCredentials(ctx context.Context, name string) error
- func (tm *TokenManager) GetClient(ctx context.Context, name string) (*http.Client, error)
- func (tm *TokenManager) GetCredentials(ctx context.Context, name string) (*goauth.Credentials, error)
- func (tm *TokenManager) GetToken(ctx context.Context, name string) (*oauth2.Token, error)
- func (tm *TokenManager) ListCredentials(ctx context.Context) ([]string, error)
- func (tm *TokenManager) LoadGoauthCredentials(ctx context.Context, name, credentialsFile, accountKey string) error
- func (tm *TokenManager) LoadGoogleServiceAccount(ctx context.Context, name, serviceAccountFile string, scopes []string) error
- func (tm *TokenManager) RefreshToken(ctx context.Context, name string) (*oauth2.Token, error)
- func (tm *TokenManager) SetCredentials(ctx context.Context, name string, creds *goauth.Credentials) error
- func (tm *TokenManager) TokenSet() tokens.TokenSet
- type VaultTokenSet
- func (ts *VaultTokenSet) DeleteToken(key string) error
- func (ts *VaultTokenSet) ExistsToken(key string) (bool, error)
- func (ts *VaultTokenSet) GetToken(key string) (*oauth2.Token, error)
- func (ts *VaultTokenSet) GetTokenInfo(key string) (*tokens.TokenInfo, error)
- func (ts *VaultTokenSet) ListTokens() ([]string, error)
- func (ts *VaultTokenSet) SetTokenInfo(key string, tokenInfo *tokens.TokenInfo) error
Constants ¶
This section is empty.
Variables ¶
var ( // ErrCredentialsNotFound is returned when credentials are not found in the vault. ErrCredentialsNotFound = errors.New("credentials not found") // ErrTokenNotFound is returned when a token is not found in the vault. ErrTokenNotFound = errors.New("token not found") // ErrTokenExpired is returned when a token has expired and cannot be refreshed. ErrTokenExpired = errors.New("token expired") // ErrRefreshFailed is returned when token refresh fails. ErrRefreshFailed = errors.New("token refresh failed") // ErrInvalidCredentials is returned when credentials are malformed. ErrInvalidCredentials = errors.New("invalid credentials") // ErrVaultNotConfigured is returned when vault is not configured. ErrVaultNotConfigured = errors.New("vault not configured") // ErrUnsupportedCredentialType is returned for unsupported credential types. ErrUnsupportedCredentialType = errors.New("unsupported credential type") )
Functions ¶
func CredentialsDir ¶
CredentialsDir returns the default credentials directory for the current user. On Unix: ~/.config/omnitoken/credentials On macOS: ~/Library/Application Support/omnitoken/credentials On Windows: %APPDATA%/omnitoken/credentials
Types ¶
type Config ¶
type Config struct {
// Vault is the vault backend for storing credentials and tokens.
// Required.
Vault vault.Vault
// CredentialsPrefix is the path prefix for storing credentials in the vault.
// Default: "credentials/"
CredentialsPrefix string
// TokensPrefix is the path prefix for storing tokens in the vault.
// Default: "tokens/"
TokensPrefix string
// RefreshBuffer is the time before expiry to trigger token refresh.
// Default: 60 seconds
RefreshBuffer time.Duration
// AutoRefresh enables automatic token refresh when tokens are near expiry.
// Default: true
AutoRefresh bool
// Logger is the logger for the token manager.
// Default: slog.Default()
Logger *slog.Logger
}
Config configures the TokenManager.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns a Config with default values. Vault must still be set before use.
type CredentialsStore ¶
type CredentialsStore struct {
// contains filtered or unexported fields
}
CredentialsStore manages goauth Credentials in a vault.
func NewCredentialsStore ¶
NewCredentialsStore creates a new CredentialsStore.
func (*CredentialsStore) Delete ¶
func (cs *CredentialsStore) Delete(ctx context.Context, name string) error
Delete removes credentials from the vault.
func (*CredentialsStore) Get ¶
func (cs *CredentialsStore) Get(ctx context.Context, name string) (*goauth.Credentials, error)
Get retrieves credentials from the vault by name.
func (*CredentialsStore) GetWithTimeout ¶
func (cs *CredentialsStore) GetWithTimeout(name string, timeout time.Duration) (*goauth.Credentials, error)
GetWithTimeout retrieves credentials with a timeout.
func (*CredentialsStore) List ¶
func (cs *CredentialsStore) List(ctx context.Context) ([]string, error)
List returns all credential names in the vault.
func (*CredentialsStore) LoadCredentialsSet ¶
func (cs *CredentialsStore) LoadCredentialsSet(ctx context.Context) (*goauth.CredentialsSet, error)
CredentialsSetFromVault loads a goauth.CredentialsSet from multiple vault entries.
func (*CredentialsStore) SaveCredentialsSet ¶
func (cs *CredentialsStore) SaveCredentialsSet(ctx context.Context, set *goauth.CredentialsSet) error
SaveCredentialsSet saves a goauth.CredentialsSet to the vault.
func (*CredentialsStore) Set ¶
func (cs *CredentialsStore) Set(ctx context.Context, name string, creds *goauth.Credentials) error
Set stores credentials in the vault.
type TokenManager ¶
type TokenManager struct {
// contains filtered or unexported fields
}
TokenManager manages credentials and tokens using vault storage. It provides a unified interface for obtaining authenticated HTTP clients from stored credentials, with automatic token refresh and caching.
func New ¶
func New(cfg Config) (*TokenManager, error)
New creates a new TokenManager with the given configuration.
func NewAuto ¶
func NewAuto() (*TokenManager, error)
NewAuto creates a TokenManager by auto-detecting configuration from environment. It checks the following in order:
- OMNITOKEN_VAULT_URI - vault URI (e.g., "op://vault", "file:///path")
- OMNITOKEN_CREDENTIALS_FILE - path to goauth CredentialsSet file
- Falls back to in-memory vault
func NewFromCredentials ¶
func NewFromCredentials(name string, creds *goauth.Credentials) (*TokenManager, error)
NewFromCredentials creates a TokenManager with a single credential. The credential is stored in an in-memory vault under the given name.
func NewFromCredentialsFile ¶
func NewFromCredentialsFile(credentialsFile string) (*TokenManager, error)
NewFromCredentialsFile creates a TokenManager from a goauth CredentialsSet JSON file. The credentials are loaded into an in-memory vault for the session. This is useful for file-based credential workflows without a persistent vault.
func NewFromCredentialsSet ¶
func NewFromCredentialsSet(set *goauth.CredentialsSet) (*TokenManager, error)
NewFromCredentialsSet creates a TokenManager from an existing goauth.CredentialsSet. The credentials are stored in an in-memory vault.
func NewFromDirectory ¶
func NewFromDirectory(directory string) (*TokenManager, error)
NewFromDirectory creates a TokenManager using a directory for file-based storage. Credentials are stored as JSON files in {directory}/credentials/ Tokens are stored as JSON files in {directory}/tokens/
This provides persistent storage without requiring a vault service.
func NewFromEnv ¶
func NewFromEnv(prefix string) (*TokenManager, error)
NewFromEnv creates a TokenManager that reads credentials from environment variables. Environment variables should be named with the pattern: {prefix}{NAME}_CREDENTIALS containing JSON-encoded goauth.Credentials.
If prefix is empty, it defaults to "OMNITOKEN_".
func NewFromVaultURI ¶
func NewFromVaultURI(uri string) (*TokenManager, error)
NewFromVaultURI creates a TokenManager from a vault URI. Supported URI schemes:
- memory:// - In-memory (testing)
- file:///path/to/dir - File-based storage
- env:// - Environment variables (with optional prefix: env://PREFIX_)
- op:// - 1Password (requires OP_SERVICE_ACCOUNT_TOKEN env var)
Additional providers can be registered via omnivault.RegisterProvider.
func NewWithVault ¶
func NewWithVault(v vault.Vault) (*TokenManager, error)
NewWithVault creates a new TokenManager with the given vault and default configuration.
func (*TokenManager) Close ¶
func (tm *TokenManager) Close() error
Close releases resources held by the token manager.
func (*TokenManager) CredentialsStore ¶
func (tm *TokenManager) CredentialsStore() *CredentialsStore
CredentialsStore returns the underlying credentials store.
func (*TokenManager) DeleteCredentials ¶
func (tm *TokenManager) DeleteCredentials(ctx context.Context, name string) error
DeleteCredentials removes credentials from the vault.
func (*TokenManager) GetClient ¶
GetClient returns an authenticated HTTP client for the named credentials. It retrieves credentials from the vault, obtains or refreshes the token as needed, and returns a client configured with the token.
func (*TokenManager) GetCredentials ¶
func (tm *TokenManager) GetCredentials(ctx context.Context, name string) (*goauth.Credentials, error)
GetCredentials retrieves credentials from the vault by name.
func (*TokenManager) GetToken ¶
GetToken returns a valid OAuth2 token for the named credentials. It retrieves from cache/vault if valid, or obtains a new token if needed.
func (*TokenManager) ListCredentials ¶
func (tm *TokenManager) ListCredentials(ctx context.Context) ([]string, error)
ListCredentials returns all credential names in the vault.
func (*TokenManager) LoadGoauthCredentials ¶
func (tm *TokenManager) LoadGoauthCredentials(ctx context.Context, name, credentialsFile, accountKey string) error
LoadGoauthCredentials loads a goauth credential from a CredentialsSet file. The credential is stored under the given name (or accountKey if name is empty).
func (*TokenManager) LoadGoogleServiceAccount ¶
func (tm *TokenManager) LoadGoogleServiceAccount(ctx context.Context, name, serviceAccountFile string, scopes []string) error
LoadGoogleServiceAccount loads a Google service account JSON file into the TokenManager. The credentials are stored under the given name with type "gcpsa". Scopes should be the OAuth2 scopes required for the Google APIs you'll use.
func (*TokenManager) RefreshToken ¶
RefreshToken forces a token refresh for the named credentials.
func (*TokenManager) SetCredentials ¶
func (tm *TokenManager) SetCredentials(ctx context.Context, name string, creds *goauth.Credentials) error
SetCredentials stores credentials in the vault.
func (*TokenManager) TokenSet ¶
func (tm *TokenManager) TokenSet() tokens.TokenSet
TokenSet returns the underlying goauth TokenSet implementation. This allows integration with goauth's multi-service token management.
type VaultTokenSet ¶
type VaultTokenSet struct {
// contains filtered or unexported fields
}
VaultTokenSet implements goauth's tokens.TokenSet interface backed by an omnivault. This allows goauth's multi-service token management to use vault storage.
func NewVaultTokenSet ¶
NewVaultTokenSet creates a new VaultTokenSet.
func (*VaultTokenSet) DeleteToken ¶
func (ts *VaultTokenSet) DeleteToken(key string) error
DeleteToken removes a token from the vault.
func (*VaultTokenSet) ExistsToken ¶
func (ts *VaultTokenSet) ExistsToken(key string) (bool, error)
ExistsToken checks if a token exists in the vault.
func (*VaultTokenSet) GetToken ¶
func (ts *VaultTokenSet) GetToken(key string) (*oauth2.Token, error)
GetToken retrieves just the OAuth2 token from the vault.
func (*VaultTokenSet) GetTokenInfo ¶
func (ts *VaultTokenSet) GetTokenInfo(key string) (*tokens.TokenInfo, error)
GetTokenInfo retrieves token info from the vault.
func (*VaultTokenSet) ListTokens ¶
func (ts *VaultTokenSet) ListTokens() ([]string, error)
ListTokens returns all token keys in the vault.
func (*VaultTokenSet) SetTokenInfo ¶
func (ts *VaultTokenSet) SetTokenInfo(key string, tokenInfo *tokens.TokenInfo) error
SetTokenInfo stores token info in the vault.