Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 04:45 UTC
| 1 | /** |
| 2 | * `Entry` — the specifier-based credential handle users interact with. |
| 3 | * |
| 4 | * Mirrors `keyring_core::api::Entry` / `CredentialApi`. An `Entry` is |
| 5 | * just a `(service, user, modifiers)` triple that routes calls through |
| 6 | * the process-global default store. There is no long-lived handle to |
| 7 | * the underlying credential — every call is a round-trip to the OS. |
| 8 | */ |
| 9 | |
| 10 | import { KeyringError } from './errors'; |
| 11 | import { type CredentialStore, type EntryModifiers, getDefaultStore } from './store'; |
| 12 | |
| 13 | const textEncoder = new TextEncoder(); |
| 14 | const textDecoder = new TextDecoder('utf-8', { fatal: true }); |
| 15 | |
| 16 | /** |
| 17 | * Identifies a credential by `service` + `user`, with optional |
| 18 | * per-store modifiers (keychain domain, Secret Service collection, |
| 19 | * custom label). |
| 20 | * |
| 21 | * Both `service` and `user` must be non-empty — empty strings act as |
| 22 | * wildcards in Keychain Services and would silently delete other |
| 23 | * credentials. This mirrors keyring-rs's `Invalid` check on the macOS |
| 24 | * backend and we enforce it uniformly so behavior doesn't diverge by |
| 25 | * platform. |
| 26 | */ |
| 27 | export class Entry { |
| 28 | readonly service: string; |
| 29 | readonly user: string; |
| 30 | readonly modifiers: EntryModifiers; |
| 31 | |
| 32 | /** |
| 33 | * Override the store just for this entry (e.g. tests). When `null`, |
| 34 | * the process-global default store is resolved on every call so that |
| 35 | * `setDefaultStore` / `unsetDefaultStore` take effect immediately. |
| 36 | */ |
| 37 | private readonly overrideStore: CredentialStore | null; |
| 38 | |
| 39 | constructor( |
| 40 | service: string, |
| 41 | user: string, |
| 42 | modifiers: EntryModifiers = {}, |
| 43 | overrideStore: CredentialStore | null = null |
| 44 | ) { |
| 45 | if (service.length === 0) { |
| 46 | throw new KeyringError({ |
| 47 | kind: 'Invalid', |
| 48 | param: 'service', |
| 49 | reason: 'service must be a non-empty string', |
| 50 | }); |
| 51 | } |
| 52 | if (user.length === 0) { |
| 53 | throw new KeyringError({ |
| 54 | kind: 'Invalid', |
| 55 | param: 'user', |
| 56 | reason: 'user must be a non-empty string', |
| 57 | }); |
| 58 | } |
| 59 | this.service = service; |
| 60 | this.user = user; |
| 61 | this.modifiers = modifiers; |
| 62 | this.overrideStore = overrideStore; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Store `password` as UTF-8 bytes under this entry's specifier. |
| 67 | * Overwrites any existing value. |
| 68 | */ |
| 69 | async setPassword(password: string): Promise<void> { |
| 70 | await this.setSecret(textEncoder.encode(password)); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Fetch the stored value and decode it as UTF-8. Throws |
| 75 | * `KeyringError({ kind: 'NoEntry' })` if nothing was stored, or |
| 76 | * `KeyringError({ kind: 'BadEncoding', bytes })` if the stored bytes |
| 77 | * aren't valid UTF-8 — matching keyring-rs's `Error::BadEncoding`. |
| 78 | */ |
| 79 | async getPassword(): Promise<string> { |
| 80 | const bytes = await this.getSecret(); |
| 81 | try { |
| 82 | return textDecoder.decode(bytes); |
| 83 | } catch { |
| 84 | throw new KeyringError({ kind: 'BadEncoding', bytes }); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /** Store raw bytes. */ |
| 89 | async setSecret(secret: Uint8Array): Promise<void> { |
| 90 | await this.resolveStore().setSecret(this.service, this.user, secret, this.modifiers); |
| 91 | } |
| 92 | |
| 93 | /** Fetch raw bytes. Throws `NoEntry` if nothing matches. */ |
| 94 | async getSecret(): Promise<Uint8Array> { |
| 95 | return this.resolveStore().getSecret(this.service, this.user, this.modifiers); |
| 96 | } |
| 97 | |
| 98 | /** Delete the credential. Throws `NoEntry` if it didn't exist. */ |
| 99 | async deleteCredential(): Promise<void> { |
| 100 | await this.resolveStore().deleteCredential(this.service, this.user, this.modifiers); |
| 101 | } |
| 102 | |
| 103 | private resolveStore(): CredentialStore { |
| 104 | return this.overrideStore ?? getDefaultStore(); |
| 105 | } |
| 106 | } |