@octen.ai/cli 0.8.0 → 0.9.0
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 +250 -4
- package/dist/api/errors.d.ts +15 -0
- package/dist/api/errors.js +15 -0
- package/dist/auth/constants.d.ts +47 -0
- package/dist/auth/constants.js +89 -0
- package/dist/auth/exchange.d.ts +74 -0
- package/dist/auth/exchange.js +124 -0
- package/dist/auth/login.d.ts +64 -0
- package/dist/auth/login.js +163 -0
- package/dist/auth/loopback.d.ts +32 -0
- package/dist/auth/loopback.js +95 -0
- package/dist/auth/oauthClient.d.ts +70 -0
- package/dist/auth/oauthClient.js +136 -0
- package/dist/auth/pkce.d.ts +6 -0
- package/dist/auth/pkce.js +13 -0
- package/dist/auth/revoke.d.ts +58 -0
- package/dist/auth/revoke.js +91 -0
- package/dist/auth/store.d.ts +34 -0
- package/dist/auth/store.js +149 -0
- package/dist/cli.js +6 -0
- package/dist/commands/configureMcp.js +34 -18
- package/dist/commands/configureSkills.js +18 -4
- package/dist/commands/login.d.ts +49 -0
- package/dist/commands/login.js +161 -0
- package/dist/commands/logout.d.ts +46 -0
- package/dist/commands/logout.js +142 -0
- package/dist/commands/reset.js +39 -3
- package/dist/commands/whoami.d.ts +27 -0
- package/dist/commands/whoami.js +109 -0
- package/dist/config/resolve.d.ts +44 -1
- package/dist/config/resolve.js +80 -5
- package/dist/mcp/install.js +15 -2
- package/dist/mcp/write.js +24 -3
- package/dist/output/pretty/whoami.d.ts +59 -0
- package/dist/output/pretty/whoami.js +103 -0
- package/dist/skills/setkey.js +5 -4
- package/dist/util/secretFile.d.ts +24 -0
- package/dist/util/secretFile.js +35 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -43,16 +43,246 @@ octen completion zsh --install # writes to ~/.zshrc — then: source ~/.zshr
|
|
|
43
43
|
|
|
44
44
|
## Auth
|
|
45
45
|
|
|
46
|
-
|
|
46
|
+
Octen CLI resolves the API key to use in this order — each step stopping resolution before
|
|
47
|
+
the next, so an explicit value always wins over a stored one:
|
|
48
|
+
|
|
49
|
+
1. `--api-key <key>` on the command itself
|
|
50
|
+
2. `OCTEN_API_KEY` in the environment
|
|
51
|
+
3. `~/.octen/credentials.json`, written by `octen login`
|
|
52
|
+
|
|
53
|
+
Get a key at https://octen.ai. Either export it directly:
|
|
47
54
|
|
|
48
55
|
```sh
|
|
49
56
|
export OCTEN_API_KEY=your_key_here
|
|
50
57
|
```
|
|
51
58
|
|
|
52
|
-
|
|
59
|
+
or log in once and let the CLI manage it:
|
|
60
|
+
|
|
61
|
+
```sh
|
|
62
|
+
octen login
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
`octen login` opens your browser, completes an OAuth consent flow, and stores the resulting
|
|
66
|
+
API key at `~/.octen/credentials.json` (written with `0600` permissions). That key does not
|
|
67
|
+
expire, so `octen configure-mcp` and `octen configure-skills --set-key` can write it into
|
|
68
|
+
AI-client configs once and forget about it.
|
|
69
|
+
|
|
70
|
+
You can also pass `--api-key <key>` or `--base-url <url>` on any command; to point at a
|
|
71
|
+
self-hosted or staging endpoint, set `OCTEN_API_URL` or pass `--base-url <url>`.
|
|
72
|
+
|
|
73
|
+
Run `octen whoami` to see which of the three sources is actually in effect — it names the
|
|
74
|
+
winner explicitly, so a stored credential that is being shadowed or ignored says so rather
|
|
75
|
+
than looking live.
|
|
76
|
+
|
|
77
|
+
### Auth environment variables
|
|
78
|
+
|
|
79
|
+
| Variable | Default | What it does |
|
|
80
|
+
| --- | --- | --- |
|
|
81
|
+
| `OCTEN_API_KEY` | — | The API key to use. Takes precedence over `~/.octen/credentials.json`, so a stored login is ignored while it is set. |
|
|
82
|
+
| `OCTEN_API_URL` | `https://api.octen.ai` | The Octen API base URL. |
|
|
83
|
+
| `OCTEN_AUTH_ISSUER` | `https://auth.octen.ai` | The OAuth authorization server `octen login` talks to. Only for local development against a self-hosted AS. |
|
|
84
|
+
| `OCTEN_AUTH_RESOURCE` | `https://cli.octen.ai` | The audience `octen login` requests the access token for. Despite the shape it is an identifier, not a site — nothing ever connects to it. Only for local development. |
|
|
85
|
+
|
|
86
|
+
`OCTEN_AUTH_ISSUER` and `OCTEN_AUTH_RESOURCE` **must not have a trailing slash** — a
|
|
87
|
+
trailing one is rejected with a named error rather than silently trimmed, because trimming
|
|
88
|
+
is what once turned a JWKS URL into `//api/oauth/jwks` and made a byte-for-byte issuer
|
|
89
|
+
comparison fail.
|
|
90
|
+
|
|
91
|
+
Changing either of them makes an existing stored credential **inapplicable**: the credential
|
|
92
|
+
records the issuer and resource it was minted for, and one minted for a different pair
|
|
93
|
+
belongs to another environment. The CLI never uses it and never deletes it — it says so and
|
|
94
|
+
names the variable. Unset the variable to go back to the stored credential, or run `octen
|
|
95
|
+
login` again to get one for the new environment. `octen whoami` shows both the stored
|
|
96
|
+
`Issuer`/`Resource` and which credential is in effect.
|
|
97
|
+
|
|
98
|
+
### What the resource is, and why there are two
|
|
99
|
+
|
|
100
|
+
One authorization server issues access tokens for two different audiences, and they are
|
|
101
|
+
deliberately not interchangeable:
|
|
102
|
+
|
|
103
|
+
| Audience | Scope | What a token for it can do |
|
|
104
|
+
| --- | --- | --- |
|
|
105
|
+
| `https://mcp.octen.ai/mcp` | `mcp:tools` | Call MCP tools |
|
|
106
|
+
| `https://cli.octen.ai` | `octen:api_key` | Exchange for your account's API key |
|
|
107
|
+
|
|
108
|
+
Only the second can be traded for the API key. If both shared one audience, a token minted
|
|
109
|
+
for the MCP server — the one an AI client holds — could be replayed against the CLI's
|
|
110
|
+
exchange endpoint and walk away with that key. The `resource` is what keeps them apart: the
|
|
111
|
+
server checks that a token's granted audience is exactly this value before handing anything
|
|
112
|
+
over, and answers `403` otherwise.
|
|
113
|
+
|
|
114
|
+
That check is a byte-for-byte comparison, with no normalization anywhere along the chain,
|
|
115
|
+
which is why the trailing-slash rule above is a rejection rather than a quiet trim.
|
|
116
|
+
Normalizing in one place and not another produces a 401 on every request while each side
|
|
117
|
+
looks correct in isolation.
|
|
118
|
+
|
|
119
|
+
You do not need to set `OCTEN_AUTH_RESOURCE` to use Octen. It exists so a locally running
|
|
120
|
+
authorization server can use a different audience string.
|
|
121
|
+
|
|
122
|
+
**Add `.octen/` to your global gitignore.** A credentials file living in a dotfile directory
|
|
123
|
+
is more likely to be committed by accident than an environment variable ever was:
|
|
124
|
+
|
|
125
|
+
```sh
|
|
126
|
+
git config --global core.excludesfile ~/.gitignore_global
|
|
127
|
+
echo ".octen/" >> ~/.gitignore_global
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
**No browser on this machine (CI, containers)?** Paste a key directly — this branch makes
|
|
131
|
+
zero network requests:
|
|
132
|
+
|
|
133
|
+
```sh
|
|
134
|
+
octen login --api-key "$OCTEN_API_KEY"
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
**Over SSH**, forward the loopback callback port and pin it on both ends:
|
|
138
|
+
|
|
139
|
+
```sh
|
|
140
|
+
# on the remote machine
|
|
141
|
+
octen login --port 8765
|
|
142
|
+
# from your local machine, before approving the consent screen in your browser
|
|
143
|
+
ssh -L 8765:localhost:8765 remote-host
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### `octen logout`'s exact semantics
|
|
147
|
+
|
|
148
|
+
`octen logout` deletes `~/.octen/credentials.json` and revokes *this device's authorization*
|
|
149
|
+
(the OAuth grant behind it). Revoking the authorization only prevents it from being used to
|
|
150
|
+
silently mint a new credential later — it does **not** deactivate the underlying API key.
|
|
151
|
+
That key is your own account-wide, long-lived key, and the same key is very likely also
|
|
152
|
+
sitting on other machines, in your production code, and in the AI-client configs `octen
|
|
153
|
+
configure-mcp` wrote. All of those keep working after `octen logout` — that's by design,
|
|
154
|
+
since they're your own legitimate uses of your own key. If you believe the key itself was
|
|
155
|
+
exposed, deactivate or rotate it from the key management page at https://octen.ai — that
|
|
156
|
+
action, unlike `logout`, affects every place holding the key.
|
|
157
|
+
|
|
158
|
+
`octen logout --local` skips the network call entirely and only removes the local file,
|
|
159
|
+
without attempting to revoke the authorization.
|
|
160
|
+
|
|
161
|
+
### One key, used by both the CLI and MCP
|
|
162
|
+
|
|
163
|
+
The audience split above applies to OAuth *access tokens*. The API key you end up with is
|
|
164
|
+
not audience-bound at all — it is your own account-wide key, and using it in more than one
|
|
165
|
+
place is the normal case, not a workaround. `octen configure-mcp` does exactly that: it
|
|
166
|
+
resolves your key through the same `--api-key` > `OCTEN_API_KEY` > stored-login order as
|
|
167
|
+
every other command and writes it into the MCP client config.
|
|
168
|
+
|
|
169
|
+
Three consequences of sharing one key:
|
|
170
|
+
|
|
171
|
+
- **`octen logout` does not stop MCP.** It revokes this device's authorization, not the key.
|
|
172
|
+
The key is still sitting in the MCP config and keeps working. To stop it everywhere,
|
|
173
|
+
deactivate or rotate the key from the key management page at https://octen.ai.
|
|
174
|
+
- **Usage and billing are pooled.** One key, one account — CLI calls and MCP calls land in
|
|
175
|
+
the same bucket.
|
|
176
|
+
- **The blast radius is shared.** Rotating or losing the key affects both at once.
|
|
177
|
+
|
|
178
|
+
To keep the two independent, pick a separate key for the CLI in the consent screen's **API
|
|
179
|
+
Key** dropdown instead of accepting the default, and leave MCP on another one.
|
|
180
|
+
|
|
181
|
+
This applies to the local stdio MCP server (`npx octen-mcp`, what `octen configure-mcp`
|
|
182
|
+
configures). A **remote OAuth connector** holds no API key at all — it carries an
|
|
183
|
+
MCP-audience token that the server resolves internally — so it is already independent of
|
|
184
|
+
whatever the CLI stores.
|
|
185
|
+
|
|
186
|
+
### Dashboard "revoke" and an already-logged-in CLI
|
|
187
|
+
|
|
188
|
+
The dashboard's per-authorization "revoke" button has **no effect on a device that already
|
|
189
|
+
holds a credential**: it only stops that device from obtaining a *new* one without going
|
|
190
|
+
through the consent screen again — the API key it already has keeps working exactly as
|
|
191
|
+
before. The authorization still shows up in the dashboard's list either way; run `octen
|
|
192
|
+
whoami` on the device in question to get its `grantId` so you can identify which entry in
|
|
193
|
+
that list corresponds to it. To actually stop a key from working anywhere, deactivate or
|
|
194
|
+
rotate it from the key management page, not the authorization list.
|
|
195
|
+
|
|
196
|
+
### Switching from a browser login to a pasted key
|
|
197
|
+
|
|
198
|
+
`octen login --api-key <KEY>` overwrites any existing `source: login` credential on disk
|
|
199
|
+
without revoking its grant — that branch makes no network request at all, deliberately, so
|
|
200
|
+
that it still works on a machine with no browser and no reachable auth server (see above).
|
|
201
|
+
So if you switch a machine from a browser login to a pasted key, the old
|
|
202
|
+
authorization is left behind: it stays listed as active in the dashboard's authorization
|
|
203
|
+
list, and once its `grantId` is gone from this machine's disk, nothing here can revoke it —
|
|
204
|
+
only the dashboard can. The command prints that `grantId` on stderr before overwriting the
|
|
205
|
+
file, so you can still find the entry; revoke it there directly if you want to clean it up,
|
|
206
|
+
or run `octen logout` first to revoke it properly.
|
|
53
207
|
|
|
54
208
|
## Commands
|
|
55
209
|
|
|
210
|
+
### `octen login`
|
|
211
|
+
|
|
212
|
+
Log in via your browser and store the resulting long-lived API key at
|
|
213
|
+
`~/.octen/credentials.json`.
|
|
214
|
+
|
|
215
|
+
```sh
|
|
216
|
+
octen login
|
|
217
|
+
|
|
218
|
+
# print the authorize URL instead of opening a browser
|
|
219
|
+
octen login --no-browser
|
|
220
|
+
|
|
221
|
+
# pin the loopback callback port, e.g. for the ssh -L forwarding above
|
|
222
|
+
octen login --port 8765
|
|
223
|
+
|
|
224
|
+
# no browser available: store a key directly, with zero network requests
|
|
225
|
+
octen login --api-key "$OCTEN_API_KEY"
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
The consent screen asks for two things, and shows one you should check:
|
|
229
|
+
|
|
230
|
+
- **Account** — whose key this is: your personal account, or an organization you belong to.
|
|
231
|
+
This decides whose usage and billing the key draws on.
|
|
232
|
+
- **API Key** — which of that account's keys to hand over. Pick a separate one here if you
|
|
233
|
+
want the CLI's key independent of the one MCP uses (see
|
|
234
|
+
[One key, used by both the CLI and MCP](#one-key-used-by-both-the-cli-and-mcp)).
|
|
235
|
+
- **"Authorization result will be sent to 127.0.0.1:PORT"** — the loopback listener this
|
|
236
|
+
`octen login` just opened. If you did not just run it, press **Deny**.
|
|
237
|
+
|
|
238
|
+
On success the terminal names the account you approved as — `Logged in as Octen family.` —
|
|
239
|
+
falling back to the raw account id when the server does not supply a name.
|
|
240
|
+
|
|
241
|
+
If a `source: login` credential already exists, `octen login` best-effort revokes its grant
|
|
242
|
+
before starting the new login, so repeated logins don't accumulate orphaned authorizations
|
|
243
|
+
in the dashboard. A failure to revoke never blocks the new login.
|
|
244
|
+
|
|
245
|
+
Options: `--port <n>`, `--no-browser`.
|
|
246
|
+
|
|
247
|
+
---
|
|
248
|
+
|
|
249
|
+
### `octen logout`
|
|
250
|
+
|
|
251
|
+
Revoke this device's authorization and remove the locally stored credential. See
|
|
252
|
+
[Auth](#auth) above for the exact semantics — this does **not** deactivate the API key
|
|
253
|
+
itself.
|
|
254
|
+
|
|
255
|
+
```sh
|
|
256
|
+
octen logout
|
|
257
|
+
|
|
258
|
+
# skip the network call; only remove the local file
|
|
259
|
+
octen logout --local
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
A credential created with `octen login --api-key` has no authorization to revoke, so this
|
|
263
|
+
just removes the file, with zero network requests, and the output never mentions revocation
|
|
264
|
+
for that case.
|
|
265
|
+
|
|
266
|
+
Options: `--local`.
|
|
267
|
+
|
|
268
|
+
---
|
|
269
|
+
|
|
270
|
+
### `octen whoami`
|
|
271
|
+
|
|
272
|
+
Show the locally stored credential: account, credential source, and the `grantId` you can
|
|
273
|
+
use to find and revoke this device in the dashboard's authorization list. This reads only
|
|
274
|
+
`~/.octen/credentials.json` — it makes **zero network requests** and does not verify that
|
|
275
|
+
the key still works (run `octen search` for that; there's deliberately no `--verify` flag).
|
|
276
|
+
|
|
277
|
+
```sh
|
|
278
|
+
octen whoami
|
|
279
|
+
octen whoami --json
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
Exits non-zero when not logged in.
|
|
283
|
+
|
|
284
|
+
---
|
|
285
|
+
|
|
56
286
|
### `octen search`
|
|
57
287
|
|
|
58
288
|
Search the live web.
|
|
@@ -307,7 +537,8 @@ Run without flags to show installed skills per client.
|
|
|
307
537
|
|
|
308
538
|
### `octen reset`
|
|
309
539
|
|
|
310
|
-
Remove the Octen MCP server and/or skills from AI clients
|
|
540
|
+
Remove the Octen MCP server and/or skills from AI clients, or clear the stored login
|
|
541
|
+
credential.
|
|
311
542
|
|
|
312
543
|
```sh
|
|
313
544
|
# Remove everything from all clients
|
|
@@ -318,9 +549,24 @@ octen reset --mcp
|
|
|
318
549
|
|
|
319
550
|
# Remove only skills from a specific client
|
|
320
551
|
octen reset --skills --claude-code
|
|
552
|
+
|
|
553
|
+
# Clear ~/.octen/credentials.json (like octen logout --local, but see the note below)
|
|
554
|
+
octen reset --credentials
|
|
321
555
|
```
|
|
322
556
|
|
|
323
|
-
Options: `--all` (both surfaces, all clients), `--mcp`, `--skills`, plus per-client flags: `--claude-code`, `--cursor`, `--claude-desktop`, `--windsurf`, `--vscode`, `--codex`, `--openclaw`, `--hermes`, `--scope` (user|project).
|
|
557
|
+
Options: `--all` (both surfaces, all clients), `--mcp`, `--skills`, `--credentials`, plus per-client flags: `--claude-code`, `--cursor`, `--claude-desktop`, `--windsurf`, `--vscode`, `--codex`, `--openclaw`, `--hermes`, `--scope` (user|project).
|
|
558
|
+
|
|
559
|
+
`--credentials` is **not** included in `--all` — `--all`'s existing meaning is "both
|
|
560
|
+
surfaces (MCP + skills) across all clients", and folding in the login credential would mean
|
|
561
|
+
`--all` silently logs you out, which would be an unwelcome surprise. Reach for `octen
|
|
562
|
+
logout` instead of `reset --credentials` when there's a remote authorization to revoke too;
|
|
563
|
+
`reset --credentials` only ever touches the local file, with no network request.
|
|
564
|
+
|
|
565
|
+
It behaves like `octen logout --local` — same file, same zero network requests — but it is
|
|
566
|
+
not the same command: `reset --credentials` reports removal in `reset`'s own style, and
|
|
567
|
+
`logout` is the command whose whole job is the authorization. Both print the `grantId` of a
|
|
568
|
+
`source: login` credential before destroying it, so the authorization left listed in the
|
|
569
|
+
dashboard can still be identified and revoked there.
|
|
324
570
|
|
|
325
571
|
---
|
|
326
572
|
|
package/dist/api/errors.d.ts
CHANGED
|
@@ -2,6 +2,21 @@ export declare class OctenError extends Error {
|
|
|
2
2
|
}
|
|
3
3
|
export declare class OctenAuthError extends OctenError {
|
|
4
4
|
}
|
|
5
|
+
/**
|
|
6
|
+
* There is no credential at all — no flag, no env var, no credentials file.
|
|
7
|
+
*
|
|
8
|
+
* A subclass rather than a flag so that every existing `instanceof
|
|
9
|
+
* OctenAuthError` keeps matching, while the two callers that must tell "you
|
|
10
|
+
* have nothing" apart from "you have something that doesn't apply here"
|
|
11
|
+
* (`configure-mcp`, `configure-skills --set-key`) can do it without matching on
|
|
12
|
+
* message text. Both used to treat any OctenAuthError as "no credential", so a
|
|
13
|
+
* working credential plus an OCTEN_AUTH_ISSUER override produced "no API key
|
|
14
|
+
* found", a `${OCTEN_API_KEY}` placeholder config, exit 0 — and, in
|
|
15
|
+
* configure-skills, advice to run `octen login`, which cannot fix an
|
|
16
|
+
* OCTEN_AUTH_* override.
|
|
17
|
+
*/
|
|
18
|
+
export declare class OctenNoCredentialError extends OctenAuthError {
|
|
19
|
+
}
|
|
5
20
|
export declare class OctenValidationError extends OctenError {
|
|
6
21
|
}
|
|
7
22
|
export declare class OctenTimeoutError extends OctenError {
|
package/dist/api/errors.js
CHANGED
|
@@ -2,6 +2,21 @@ export class OctenError extends Error {
|
|
|
2
2
|
}
|
|
3
3
|
export class OctenAuthError extends OctenError {
|
|
4
4
|
}
|
|
5
|
+
/**
|
|
6
|
+
* There is no credential at all — no flag, no env var, no credentials file.
|
|
7
|
+
*
|
|
8
|
+
* A subclass rather than a flag so that every existing `instanceof
|
|
9
|
+
* OctenAuthError` keeps matching, while the two callers that must tell "you
|
|
10
|
+
* have nothing" apart from "you have something that doesn't apply here"
|
|
11
|
+
* (`configure-mcp`, `configure-skills --set-key`) can do it without matching on
|
|
12
|
+
* message text. Both used to treat any OctenAuthError as "no credential", so a
|
|
13
|
+
* working credential plus an OCTEN_AUTH_ISSUER override produced "no API key
|
|
14
|
+
* found", a `${OCTEN_API_KEY}` placeholder config, exit 0 — and, in
|
|
15
|
+
* configure-skills, advice to run `octen login`, which cannot fix an
|
|
16
|
+
* OCTEN_AUTH_* override.
|
|
17
|
+
*/
|
|
18
|
+
export class OctenNoCredentialError extends OctenAuthError {
|
|
19
|
+
}
|
|
5
20
|
export class OctenValidationError extends OctenError {
|
|
6
21
|
}
|
|
7
22
|
export class OctenTimeoutError extends OctenError {
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The pre-registered public client id. This is not a value the CLI chooses:
|
|
3
|
+
* it must byte-match the client row seeded on the authorization server, and a
|
|
4
|
+
* mismatch fails at the authorize step with `invalid_client`, before the user
|
|
5
|
+
* ever sees a consent screen. There is no dynamic client registration, so
|
|
6
|
+
* nothing negotiates this at runtime — it is a cross-repository constant.
|
|
7
|
+
*/
|
|
8
|
+
export declare const CLI_CLIENT_ID = "octen-cli";
|
|
9
|
+
export declare const CLI_SCOPE = "octen:api_key";
|
|
10
|
+
export declare const REQUEST_TIMEOUT_MS = 10000;
|
|
11
|
+
export declare const LOOPBACK_TIMEOUT_MS: number;
|
|
12
|
+
/**
|
|
13
|
+
* Every origin that this CLI will send a credential to must be https, with the
|
|
14
|
+
* single exception of a loopback literal (a locally running authorization
|
|
15
|
+
* server, which is what the OCTEN_AUTH_ISSUER doc below invites).
|
|
16
|
+
*
|
|
17
|
+
* This is load-bearing, not hygiene. Two requests carry secrets to whatever
|
|
18
|
+
* this string names: `exchange.ts` sends the access token to `/api/oauth/cli/key`,
|
|
19
|
+
* and `revoke.ts` sends the account-wide, long-lived API key to
|
|
20
|
+
* `/api/oauth/cli/revoke`. Before this check existed, `OCTEN_AUTH_ISSUER=http://…`
|
|
21
|
+
* put both on the wire in cleartext with no warning, and `store.ts` accepted any
|
|
22
|
+
* non-empty string as a *stored* issuer, so a credentials file could name an
|
|
23
|
+
* arbitrary plaintext host and `login`/`logout` would mail the key to it.
|
|
24
|
+
*
|
|
25
|
+
* Exported because store.ts must apply the identical rule to the issuer it reads
|
|
26
|
+
* back off disk — one rule, one implementation, or the two drift.
|
|
27
|
+
*/
|
|
28
|
+
export declare function assertSecureOrigin(name: string, value: string): string;
|
|
29
|
+
/**
|
|
30
|
+
* OCTEN_AUTH_ISSUER, default https://auth.octen.ai.
|
|
31
|
+
*
|
|
32
|
+
* Note that the pre-production and production environments share this one
|
|
33
|
+
* auth host — there is no `auth.pre.octen.ai`, and inventing one by analogy
|
|
34
|
+
* with the other per-environment hostnames will produce a DNS failure rather
|
|
35
|
+
* than a pre-prod login. Point this at a locally running authorization server
|
|
36
|
+
* for development instead.
|
|
37
|
+
*/
|
|
38
|
+
export declare function authIssuer(env: NodeJS.ProcessEnv): string;
|
|
39
|
+
/**
|
|
40
|
+
* OCTEN_AUTH_RESOURCE, default https://cli.octen.ai.
|
|
41
|
+
*
|
|
42
|
+
* Unlike the issuer this is never dialled — it is an audience identifier that
|
|
43
|
+
* goes into the authorize request and is compared byte-for-byte on the server.
|
|
44
|
+
* So it gets the trailing-slash rule (a mismatch there is a silent auth
|
|
45
|
+
* failure) but not the https rule: nothing is ever sent *to* it.
|
|
46
|
+
*/
|
|
47
|
+
export declare function authResource(env: NodeJS.ProcessEnv): string;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { OctenValidationError } from "../api/errors.js";
|
|
2
|
+
/**
|
|
3
|
+
* The pre-registered public client id. This is not a value the CLI chooses:
|
|
4
|
+
* it must byte-match the client row seeded on the authorization server, and a
|
|
5
|
+
* mismatch fails at the authorize step with `invalid_client`, before the user
|
|
6
|
+
* ever sees a consent screen. There is no dynamic client registration, so
|
|
7
|
+
* nothing negotiates this at runtime — it is a cross-repository constant.
|
|
8
|
+
*/
|
|
9
|
+
export const CLI_CLIENT_ID = "octen-cli";
|
|
10
|
+
export const CLI_SCOPE = "octen:api_key";
|
|
11
|
+
export const REQUEST_TIMEOUT_MS = 10_000;
|
|
12
|
+
export const LOOPBACK_TIMEOUT_MS = 5 * 60 * 1000;
|
|
13
|
+
const DEFAULT_ISSUER = "https://auth.octen.ai";
|
|
14
|
+
const DEFAULT_RESOURCE = "https://cli.octen.ai";
|
|
15
|
+
/**
|
|
16
|
+
* Reject a trailing slash rather than trimming it. A sibling project shipped a
|
|
17
|
+
* bug where a trailing slash turned the JWKS URL into `//api/oauth/jwks` and
|
|
18
|
+
* made the issuer comparison fail byte-for-byte — loud failure beats silent
|
|
19
|
+
* correction.
|
|
20
|
+
*/
|
|
21
|
+
function rejectTrailingSlash(name, value) {
|
|
22
|
+
if (value.endsWith("/")) {
|
|
23
|
+
throw new OctenValidationError(`${name} must not have a trailing slash: ${value}`);
|
|
24
|
+
}
|
|
25
|
+
return value;
|
|
26
|
+
}
|
|
27
|
+
/** RFC 8252 §8.3 loopback literals. `localhost` is deliberately absent: it can be
|
|
28
|
+
* redirected via the hosts file, so it earns no plaintext exemption. */
|
|
29
|
+
function isLoopbackHost(hostname) {
|
|
30
|
+
return hostname === "127.0.0.1" || hostname === "::1" || hostname === "[::1]";
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Every origin that this CLI will send a credential to must be https, with the
|
|
34
|
+
* single exception of a loopback literal (a locally running authorization
|
|
35
|
+
* server, which is what the OCTEN_AUTH_ISSUER doc below invites).
|
|
36
|
+
*
|
|
37
|
+
* This is load-bearing, not hygiene. Two requests carry secrets to whatever
|
|
38
|
+
* this string names: `exchange.ts` sends the access token to `/api/oauth/cli/key`,
|
|
39
|
+
* and `revoke.ts` sends the account-wide, long-lived API key to
|
|
40
|
+
* `/api/oauth/cli/revoke`. Before this check existed, `OCTEN_AUTH_ISSUER=http://…`
|
|
41
|
+
* put both on the wire in cleartext with no warning, and `store.ts` accepted any
|
|
42
|
+
* non-empty string as a *stored* issuer, so a credentials file could name an
|
|
43
|
+
* arbitrary plaintext host and `login`/`logout` would mail the key to it.
|
|
44
|
+
*
|
|
45
|
+
* Exported because store.ts must apply the identical rule to the issuer it reads
|
|
46
|
+
* back off disk — one rule, one implementation, or the two drift.
|
|
47
|
+
*/
|
|
48
|
+
export function assertSecureOrigin(name, value) {
|
|
49
|
+
let url;
|
|
50
|
+
try {
|
|
51
|
+
url = new URL(value);
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
throw new OctenValidationError(`${name} must be an absolute URL: ${value}`);
|
|
55
|
+
}
|
|
56
|
+
if (url.username || url.password) {
|
|
57
|
+
throw new OctenValidationError(`${name} must not contain credentials: ${value}`);
|
|
58
|
+
}
|
|
59
|
+
if (url.protocol === "https:")
|
|
60
|
+
return value;
|
|
61
|
+
if (url.protocol === "http:" && isLoopbackHost(url.hostname))
|
|
62
|
+
return value;
|
|
63
|
+
throw new OctenValidationError(`${name} must use https (or http on a 127.0.0.1 loopback address for local development): ${value}`);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* OCTEN_AUTH_ISSUER, default https://auth.octen.ai.
|
|
67
|
+
*
|
|
68
|
+
* Note that the pre-production and production environments share this one
|
|
69
|
+
* auth host — there is no `auth.pre.octen.ai`, and inventing one by analogy
|
|
70
|
+
* with the other per-environment hostnames will produce a DNS failure rather
|
|
71
|
+
* than a pre-prod login. Point this at a locally running authorization server
|
|
72
|
+
* for development instead.
|
|
73
|
+
*/
|
|
74
|
+
export function authIssuer(env) {
|
|
75
|
+
const value = env.OCTEN_AUTH_ISSUER ?? DEFAULT_ISSUER;
|
|
76
|
+
return assertSecureOrigin("OCTEN_AUTH_ISSUER", rejectTrailingSlash("OCTEN_AUTH_ISSUER", value));
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* OCTEN_AUTH_RESOURCE, default https://cli.octen.ai.
|
|
80
|
+
*
|
|
81
|
+
* Unlike the issuer this is never dialled — it is an audience identifier that
|
|
82
|
+
* goes into the authorize request and is compared byte-for-byte on the server.
|
|
83
|
+
* So it gets the trailing-slash rule (a mismatch there is a silent auth
|
|
84
|
+
* failure) but not the https rule: nothing is ever sent *to* it.
|
|
85
|
+
*/
|
|
86
|
+
export function authResource(env) {
|
|
87
|
+
const value = env.OCTEN_AUTH_RESOURCE ?? DEFAULT_RESOURCE;
|
|
88
|
+
return rejectTrailingSlash("OCTEN_AUTH_RESOURCE", value);
|
|
89
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The account's long-lived API key, resolved from an access token via
|
|
3
|
+
* `POST {issuer}/api/oauth/cli/key`.
|
|
4
|
+
*/
|
|
5
|
+
export interface ExchangeResult {
|
|
6
|
+
apiKey: string;
|
|
7
|
+
/**
|
|
8
|
+
* Epoch seconds, or `null` for "does not expire". The server returns null
|
|
9
|
+
* for every credential it mints today — this is a forward-compatibility
|
|
10
|
+
* slot, kept so that if short-lived API keys ever ship, the client already
|
|
11
|
+
* records the deadline instead of needing a storage-format change.
|
|
12
|
+
*/
|
|
13
|
+
expiresAt: number | null;
|
|
14
|
+
/**
|
|
15
|
+
* The id of the OAuth grant behind this credential. Stored on disk and
|
|
16
|
+
* surfaced by `octen whoami`, and it is the only thing `octen logout` can
|
|
17
|
+
* use to name the authorization it wants revoked — no refresh token is ever
|
|
18
|
+
* kept, so this id plus the API key is the whole revocation credential.
|
|
19
|
+
*/
|
|
20
|
+
grantId: string;
|
|
21
|
+
accountId?: string;
|
|
22
|
+
accountType?: string;
|
|
23
|
+
/**
|
|
24
|
+
* Human-readable name for the account — "Octen family", or "Personal" for a
|
|
25
|
+
* user account. Display-only, and deliberately NOT written to disk: the
|
|
26
|
+
* login confirmation line is the only thing that renders it, so a name that
|
|
27
|
+
* drifts after the organization is renamed can never be shown.
|
|
28
|
+
*
|
|
29
|
+
* Optional in both directions. A server older than the field omits it, so
|
|
30
|
+
* does a current server that could not load the name, and prod omits it
|
|
31
|
+
* until this ships there — every caller must fall back to `accountId`.
|
|
32
|
+
*/
|
|
33
|
+
accountName?: string;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Exchange an access token for the account's own long-lived API key at
|
|
37
|
+
* `POST {issuer}/api/oauth/cli/key`.
|
|
38
|
+
*
|
|
39
|
+
* This is the public counterpart of the server's existing internal
|
|
40
|
+
* `/internal/oauth/resolve-key` service call: same authorization facts,
|
|
41
|
+
* different authentication — a Bearer access token instead of a shared
|
|
42
|
+
* service secret. The server reads the grant id out of
|
|
43
|
+
* the token's own session, never from the caller, so this request carries no
|
|
44
|
+
* body at all.
|
|
45
|
+
*
|
|
46
|
+
* Error classification — by transport layer FIRST, then body, mirroring
|
|
47
|
+
* `exchangeCode` in `./oauthClient.ts`. The ordering matters because an
|
|
48
|
+
* `OctenAuthError` is grounds for discarding a credential while an
|
|
49
|
+
* `OctenNetworkError` means "retry later": if a response body were allowed to
|
|
50
|
+
* decide, a struggling server could talk the CLI into throwing away a
|
|
51
|
+
* credential that was never invalid.
|
|
52
|
+
* - a fetch-level failure (DNS, connection reset, our own timeout) is
|
|
53
|
+
* always `OctenNetworkError`.
|
|
54
|
+
* - any 3xx (or an opaque redirect from `redirect: "manual"`) is treated as
|
|
55
|
+
* a transport-layer fault, never followed: a request carrying a bearer
|
|
56
|
+
* token must not be forwarded to an unintended host.
|
|
57
|
+
* - 408 / 429 / 5xx are always `OctenNetworkError`, EVEN IF the body claims
|
|
58
|
+
* `active: false` — a server fault dressed up as a credential problem
|
|
59
|
+
* must never trigger credential deletion downstream.
|
|
60
|
+
* - 401 is always `OctenAuthError`: missing/non-Bearer/unusable token, or
|
|
61
|
+
* (via the 200 path below) a grant that no longer resolves.
|
|
62
|
+
* - 403 is always `OctenAuthError`: audience/scope/client mismatch,
|
|
63
|
+
* independent of the body; retrying cannot help.
|
|
64
|
+
* - a 200 body with `active: false` is `OctenAuthError` too.
|
|
65
|
+
* - a 200 body missing `api_key`, or whose `expires_at` cannot be parsed,
|
|
66
|
+
* is `OctenNetworkError` — a contract violation, never silently absorbed.
|
|
67
|
+
*
|
|
68
|
+
* No error message here ever includes `a.accessToken` or a returned API key.
|
|
69
|
+
*/
|
|
70
|
+
export declare function exchangeForApiKey(a: {
|
|
71
|
+
issuer: string;
|
|
72
|
+
accessToken: string;
|
|
73
|
+
fetchImpl?: typeof fetch;
|
|
74
|
+
}): Promise<ExchangeResult>;
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { OctenAuthError, OctenNetworkError } from "../api/errors.js";
|
|
2
|
+
import { REQUEST_TIMEOUT_MS } from "./constants.js";
|
|
3
|
+
/**
|
|
4
|
+
* Convert the server's ISO-8601 `expires_at` to epoch seconds. `null` stays
|
|
5
|
+
* `null` and means "does not expire" — the only value the server sends today.
|
|
6
|
+
*
|
|
7
|
+
* Anything else that fails to parse returns `undefined`, deliberately
|
|
8
|
+
* distinct from `null`, so the caller can treat it as a contract violation.
|
|
9
|
+
* The alternative — falling back to a locally computed deadline — would
|
|
10
|
+
* invent an expiry the server never stated and expire a working credential.
|
|
11
|
+
*/
|
|
12
|
+
function parseExpiresAt(value) {
|
|
13
|
+
if (value === null)
|
|
14
|
+
return null;
|
|
15
|
+
if (typeof value !== "string")
|
|
16
|
+
return undefined;
|
|
17
|
+
const ms = Date.parse(value);
|
|
18
|
+
if (Number.isNaN(ms))
|
|
19
|
+
return undefined;
|
|
20
|
+
return Math.floor(ms / 1000);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Exchange an access token for the account's own long-lived API key at
|
|
24
|
+
* `POST {issuer}/api/oauth/cli/key`.
|
|
25
|
+
*
|
|
26
|
+
* This is the public counterpart of the server's existing internal
|
|
27
|
+
* `/internal/oauth/resolve-key` service call: same authorization facts,
|
|
28
|
+
* different authentication — a Bearer access token instead of a shared
|
|
29
|
+
* service secret. The server reads the grant id out of
|
|
30
|
+
* the token's own session, never from the caller, so this request carries no
|
|
31
|
+
* body at all.
|
|
32
|
+
*
|
|
33
|
+
* Error classification — by transport layer FIRST, then body, mirroring
|
|
34
|
+
* `exchangeCode` in `./oauthClient.ts`. The ordering matters because an
|
|
35
|
+
* `OctenAuthError` is grounds for discarding a credential while an
|
|
36
|
+
* `OctenNetworkError` means "retry later": if a response body were allowed to
|
|
37
|
+
* decide, a struggling server could talk the CLI into throwing away a
|
|
38
|
+
* credential that was never invalid.
|
|
39
|
+
* - a fetch-level failure (DNS, connection reset, our own timeout) is
|
|
40
|
+
* always `OctenNetworkError`.
|
|
41
|
+
* - any 3xx (or an opaque redirect from `redirect: "manual"`) is treated as
|
|
42
|
+
* a transport-layer fault, never followed: a request carrying a bearer
|
|
43
|
+
* token must not be forwarded to an unintended host.
|
|
44
|
+
* - 408 / 429 / 5xx are always `OctenNetworkError`, EVEN IF the body claims
|
|
45
|
+
* `active: false` — a server fault dressed up as a credential problem
|
|
46
|
+
* must never trigger credential deletion downstream.
|
|
47
|
+
* - 401 is always `OctenAuthError`: missing/non-Bearer/unusable token, or
|
|
48
|
+
* (via the 200 path below) a grant that no longer resolves.
|
|
49
|
+
* - 403 is always `OctenAuthError`: audience/scope/client mismatch,
|
|
50
|
+
* independent of the body; retrying cannot help.
|
|
51
|
+
* - a 200 body with `active: false` is `OctenAuthError` too.
|
|
52
|
+
* - a 200 body missing `api_key`, or whose `expires_at` cannot be parsed,
|
|
53
|
+
* is `OctenNetworkError` — a contract violation, never silently absorbed.
|
|
54
|
+
*
|
|
55
|
+
* No error message here ever includes `a.accessToken` or a returned API key.
|
|
56
|
+
*/
|
|
57
|
+
export async function exchangeForApiKey(a) {
|
|
58
|
+
const f = a.fetchImpl ?? fetch;
|
|
59
|
+
let res;
|
|
60
|
+
try {
|
|
61
|
+
res = await f(`${a.issuer}/api/oauth/cli/key`, {
|
|
62
|
+
method: "POST",
|
|
63
|
+
headers: { Authorization: `Bearer ${a.accessToken}` },
|
|
64
|
+
// This request carries a bearer token — never let a redirect silently
|
|
65
|
+
// forward it to a different host.
|
|
66
|
+
redirect: "manual",
|
|
67
|
+
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
throw new OctenNetworkError("Could not reach the authorization server; please try again.");
|
|
72
|
+
}
|
|
73
|
+
if (res.type === "opaqueredirect" || (res.status >= 300 && res.status < 400)) {
|
|
74
|
+
throw new OctenNetworkError("The authorization server returned an unexpected redirect; please try again.");
|
|
75
|
+
}
|
|
76
|
+
// Transport-layer classification wins over the body, even when the body
|
|
77
|
+
// claims a credential problem: a 5xx/408/429 is a server or infrastructure
|
|
78
|
+
// fault, and nothing about it establishes that the token or key is bad.
|
|
79
|
+
if (res.status === 408 || res.status === 429 || res.status >= 500) {
|
|
80
|
+
throw new OctenNetworkError("The authorization server is unavailable; please retry later.");
|
|
81
|
+
}
|
|
82
|
+
if (res.status === 401) {
|
|
83
|
+
throw new OctenAuthError("Your session is no longer valid. Run `octen login` again.");
|
|
84
|
+
}
|
|
85
|
+
if (res.status === 403) {
|
|
86
|
+
throw new OctenAuthError("Authorization was refused. Run `octen login` again.");
|
|
87
|
+
}
|
|
88
|
+
if (!res.ok) {
|
|
89
|
+
throw new OctenAuthError(`Key exchange failed (HTTP ${res.status}). Run \`octen login\` again.`);
|
|
90
|
+
}
|
|
91
|
+
let payload;
|
|
92
|
+
try {
|
|
93
|
+
payload = await res.json();
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
throw new OctenNetworkError("The authorization server returned an invalid response; please try again.");
|
|
97
|
+
}
|
|
98
|
+
const body = payload !== null && typeof payload === "object" ? payload : {};
|
|
99
|
+
if (body.active === false) {
|
|
100
|
+
throw new OctenAuthError("Your session is no longer valid. Run `octen login` again.");
|
|
101
|
+
}
|
|
102
|
+
const apiKey = body.api_key;
|
|
103
|
+
if (typeof apiKey !== "string" || apiKey.length === 0) {
|
|
104
|
+
throw new OctenNetworkError("The authorization server did not return an API key; please try again.");
|
|
105
|
+
}
|
|
106
|
+
const grantId = body.grant_id;
|
|
107
|
+
if (typeof grantId !== "string" || grantId.length === 0) {
|
|
108
|
+
throw new OctenNetworkError("The authorization server did not return a grant id; please try again.");
|
|
109
|
+
}
|
|
110
|
+
const expiresAt = parseExpiresAt(body.expires_at);
|
|
111
|
+
if (expiresAt === undefined) {
|
|
112
|
+
throw new OctenNetworkError("The authorization server returned an invalid expiry; please try again.");
|
|
113
|
+
}
|
|
114
|
+
const accountId = typeof body.account_id === "string" ? body.account_id : undefined;
|
|
115
|
+
const accountType = typeof body.account_type === "string" ? body.account_type : undefined;
|
|
116
|
+
// Unlike the fields above, a blank name is normalized away rather than
|
|
117
|
+
// passed through: it is only ever interpolated into a sentence, and
|
|
118
|
+
// "Logged in as ." is worse than falling back to the account id. This is a
|
|
119
|
+
// display default, never a contract violation — the server omits the field
|
|
120
|
+
// by design whenever it has no name to give.
|
|
121
|
+
const rawAccountName = typeof body.account_name === "string" ? body.account_name.trim() : "";
|
|
122
|
+
const accountName = rawAccountName.length > 0 ? rawAccountName : undefined;
|
|
123
|
+
return { apiKey, expiresAt, grantId, accountId, accountType, accountName };
|
|
124
|
+
}
|