Implementation:1ay1 Agentty Provider Key Vault
| Knowledge Sources | |
|---|---|
| Domains | Auth, Security |
| Last Updated | 2026-10-28 12:00 GMT |
Overview
Sealed at-rest vault for provider API keys, persisting through the same machine-bound AES-256-GCM envelope as credentials.json, mirrored into the OS keystore when enabled.
Description
The agentty::auth::keys module is the sealed vault for provider API keys. Hosted-preset keys ("openai", "groq") and custom-host keys (host:port / URL specs) previously rested in plaintext in `settings.json` under `"provider_keys"`. Every provider key now persists through this module into `provider-keys.json` — the same machine-bound AES-256-GCM envelope credentials.json uses, mirrored into the OS keystore when `AGENTTY_USE_KEYSTORE` is enabled.
The runtime story is unchanged: `store::Settings::provider_keys` stays the in-memory source of truth (selection, picker rows, `resolve()` all read it); only the at-rest layer moves. An empty map saved is authoritative (a sign-out must be able to clear the vault), so `save()` always writes — there is no "skip empty" short-circuit. On first load, a legacy plaintext `provider_keys` object in settings.json is imported into the vault and stripped from settings.json.
Usage
Import this when you need to load or save the provider API key vault at rest. The in-memory `Settings::provider_keys` map converts directly to/from the `KeyMap` type.
Code Reference
Source Location
- Repository: 1ay1_Agentty
- File: include/agentty/auth/keys.hpp
- Lines: 1-54
Signature
namespace agentty::auth::keys {
using KeyMap = std::map<std::string, std::string>; // provider id → key
[[nodiscard]] std::string path(); // <credentials>/provider-keys.json
// Read: keystore first (when enabled), sealed file as fallback.
// Returns {} on nothing stored or auth failure (fail closed).
[[nodiscard]] KeyMap load();
// Seal + persist (keystore mirror + atomic 0600 file write).
// Returns false on sealing/write failure — never falls back to plaintext.
bool save(const KeyMap& keys);
// Best-effort wipe: keystore item + sealed file.
void clear();
}
Import
#include "agentty/auth/keys.hpp"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| keys | KeyMap (std::map<string,string>) | Yes (save) | Provider id / custom-host spec → API key |
| AGENTTY_USE_KEYSTORE | env var | When truthy, mirrors the sealed envelope into the OS keystore |
Outputs
| Name | Type | Description |
|---|---|---|
| load() | KeyMap | All stored keys; empty on nothing stored or auth failure |
| save() | bool | True on success; false on seal/write failure |
Usage Examples
Load and Save Provider Keys
#include "agentty/auth/keys.hpp"
// Load the vault at startup
auto keys = agentty::auth::keys::load();
// keys["openai"] = "sk-...", keys["groq"] = "gsk_..."
// The in-memory Settings map converts directly
settings.provider_keys = keys;
// Save after a user enters a new key via the login modal
settings.provider_keys["openai"] = new_key;
agentty::auth::keys::save(settings.provider_keys);
// → sealed envelope written to provider-keys.json (0600)
// → mirrored to OS keystore when AGENTTY_USE_KEYSTORE is set
Clear All Provider Keys (Sign Out)
settings.provider_keys.clear();
agentty::auth::keys::save(settings.provider_keys); // authoritative empty
// Or best-effort wipe:
agentty::auth::keys::clear();