@owlmeans/cli-auth 0.1.18-rc.16 → 0.1.18-rc.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,180 @@
1
+ # @owlmeans/cli-auth
2
+
3
+ Browser sign-in for a command-line tool or a stdio MCP server, over the OAuth 2.1 device
4
+ authorization grant. It provides a dotenv-style `~/.owlmeans` credentials file (overridable, the
5
+ environment always wins), atomic `0600` writes, a cross-process sign-in lock so several invocations
6
+ converge on one browser round trip, a best-effort browser opener, and `makeCliCredentials` — a small
7
+ credential holder a CLI wraps its API calls with. It is a generic public utility for Node/Bun: no
8
+ React, no DOM, no OwlMeans context. Its current consumer is the closed-source `@owlmeans/viable-mcp`
9
+ connector, built outside this repository. A browser app signs in through `@owlmeans/client-auth`
10
+ instead; the protocol calls themselves (discovery, device authorization, polling, revoke) live in
11
+ `@owlmeans/oauth`, and the server side is any API built on `@owlmeans/server-oauth`.
12
+
13
+ ## Installation
14
+
15
+ ```bash
16
+ bun add @owlmeans/cli-auth@^0.1.18-rc.18
17
+ ```
18
+
19
+ ## Concepts
20
+
21
+ - **Credentials file** — `KEY=VALUE` lines at `OWLMEANS_CREDENTIALS`, else `~/.owlmeans`. One file
22
+ per deployment; files are never merged.
23
+ - **Precedence** — the process environment wins over the file, but an environment value that is the
24
+ empty string counts as unset.
25
+ - **Bound token** — a file token belongs to the API URL recorded under `apiUrlEnvKey`. A file naming
26
+ a different URL yields no token; a file naming none belongs to whatever URL is asked for.
27
+ - **Holder** — `makeCliCredentials(opts)` → `token()`, `require(waitMs?)`, `invalidate(rejected)`,
28
+ `signOut()`.
29
+ - **Single flight** — one device sign-in per API URL per process (module-level map), and across
30
+ processes a `<credentials file>.lock` that a second process joins instead of opening a second
31
+ browser tab. The lock is best-effort, not mutual exclusion.
32
+ - **Background sign-in** — when `require()` gives up waiting it throws `SignInRequired`; polling
33
+ continues and the next `require()` joins it.
34
+
35
+ ## Usage
36
+
37
+ ### Wrap API calls with the holder
38
+
39
+ ```typescript
40
+ import { makeCliCredentials } from '@owlmeans/cli-auth'
41
+
42
+ const credentials = makeCliCredentials({
43
+ apiUrl: 'https://api.example.com',
44
+ clientId: 'https://example.com/oauth/cli.json', // a static client id or a CIMD URL
45
+ tokenEnvKey: 'EXAMPLE_API_TOKEN',
46
+ apiUrlEnvKey: 'EXAMPLE_API_URL',
47
+ onNotify: message => process.stderr.write(`${message}\n`),
48
+ })
49
+
50
+ const token = await credentials.require() // starts or joins a device sign-in when needed
51
+
52
+ const res = await fetch(`${apiUrl}/projects`, { headers: { Authorization: `Bearer ${token}` } })
53
+ if (res.status === 401) {
54
+ await credentials.invalidate(token) // forget a file token; throw TokenRejected for an env token
55
+ }
56
+
57
+ await credentials.signOut() // revoke at the server (best effort) and clear the file token
58
+ ```
59
+
60
+ `require()` throws `SignInRequired` (from `@owlmeans/oauth`, carrying `url`, `code`, `expiresAt`)
61
+ when nobody approves within `waitMs` (default 20 s), `OAuthAccessDenied` when the person denies, and
62
+ `OAuthError` when the authorization expires or is aborted.
63
+
64
+ ### Present the token to OwlMeans routes
65
+
66
+ The holder's `token()` fits the thunk form of the `@owlmeans/auth-token` carrier guard, with
67
+ `invalidate` wired to the guard's `onRejected`.
68
+
69
+ ```typescript
70
+ import { DEFAULT_GUARD } from '@owlmeans/auth-common'
71
+ import { makeTokenCarrierGuard } from '@owlmeans/auth-token'
72
+
73
+ let current = ''
74
+ context.registerService(makeTokenCarrierGuard(DEFAULT_GUARD, {
75
+ token: async () => (current = await credentials.require()),
76
+ scheme: 'bearer',
77
+ onRejected: async () => { await credentials.invalidate(current) },
78
+ }))
79
+ ```
80
+
81
+ ### Read and write the credentials file directly
82
+
83
+ ```typescript
84
+ import { envFileHelper } from '@owlmeans/cli-auth'
85
+
86
+ const path = envFileHelper.resolveEnvFile()
87
+ const values = await envFileHelper.loadOwlmeansEnv() // file overlaid by non-empty env values
88
+
89
+ const { insecurePermissions } = await envFileHelper.setEnvValues(path, {
90
+ EXAMPLE_API_URL: 'https://api.example.com',
91
+ EXAMPLE_API_TOKEN: undefined, // undefined removes the key
92
+ })
93
+ if (insecurePermissions) {
94
+ console.warn(`${path} is readable by other users`)
95
+ }
96
+ ```
97
+
98
+ ### Open a browser
99
+
100
+ ```typescript
101
+ import { openBrowser } from '@owlmeans/cli-auth'
102
+
103
+ if (!openBrowser(verificationUri)) {
104
+ console.error(`Open ${verificationUri} in a browser`)
105
+ }
106
+ ```
107
+
108
+ ## API
109
+
110
+ ### Functions and helpers
111
+
112
+ | Symbol | Kind | Purpose |
113
+ |---|---|---|
114
+ | `makeCliCredentials(opts)` | function | the credential holder (`CliCredentials`) |
115
+ | `envFileHelper`, `createEnvFileHelper()` | helper | `resolveEnvFile`, `parseEnv`, `readCredentialsFile`, `loadOwlmeansEnv`, `setEnvValues` |
116
+ | `openBrowser(url, env?)` | function | detached `open` / `xdg-open` / `cmd /c start` with ignored stdio; `false` on any refusal |
117
+ | `lockPathFor(credentialsPath)` | function | `<credentialsPath>.lock` |
118
+ | `makeSignInLockHelper(path)` | function | `readLock`, `claimOrJoinLock(apiUrl, draft)`, `releaseLock(nonce)` |
119
+ | `loadOwlmeansEnv(env?)` | deprecated function | `compat:factory-refactor` wrapper — use `envFileHelper.loadOwlmeansEnv` |
120
+
121
+ ### `CliCredentialsOptions`
122
+
123
+ | Field | Purpose |
124
+ |---|---|
125
+ | `apiUrl` | the API origin; also the OAuth `resource` unless `resource` is set |
126
+ | `clientId` | the CLI's OAuth `client_id` — static, or a Client ID Metadata Document URL |
127
+ | `tokenEnvKey`, `apiUrlEnvKey` | key names for the token and its bound URL, in the file and the environment |
128
+ | `deviceName?` | defaults to `<hostname> · <user>` |
129
+ | `resource?`, `scope?` | OAuth request parameters |
130
+ | `env?` | defaults to `process.env` |
131
+ | `onNotify?` | progress messages ("Sign in at … with code …", "Signed in.") |
132
+
133
+ ### Constants
134
+
135
+ | Symbol | Value |
136
+ |---|---|
137
+ | `ENV_CREDENTIALS_FILE` | `'OWLMEANS_CREDENTIALS'` |
138
+ | `DEFAULT_CREDENTIALS_FILENAME` | `'.owlmeans'` |
139
+ | `DEFAULT_WAIT_MS` | `20_000` — `require()` wait before `SignInRequired` |
140
+ | `MAX_SIGN_IN_WAIT_MS` | 15 minutes — the poller's own ceiling |
141
+
142
+ ### Types
143
+
144
+ `CliCredentials`, `CliCredentialsOptions`, `SignInLockInfo`, `SignInLockDraft`, `SignInLockClaim`,
145
+ `SignInLockHelper`, `EnvFileHelper`, `SetEnvValuesResult`.
146
+
147
+ ## Browser suppression
148
+
149
+ `openBrowser` returns `false` without spawning when `OWLMEANS_NO_BROWSER=1` or `BROWSER=none`, on
150
+ Linux with neither `DISPLAY` nor `WAYLAND_DISPLAY`, and on an unknown platform. The printed
151
+ "Sign in at … with code …" line is the real interface; the browser is a convenience. Only the lock
152
+ owner opens a browser.
153
+
154
+ ## Common pitfalls
155
+
156
+ - Never read the token from argv or write it into a harness or config file; it lives in the
157
+ environment or the credentials file.
158
+ - An environment token that the server rejects is reported (`TokenRejected`), never silently
159
+ replaced by a new sign-in.
160
+ - A token stored for one API URL is not sent to another; point `OWLMEANS_CREDENTIALS` at a separate
161
+ file per deployment.
162
+ - Loose permissions on an existing file are reported, not tightened.
163
+ - In tests, point `OWLMEANS_CREDENTIALS` at a temp path, use a distinct `apiUrl` per test (the
164
+ in-flight map is module-level), and set `OWLMEANS_NO_BROWSER=1`.
165
+ - Any timer added around `require()` must be cleared; a pending timer keeps the process alive after
166
+ sign-in.
167
+
168
+ The `cli-auth` skill covers file precedence, the holder's single-flight rules, the lock and test
169
+ isolation; the `oauth` skill covers the device grant itself.
170
+
171
+ ## Related packages
172
+
173
+ - [`@owlmeans/oauth`](../oauth) — discovery, device authorization, polling, revoke and `SignInRequired`
174
+ - [`@owlmeans/server-oauth`](../server-oauth) — the authorization server a CLI signs in against
175
+ - [`@owlmeans/auth-token`](../auth-token) — the carrier guard that presents the resulting token
176
+ - [`@owlmeans/error`](../error) — `ResilientError`, the base of the thrown OAuth errors
177
+
1
178
  <!-- owlmeans:agent-guidance:start -->
2
179
  ## Agent guidance
3
180
 
@@ -6,7 +183,7 @@ This package ships embedded agent skills under `agent-meta/`. After installing y
6
183
  your project's skill store (`.agents/skills/`):
7
184
 
8
185
  ```sh
9
- npx @owlmeans/agent-skills@^0.1.18-rc.49
186
+ npx @owlmeans/agent-skills@^0.1.18-rc.57
10
187
  ```
11
188
 
12
189
  The embedded files are version-matched to this package release. Do not edit them
@@ -1,8 +1,8 @@
1
1
  {
2
- "schemaVersion": 2,
2
+ "schemaVersion": 3,
3
3
  "package": "@owlmeans/cli-auth",
4
- "version": "0.1.18-rc.16",
5
- "generatedAt": "2026-10-05T21:03:06.419Z",
4
+ "version": "0.1.18-rc.18",
5
+ "generatedAt": "2026-10-20T23:19:58.650Z",
6
6
  "canonicalRepo": "https://github.com/owlmeans/common",
7
7
  "entries": [
8
8
  {
@@ -8,9 +8,9 @@ user-invocable: false
8
8
  # @owlmeans/cli-auth
9
9
 
10
10
  **Layer:** Tooling (Node/Bun — no React, no DOM, no framework runtime)
11
- **Install:** `"@owlmeans/cli-auth": "^0.1.18-rc.16"` in `dependencies`
11
+ **Install:** `"@owlmeans/cli-auth": "^0.1.18-rc.18"` in `dependencies`
12
12
  **Contracts:** `@owlmeans/oauth` (discovery, device authorization, polling, revoke, `SignInRequired`)
13
- **Server half:** any API built on `@owlmeans/server-oauth`; **consumers:** `@owlmeans/viable-mcp`
13
+ **Server half:** any API built on `@owlmeans/server-oauth`; **consumers:** `@owlmeans/viable-mcp` (closed-source, built outside this repository)
14
14
 
15
15
  ## Key Exports
16
16
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@owlmeans/cli-auth",
3
- "version": "0.1.18-rc.16",
3
+ "version": "0.1.18-rc.18",
4
4
  "license": "MIT",
5
5
  "description": "OAuth device-authorization sign-in for a command-line tool: a dotenv-style ~/.owlmeans credentials file (overridable, environment always wins), a cross-process sign-in lock so several invocations converge on one browser round trip, a browser opener, and a small credential holder a CLI wraps its API calls with.",
6
6
  "type": "module",
@@ -23,8 +23,8 @@
23
23
  }
24
24
  },
25
25
  "dependencies": {
26
- "@owlmeans/error": "^0.1.18-rc.38",
27
- "@owlmeans/oauth": "^0.1.18-rc.16"
26
+ "@owlmeans/error": "^0.1.18-rc.39",
27
+ "@owlmeans/oauth": "^0.1.18-rc.18"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@owlmeans/dep-config": "workspace:*",