Skip to content

fluree login / logout: sign a machine in to a fluree.ai account - #2205

Merged
aaj3f merged 5 commits into
mainfrom
feat/fluree-login
Nov 3, 2026
Merged

aaj3f merged 5 commits into
mainfrom
feat/fluree-login

Conversation

@aaj3f

@aaj3f aaj3f commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

This adds fluree login / fluree logout: signing a machine in to a fluree.ai account, so hosted services can be called from the CLI without a project or a pasted key. The server side lives in fluree.ai, not in this repo. It speaks the same discovery → device grant → token exchange contract that fluree auth login already speaks to Solo stacks (docs/design/auth-contract.md), so most of this is reuse. The difference is where the result lives.

The main design question was whether a fluree.ai account should just be a remote (fluree remote add fluree-ai https://fluree.ai && fluree auth login --remote fluree-ai). I don't think it should. Remotes are Fluree data APIs, so remote add appends /fluree, and query / list / track all assume Fluree routes, which leaves a remote half the commands can't use. A remote also stores its tokens in the nearest .fluree/config.toml, which means a fluree init first and a credential sitting in a project directory. So the account is its own thing:

  • fluree login [--url URL] [--no-browser] reads <url>/.well-known/fluree.json, and requires the new account object (an account service), the device grant, and a refresh token from the exchange. It stores the result in account.toml in the global config dir ($FLUREE_HOME, else the platform dir), written 0600 and replaced atomically. Logging in again signs the previous one out on the server.
  • fluree logout does POST {account api}/auth/disconnect with the access token (refreshing it first if needed), then deletes the file. If the server can't be reached it still removes the local copy and says so.
  • fluree auth status without --remote now shows the sign-in first, then the only remote as before. Outside any project it shows just the sign-in instead of failing on a missing .fluree/. It's still local only and never calls the server. With --remote nothing changes.
  • fluree remote add <url> refuses a URL whose discovery describes an account service, and points at fluree login.
  • account::session() is the piece the next commands will use: an access token with time left, refreshed through the exchange's refresh_token grant and saved. Nothing calls it yet. I deliberately didn't hang it off doc.remote here, because that path fills the LLM slots with model = "auto", which a Solo stack's gateway resolves but fluree.ai's own /v1/responses would treat as a literal model name. That needs its own change.

On the discovery side (auth-contract.md gets an "Account services" section): an account service has service plus an account object (api_base_url, dashboard_url, status_url) and no top-level api_base_url, since that field means "Fluree data API" to every CLI. The device request also sends a non-standard device_name form field (the host name) so the approval page can say which machine is asking. RFC 8628 servers ignore it, and fluree auth login against a remote doesn't send it.

Reused from auth.rs rather than copied: discover_oidc_endpoints, run_device_code_flow (now taking device_name and open_browser), exchange_token (which also keeps the response body, for the account summary), and decode_token_summary. discover_remote learned to read account.

Docs: new docs/cli/login.md and logout.md (README index, SUMMARY.md), plus auth.md, remote.md and the contract section.

Tests:

  • tests/account_login.rs runs the real binary against an in-process account service (axum). It covers login with no project (pending → grant → exchange, device_name sent, 0600 file, no .fluree/ created); auth status; re-login signing the old one out; logout hitting disconnect; remote add refusing an account URL; and a refresh that keeps an unrotated refresh token.
  • account.rs has unit tests for the file round trip and permissions, the missing file, exp parsing, and the hint text.
  • cargo test -p fluree-db-cli and cargo clippy -p fluree-db-cli --all-targets are clean.

I've also run it against the fluree.ai side, deployed to a throwaway sandbox stack. That covered login from an empty directory, auth status, the token accepted by the account's lanes, refresh, logout revoking both the refresh credential and the live token, and a machine disconnected from the dashboard. That run turned up two things, both fixed in the second commit:

  • reqwest sends no User-Agent by default, and the WAF in front of fluree.ai answers 403 to requests without one, so discovery saw "no sign-in here". The sign-in clients now send fluree/<version> (fluree_db_cli::USER_AGENT), and the test server refuses requests without it. The data-plane client in remote_client.rs still sends none; Solo stacks don't sit behind that WAF, so I've left it alone here.
  • logout after a dashboard disconnect claimed the server "did not confirm" the sign-out. It now recognises an already-revoked sign-in.

🤖 Generated with Claude Code

https://claude.ai/code/session_016CjYmNEuWYTradVSiSAz2N

aaj3f and others added 2 commits October 28, 2026 17:37
Sign a machine in to an account service once, without a project, over
the contract `fluree auth login` already uses for remotes: discovery,
the RFC 8628 device grant, and the token exchange.

- An account is not a remote. The sign-in lives in `account.toml` in the
  global config dir (0600, replaced atomically), never in a project's
  config.toml, and `remote add` refuses an account URL with a hint to
  sign in instead.
- Discovery: an `account` object (api_base_url, dashboard_url) and no
  top-level api_base_url mark an account service. Documented as "Account
  services" in docs/design/auth-contract.md.
- `login` needs the device grant and a refresh token. It sends the host
  name as `device_name` for the approval page and has a `--no-browser`
  flag. Logging in again signs the previous sign-in out on the server.
- `logout` POSTs {api}/auth/disconnect, then deletes the file.
- `auth status` without --remote shows the sign-in, and works outside a
  project.
- `account::session()` returns a fresh access token, refreshing it
  through the exchange's refresh_token grant and saving the result.
- Reuses auth.rs's OIDC discovery, device flow and exchange (now
  pub(crate)) rather than copying them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016CjYmNEuWYTradVSiSAz2N
…d sign-in

Found running `fluree login` against a deployed account service:

- reqwest sends no User-Agent by default, and the AWS WAF common rule set
  in front of fluree.ai answers 403 to requests without one. Discovery
  read that as "no sign-in here". The discovery, OIDC-login and login
  clients now send `fluree/<version>` (`fluree_db_cli::USER_AGENT`), and
  the test server refuses requests without it.
- `fluree logout` after the machine was disconnected from the dashboard
  said the server "did not confirm" and asked for a dashboard disconnect
  that had already happened. An `invalid_grant` refresh or an
  `invalid_state` disconnect now counts as already signed out.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016CjYmNEuWYTradVSiSAz2N
@aaj3f
aaj3f marked this pull request as ready for review October 29, 2026 13:15
@aaj3f
aaj3f requested a review from bplatz October 29, 2026 13:15
@bplatz

bplatz commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

A few things, none blocking:

  1. fluree logout can't clear an unreadable account.toml. When the file doesn't parse, load_from says "…is unreadable; run fluree logout and sign in again" (account.rs L110), but run_logout starts with account::load()?, so logout fails with that same message. fluree auth status without --remote fails the same way (auth.rs L84), even when the project has a remote it could still report. Only fluree login recovers, since it ignores the load error. Logout removing an unreadable file (with a note that the server-side sign-in may still be live, so disconnect it from the dashboard), or the hint naming fluree login, would fix the loop.

  2. Concurrent refreshes, for when session() gets callers. Two CLI processes near expiry would both refresh with the same refresh token. That's fine while fluree.ai doesn't rotate it, but with rotation and reuse detection one of them gets invalid_grant ("your sign-in is no longer valid"), and whichever saves last can leave a dead refresh token in the file. A file lock around load → refresh → save, re-reading after the lock is taken, would make it safe. Relatedly, the temp file is named by pid alone (L142), so two saves in one process would share it.

  3. auth status drops the "pick a remote" error when signed in. With no --remote, a sign-in, and several remotes configured, it shows only the account (auth.rs L93, Err(_) if signed_in => Ok(())), and it swallows any other remote-config error the same way. A one-line hint (--remote <name> to see one) would keep that discoverable.

  4. The re-login test doesn't pin which sign-in is revoked. It asserts one disconnect happened (account_login.rs L258) but not that it carried the old access token. The fake records the Authorization header, so asserting disconnects[0] is the first login's token would catch signing out the new sign-in instead.

  5. Labels. The PR has none, so it lands under "Other Changes" in the release notes; enhancement and area:cli look right.

  6. Nits:

    • account::refresh re-implements the grant_type=refresh_token call that remote_client.rs already makes (L508); one helper could serve both.
    • --url accepts http://, so the device grant, exchange and refresh tokens can cross the network in cleartext. Requiring https except for loopback hosts would keep the tests' 127.0.0.1 fake working.

@bplatz bplatz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approving. Keeping the account out of remotes reads right, the credential file handling (0600, atomic replace, no project needed) is careful, and the reuse of the existing device-grant and exchange code is clean.

Some feedback in the comment above — one small bug (logout can't clear an unreadable account.toml), a note for when session() gets callers, and a few smaller items. None blocking.

aaj3f and others added 2 commits November 3, 2026 14:58
… take turns, config_stale renews

- `fluree logout` removes an `account.toml` it can't read (and warns
  the server wasn't told); `auth status` reports it instead of failing,
  and the load error now names `fluree login`.
- A refresh holds `account.toml.lock` and re-reads the file once it has
  it, so two processes near expiry spend the refresh token once (tested
  with two concurrent sessions; red without the lock). Temp files are
  named by pid, a per-process counter and the time.
- `account::send` makes a call with the sign-in and, on 401
  `config_stale` (the account's configuration changed after the token was
  issued), refreshes and sends it once more. `fluree auth token
  --account [--refresh]` prints the sign-in's token, renewing it on
  demand. The contract documents the code.
- `auth status` with a sign-in and several remotes says how to see one;
  other remote-config errors are shown as a warning, not dropped.
- The re-login test pins that the previous sign-in, by its own token, is
  the one signed out.
- One `grant_type=refresh_token` call (`account::post_refresh`) serves
  the account and a remote's refresh.
- `--url` must be https, except to localhost or a loopback address.
- The auth contract's account example names the issuer under
  `/v1/oauth`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016CjYmSfiNVsRZmcpMicDfq
@aaj3f aaj3f added enhancement New feature or request area:cli fluree CLI UX, rdf toolkit, publish/export/insert flows labels Nov 3, 2026
@aaj3f

aaj3f commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review, @bplatz, all six were right. They're in cddff59 (on top of a merge of main), along with one more that came up on the server side.

  1. Unreadable account.toml. fluree logout now removes a file it can't read. It warns that the server wasn't told, and says to disconnect the machine from the account's dashboard if it was signed in. fluree auth status reports the problem under "Account:" instead of failing, so the project's remotes still show. The load error now points at fluree login (which replaces the file) as well as logout. Tested in an_unreadable_sign_in_can_still_be_removed.
  2. Concurrent refreshes. A refresh now holds an exclusive lock on account.toml.lock (std File::lock, taken on a blocking thread) and re-reads the file once it has the lock. If another process refreshed in the meantime, it uses that token instead of spending the refresh token again. two_refreshes_at_once_spend_the_refresh_token_once runs two sessions concurrently against an expired token and expects a single refresh; with the lock removed it goes red (2 refreshes). Temp files are now named by pid, a per-process counter and the time.
  3. auth status with several remotes. It now prints hint: several remotes are configured; fluree auth status --remote <name> shows one. Any other remote-config error shows as a warning rather than being dropped. "No remotes configured" stays quiet, since the account is the status then.
  4. The re-login test now asserts disconnects == ["Bearer <first login's access token>"], and that the two tokens differ.
  5. Labels: added enhancement and area:cli.
  6. Nits:
    • One refresh call: account::post_refresh is now the single grant_type=refresh_token call, used by both account::refresh and RemoteLedgerClient::try_refresh.
    • https only: fluree login --url must be https, except to localhost or a loopback address, so the tests' 127.0.0.1 fake keeps working (login_needs_https_except_to_this_machine).

Also: renewing a token after a configuration change. fluree.ai answers 401 config_stale when the account's configuration changed after a token was issued, a service key being turned on or off say. Before this, a signed-in machine would have stayed broken until the hour ran out. Now:

  • account::send makes a call with the sign-in and, on 401 config_stale (top-level error or error.error), refreshes and sends it once more. A second stale answer is returned as is, with no loop.
  • fluree auth token --account [--refresh] prints the sign-in's access token for scripts (no project needed), and --refresh forces a renewal.

a_config_stale_answer_refreshes_and_sends_once_more and auth_token_account_prints_the_sign_ins_token_and_refresh_forces_one cover both. The auth contract documents the code, and its account example now names the issuer under /v1/oauth, where fluree.ai's sign-in document now lives. The CLI reads the issuer from discovery, so nothing else changed for it.

Gate on the pushed head: cargo clippy -p fluree-db-cli --all-targets clean, cargo test -p fluree-db-cli green (including account_login 9/9 and docs_coverage). CLI reference pages for auth token, login and logout are updated.

@aaj3f
aaj3f merged commit cce6963 into main Nov 3, 2026
19 checks passed
@aaj3f
aaj3f deleted the feat/fluree-login branch November 3, 2026 21:29
bplatz added a commit that referenced this pull request Nov 4, 2026
Brings #2247, #2232, #2246, #2248, #2245, #2230, #2205, #2231, #2255,
#2219, #2209, #2207, #2190, #2216, #2186, #2215, #2213, #2184, #2210,
#2206, #2198, #2187, #2183, #2200, #2193, #2199, #2196 and #2145 into next.

Resolutions:
- Novelty: main's fact-state trim (a cutoff plus a bounded sweep) and the
  dictionary's coverage watermark sit on next's graph slots and per-graph
  OrdMaps. A commit's reification links count toward coverage. Planner
  stats from segment sums decline under annotations, whose link deltas
  only the walk counts, and otherwise carry the indexed link counts.
- Stats-view identity: the index root's id reaches the snapshot through
  next's root decoder (`decode` takes the id, as `take_snapshot_metadata`
  does). `LedgerState::load_record` has a decoding-root form beside
  `load_decoding_root`.
- Transact: #2184's parallel-relationship check runs in next's link
  cascade, counting the `rdf:reifies` links on the retracted edge that the
  transaction does not retract itself. Main's bundle `edge_reifiers` is
  dropped, and `subjects_with_prior_rows` and
  `enforce_single_target_reifiers` stay deleted. The upsert pre-check
  takes main's dictionary probe and bounded seek.
- Query: geo search resolves the graph's POST tree once at open (pooled
  graphs keep their filter), an empty manifest when there is none.
  `branch_or_none` reads the dedicated branch, and the batched joins keep
  next's pooled lanes. The stats-view key hashes the root id.
- Indexer: `apply_dict_novelty` and `compact_forward_streams` take the
  cycle's compaction budget, so the term dictionary keeps its reserved
  share. `Prefetched` holds `ContentBytes`. `relocalize_sorted_commit`
  rewrites next's 20-byte types records.
- Disk cache: main's in-flight write registration, with an empty entry
  still read as crash debris that a write replaces. The process-wide
  eviction walk stays removed.
- Graph import (#2232): fenced CAS and publishes, the seed carries the
  record's namespace and fence, 32-bit graph ids and next's loaders. An
  import into a graph of an existing ledger refuses triple terms and edge
  annotations: the splice merges no triple-term dictionary yet.
- Iceberg: `create_iceberg_graph_source` goes with main (a source needs a
  mapping); the R2RML path keeps next's `model_instance`.
- CLI: main's shared import flags without the leaflet flags; `graph
  import` routes through next's process-wide server routing, and
  `upload_source_import` serves next's `Destination`. Doc sources read a
  ledger through the command's open instance.
- Tests: sibling ledger ids become distinct names, `genesis_ledger` is
  awaited, Cypher state dumps include each relationship's `rdf:reifies`
  link, and the graph-import arena test checks the refusal instead.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:cli fluree CLI UX, rdf toolkit, publish/export/insert flows enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants