Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 107 lines 3.61 KB
Raw
1 /**
2 * Store contract and process-global registry.
3 *
4 * Mirrors `keyring_core::api::CredentialStoreApi` + the
5 * `set_default_store` / `get_default_store` / `unset_default_store`
6 * helpers from `keyring_core::lib`. Tests can swap the default store
7 * with an in-memory mock via `setDefaultStore`.
8 */
9
10 import type { CredentialPersistence } from './persistence';
11 import { KeyringError } from './errors';
12
13 /**
14 * Optional per-entry modifiers supported by this package. Not every
15 * store honors every modifier:
16 *
17 * - `label` — human-readable label shown in Keychain Access / seahorse.
18 * If omitted, stores default to `keyring:{user}@{service}`
19 * to match the label format keyring-rs produces.
20 * - `collection` — Secret Service collection name (Linux only).
21 * Defaults to `"default"`, matching keyring-rs's `target`
22 * attribute convention.
23 * - `keychain` — macOS keychain domain (`User` | `System` | `Common` |
24 * `Dynamic`). Defaults to `User` (the login keychain).
25 */
26 export interface EntryModifiers {
27 readonly label?: string;
28 readonly collection?: string;
29 readonly keychain?: 'User' | 'System' | 'Common' | 'Dynamic';
30 }
31
32 /**
33 * The low-level operations a backend must implement. `Entry` is a thin
34 * specifier wrapper that routes every call through the active store.
35 *
36 * All operations are async because both supported backends are
37 * subprocess-based (`/usr/bin/security`, `secret-tool`). Throwing
38 * `KeyringError` with a precise `kind` is required — callers need to
39 * distinguish `NoEntry` from `NoStorageAccess` at minimum.
40 */
41 export interface CredentialStore {
42 /** Short identifier shown in error messages and logs. */
43 readonly id: string;
44 /** Vendor / platform description. */
45 readonly vendor: string;
46
47 /** Lifetime characteristics of credentials stored by this backend. */
48 persistence(): CredentialPersistence;
49
50 /**
51 * Store `secret` under `(service, user)`. Overwrites any existing
52 * value for the same specifier.
53 */
54 setSecret(
55 service: string,
56 user: string,
57 secret: Uint8Array,
58 modifiers: EntryModifiers
59 ): Promise<void>;
60
61 /**
62 * Fetch the raw bytes previously stored via `setSecret`. Must throw
63 * `KeyringError({ kind: 'NoEntry' })` if nothing matches.
64 */
65 getSecret(service: string, user: string, modifiers: EntryModifiers): Promise<Uint8Array>;
66
67 /**
68 * Remove the credential identified by `(service, user)`. Throws
69 * `NoEntry` if it didn't exist.
70 */
71 deleteCredential(service: string, user: string, modifiers: EntryModifiers): Promise<void>;
72 }
73
74 // -----------------------------------------------------------------------------
75 // Process-global default store registry
76 // -----------------------------------------------------------------------------
77
78 let defaultStore: CredentialStore | null = null;
79
80 /**
81 * Register the store that `new Entry(service, user)` should dispatch to.
82 * The CLI calls this once at startup; tests call it to inject a mock.
83 */
84 export function setDefaultStore(store: CredentialStore): void {
85 defaultStore = store;
86 }
87
88 /** Clear the registered store — mostly useful for test teardown. */
89 export function unsetDefaultStore(): void {
90 defaultStore = null;
91 }
92
93 /**
94 * Return the active default store or throw `NoDefaultStore` if none is
95 * registered. Callers that want a non-throwing check should use
96 * `hasDefaultStore()` instead.
97 */
98 export function getDefaultStore(): CredentialStore {
99 if (defaultStore === null) {
100 throw new KeyringError({ kind: 'NoDefaultStore' });
101 }
102 return defaultStore;
103 }
104
105 export function hasDefaultStore(): boolean {
106 return defaultStore !== null;
107 }