Skip to content

feat: keep OAuth tokens in the OS keychain - #131

Closed
mykytanetipa wants to merge 1 commit into
mykytanetipa/oauth-clifrom
mykytanetipa/oauth-keychain
Closed

mykytanetipa wants to merge 1 commit into
mykytanetipa/oauth-clifrom
mykytanetipa/oauth-keychain

Conversation

@mykytanetipa

Copy link
Copy Markdown

An owner-only file hides tokens from other users on the machine, not from other programs running as you, backups, or synced dotfiles. This adds an optional keychain store mode that keeps access and refresh tokens in macOS Keychain, Windows Credential Manager, or Linux Secret Service. Plain file storage remains the default.

Example:

$ a2a send -a https://agent.example.com --oauth-store keychain "hello"
To authorize, open:
https://auth.example.com/authorize?client_id=my-cli-client&...
Authorized. Grant saved for https://agent.example.com.
[agent] Hi!

$ cat ~/.config/a2a-cli/oauth/keychain-grants.json
{
  "version": 1,
  "grants": [
    {
      "key": {
        "issuer": "https://auth.example.com",
        "client_id": "my-cli-client",
        "resource": "https://agent.example.com",
        "scopes": ["a2a.invoke"]
      },
      "token_type": "Bearer",
      "expires_at": "2026-11-03T22:57:51.27214951Z",
      "endpoints": {
        "issuer": "https://auth.example.com",
        "authorization": "https://auth.example.com/authorize",
        "token": "https://auth.example.com/token",
        "revocation": "https://auth.example.com/revoke"
      },
      "created_at": "2026-11-03T21:57:51.27214951Z",
      "client_source": "config"
    }
  ]
}

$ a2a auth get https://agent.example.com --oauth-store keychain --reveal
oauth grants:
RESOURCE                   ISSUER                    CLIENT         SCOPES      EXPIRES
https://agent.example.com  https://auth.example.com  my-cli-client  a2a.invoke  2026-11-03T22:57:51Z (refreshable)
access token for https://agent.example.com: at_0422e9fa19c466f2d56d16e4

Cross-platform keychains accessed through go-keyring only look up entries by key and cannot enumerate stored grants on their own. To support listing and matching grants cleanly, the keychain store keeps non-secret metadata in its own index file, keychain-grants.json, so the file store never mistakes a keychain grant for one without tokens. That metadata covers issuer, client, agent URL, scopes, expiry, and endpoints, while the access and refresh tokens live in the OS keychain as separate entries, which keeps each within the size limits of the macOS and Windows stores.

Saving a grant writes to the keychain before touching the index file on disk. When no keychain service is running, such as on a headless Linux session without Secret Service, or a token is too large for it, the write fails immediately with a message saying which. Rather than quietly falling back to writing tokens to disk, it points the user to the file store.

Fixes #98

A 0600 file protects tokens from other users but not from other programs
running as the same user, backups or synced dotfiles. KeyringStore keeps
the tokens in the macOS Keychain, Windows Credential Manager or the
Linux Secret Service through zalando/go-keyring, one entry per token to
stay within the size limits of the macOS and Windows stores, and
everything else in keychain-grants.json, so grants can be listed without
reading secrets and the file store never reads a keychain grant as one
without tokens.

--oauth-store selects the store: file (default) or keychain. Like
--oauth-clients, it is not read from a discovered .env. Without a keychain,
for example on a headless Linux host with no Secret Service, saving a grant
fails and points to --oauth-store file instead of writing the tokens to
disk.

Refs #98
return nil, err
}
for i := range grants {
if err := s.fill(&grants[i]); err != nil {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

List reads both tokens of every grant from the keychain, but callers only compare keys and then Get the one they picked. let's return index metadata from List and read secrets only in Get

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in mykytanetipa#7. List reads only the index, which also records whether a grant is refreshable, and secrets are read only in Get. The SDK's Store contract allows List to leave tokens out (a2aproject/a2a-go#569).

@mykytanetipa

Copy link
Copy Markdown
Author

Closing in favor of the a2a-go OAuth client (a2aproject/a2a-go#568, a2aproject/a2a-go#569, a2aproject/a2a-go#570) and the CLI changes built on it (mykytanetipa#6, mykytanetipa#7).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: extensible OAuth authentication with protected resource discovery

2 participants