Skip to content

Scoped auth tokens in the CLI - #228

Closed
alexcos20 wants to merge 1 commit into
mainfrom
feature/authtokens_v3
Closed

alexcos20 wants to merge 1 commit into
mainfrom
feature/authtokens_v3

Conversation

@alexcos20

Copy link
Copy Markdown
Member

Scoped auth tokens in the CLI (ocean.js PR #2214)

Brings the scoped auth tokens feature from ocean.js PR #2214
(Feat/scoped auth tokens + OJ-03 issuer verification) to ocean-cli.

Why

In ocean.js v11, ProviderInstance.generateAuthToken is a breaking change: it now mints a
node-scoped, self-expiring v2 token by default and requires an explicit validUntil (epoch
ms). The CLI's current call —

await ProviderInstance.generateAuthToken(this.signer, this.oceanNodeUrl); // no options

— would throw under v11. This PR fixes that (required) and also surfaces the new scoping /
discovery surface to CLI users (optional, additive).

⚠️ Dependency gate (must read)

  • package.json pins @oceanprotocol/lib to 11.0.0-next.2 (published on npm; an exact
    prerelease pin — a ^11.0.0 range would not resolve a -next prerelease).
  • This branch also requires the Aquarius → ProviderInstance migration (ocean.js v11 removed
    the Aquarius class). That migration landed in ocean-cli Support for new HA Database node #215 (merged to rc_3.0.0), so this
    branch must sit on top of rc_3.0.0.
  • Merge order: target rc_3.0.0 (post-Support for new HA Database node #215). When ocean.js cuts the final v11 release, bump the
    pin from 11.0.0-next.2 to it.

Validated against published 11.0.0-next.2

Verified against the real npm-published @oceanprotocol/lib@11.0.0-next.2, stacked on the #215
migration. Procedure: merge rc_3.0.0 into this branch (only CLAUDE.md conflicts — a doc-only
merge; commands.ts/helpers.ts auto-merge), install 11.0.0-next.2, run tsc + the no-infra
suites under Node 24. Results:

  • tsc --noEmit: clean (exit 0) on the whole project against 11.0.0-next.2 with the migration
    in place (AuthTokenOptions, AuthTokenPolicy, UNRESTRICTED_AUDIENCE, decodeJwt,
    listScopableCommands, getNodeAuthTokenPolicy all resolve).
  • 27 tests pass — 20 unit (parseAuthTokenOptions) + 7 CLI-level runRepl (node-free
    listScopableCommands/decodeAuthToken, aliases, unsigned-JWT decode, expired/missing/garbage
    token + REPL survival, node-gated getNodeAuthTokenPolicy refusal).

An earlier build against the pre-release feat/scoped-auth-tokens branch also caught two real issues
a diff-only review missed, now fixed: (a) decodeJwt throws "Error decoding JWT" (and requires a
string address claim) rather than returning null, so decodeAuthToken wraps it in try/catch; (b) a
stale epoch-ms example value that was already in the past (now a clearly-future value in README/tests).

What changed

generateAuthToken (fixed + extended)

Now mints a node-scoped, self-expiring token and exposes the scoping options as flags:

Flag Meaning
--ttl <seconds> Lifetime in seconds (default 3600; the default is capped to the node's maxTokenLifetime)
--valid-until <epochMs> Absolute expiry; overrides --ttl. Rejected if non-integer, ≤0, or in the past (catches the epoch-seconds-vs-ms footgun)
--audience <peerIds> CSV of node peerIds, or any for an unrestricted (reusable, network-wide) token. Omit ⇒ node-scoped (lib defaults to the verified issuer)
--scope <commands> CSV of protocol commands the token may perform. Omit ⇒ any command
--allow-unverified-issuer Skip the OJ-03 node-identity check (insecure; only for pre-OJ-03 nodes)

The CLI supplies a sensible default validUntil (ocean.js deliberately refuses to default it), so the
bare generateAuthToken command keeps working. It reads the node's authTokenPolicy (best-effort)
to clamp only the default TTL to the node cap; an explicit --ttl/--valid-until is passed
through verbatim (the lib/node surfaces a precise cap error). The token prints to stdout; the
expiry/audience/scope summary and all warnings go to stderr, so piping captures only the token.

New commands

Command (alias) Node? Purpose
getNodeAuthTokenPolicy (authTokenPolicy) needs node Print the node's policy (max lifetime, cross-node federation, revocation posture)
listScopableCommands (scopableCommands) node-free List the protocol commands valid in a --scope (static lib catalog)
decodeAuthToken (decodeToken) node-free Decode a JWT locally and print its claims (scope/audience/expiry). Clearly labelled signature NOT verified

Files

  • package.json — lib ^10.1.0 → pinned 11.0.0-next.2 (published prerelease).
  • src/helpers.ts — new pure parseAuthTokenOptions(raw, maxTokenLifetimeSeconds?) →
    AuthTokenOptions, plus the AuthTokenCliOptions type and DEFAULT_AUTH_TOKEN_TTL_SECONDS = 3600.
    Handles validUntil/ttl precedence, default-only clamping, audience/scope CSV parsing, any
    (case-insensitive), and all the reject cases.
  • src/commands.ts — rewrote Commands.generateAuthToken(rawOptions) (best-effort policy read
    → clamp default TTL → warn on unrestricted / --allow-unverified-issuer → mint → summary), added
    private printAuthTokenSummary (stderr) and Commands.getNodeAuthTokenPolicy(). New lib imports:
    AuthTokenOptions, AuthTokenPolicy, UNRESTRICTED_AUDIENCE, decodeJwt.
  • src/cli.ts — added the flags above to generateAuthToken; registered getNodeAuthTokenPolicy,
    listScopableCommands, decodeAuthToken. The two pure commands are added to NODE_FREE_COMMANDS
    and CONFIG_BOOTSTRAP_COMMANDS (names + aliases, so they run with no RPC/signer); all three new
    canonical names are added to the HELP_GROUPS "Tokens & auth" group (required by
    assertHelpGroupsCoverAll). New lib imports: decodeJwt, listScopableCommands.
  • README.md — rewrote the "Auth tokens" section (new flags, breaking-change note, discovery
    commands, stdout/stderr contract, decode≠verify caveat) and the per-command options table.
  • test/authTokenOptions.test.ts (new, no-infra) — 20 unit tests for parseAuthTokenOptions
    (precedence, default-only clamp boundaries, audience/scope parsing + throw branches, blank inputs,
    integer-floor, past/seconds rejection, full-object build).
  • test/authTokenCommands.test.ts (new, no-infra, runRepl) — CLI-level tests: node-free
    listScopableCommands/decodeAuthToken (incl. bootstrap env-free, aliases, unsigned-JWT decode,
    expired, missing/garbage token + REPL survival), node-gated getNodeAuthTokenPolicy refusal
    without a node, and generateAuthToken flag→option wiring validating before any network call.

Security notes

  • Unrestricted audience (--audience any) and --allow-unverified-issuer each print an
    explicit stderr warning at mint time (not just help text), since both are footguns (a reusable
    bearer, and trusting a possibly-spoofed node, respectively).
  • decodeAuthToken labels its output "signature NOT verified; treat these claims as untrusted" and
    guards the validUntil → Date conversion against missing/malformed/out-of-range values.
  • No injection/SSRF/secret-leak vectors: the decode path is a local base64 decode of user-supplied
    input; the token itself is necessarily printed (to stdout) for the user to use.

Support for new HA Database node
@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Caution

Review failed

The pull request is closed.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@alexcos20 alexcos20 closed this Nov 27, 2026
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.

2 participants