# API authentication

Source: https://nodedocs.mor.org/reference/api-auth

## Agent Instructions

- Non-browser fetches of page URLs on this site return clean Markdown (not the JS UI). Prefer `https://nodedocs.mor.org/llms-full.txt` for the full corpus, or `https://nodedocs.mor.org/llms.txt` for the index.
- Per-page Markdown is also at `<page-url>.md` (homepage: `https://nodedocs.mor.org/index.md`).
- Docs search MCP: `https://nodedocs.mor.org/mcp` (discovery: `https://nodedocs.mor.org/.well-known/mcp`).
- Never invent contract addresses, chain IDs, token addresses, or live bid/model counts. Cite Networks and tokens; link active.mor.org for live data.
- Never claim Morpheus runs inference — independent providers do. Opening a session escrows MOR; it does not spend it.

API access requires authentication and network access. This was introduced in main release `2.0.0` (and dev/test `1.6.1`) to:
1. Prevent unauthorized access to the API and the wallet in case of network or firewall mis-configuration.
2. Enable governed access for AI agents — agents should have **scoped** access to the API, not full admin.

## Files involved

| File | Default location | Purpose |
|------|------------------|---------|
| `.cookie` | proxy-router binary directory | Admin username/password |
| `proxy.conf` | proxy-router binary directory | All users + per-user method whitelists |

Override paths via env vars:

| Var | Effect |
|-----|--------|
| `COOKIE_FILE_PATH` | Where to read/write the `.cookie` file |
| `AUTH_CONFIG_FILE_PATH` | Where to read/write `proxy.conf` |

## Cookie file

When the cookie file does not exist, the proxy-router auto-generates one:

```
admin:JJLRNze08ZN3vlNdgwgbrh6c4dRw9gQT
```

- `admin` is the administrator username.
- The trailing string is a randomly generated password.

## Proxy configuration file

`proxy.conf` stores user credentials (`rpcauth=`) and permission whitelists (`rpcwhitelist=`):

```
rpcauth=admin:e13576ba0e96bd69f71317c75a06c6f8$cc56ee41055c65b184a34aa5e953d2d069626ce061dd56e22337d2e73804c35c
rpcwhitelist=admin:*
rpcwhitelistdefault=0
```

| Line | Meaning |
|------|---------|
| `rpcauth=<user>:<salt>$<hash>` | Username with salted, hashed password |
| `rpcwhitelist=<user>:<methods>` | Allowed methods. `*` = all permitted |
| `rpcwhitelistdefault=0` | `0` = only whitelisted methods allowed; `1` = all allowed unless restricted |

### Adding a scoped user

```
rpcauth=admin:e13576ba0e96bd69f71317c75a06c6f8$cc56ee41055c65b184a34aa5e953d2d069626ce061dd56e22337d2e73804c35c
rpcauth=agent:ad7a18621d37167502f29712ffc5f324$c056e5f7aa94f6e48c88c81973dc280d16436c1f7bc3c8bded090ae8ea8fc121
rpcwhitelist=agent:get_balance
rpcwhitelist=admin:*
rpcwhitelistdefault=0
```

The `agent` user can only call `get_balance`. The `admin` user retains full access.

## HTTP endpoints to manage users

Both endpoints require **Basic Auth** (administrator credentials).

### Add or update a user

`POST /auth/users` — `application/json`

```http
POST /auth/users
Authorization: Basic YWRtaW46SkpMUk56ZTA4Wk4zdmxOZGd3Z2JyaDZjNGRSdzlnUVQ=
Content-Type: application/json

{
  "username": "agent",
  "password": "agentPassword",
  "methods": ["get_balance"]
}
```

### Remove a user

`DELETE /auth/users` — `application/json`

```http
DELETE /auth/users
Authorization: Basic YWRtaW46SkpMUk56ZTA4Wk4zdmxOZGd3Z2JyaDZjNGRSdzlnUVQ=
Content-Type: application/json

{
  "username": "agent"
}
```

## Authorization header

All endpoints require:

```
Authorization: Basic <base64(username:password)>
```

For example, `YWRtaW46SkpMUk56ZTA4Wk4zdmxOZGd3Z2JyaDZjNGRSdzlnUVQ=` decodes to `admin:JJLRNze08ZN3vlNdgwgbrh6c4dRw9gQT`.

## Permission methods

These method names are recognized in `rpcwhitelist=` entries (e.g. `rpcwhitelist=agent:get_balance,get_transactions`). Set `rpcwhitelistdefault=1` to allow them by default unless restricted.

```
get_balance
get_transactions
get_allowance
get_latest_block
get_stakes_on_hold
withdraw_user_stakes
approve
send_eth
send_mor
get_providers
create_provider
delete_provider
get_models
create_model
delete_model
create_bid
get_bids
delete_bids
get_sessions
session_provider_claim
open_session
close_session
get_budget
get_supply
get_wallet
set_wallet
remove_wallet
system_config
model_health_refresh
add_user
remove_user
agent_requests
request_allowance
initiate_session
chat
embeddings
decisions
audio_transcription
audio_speech
video_jobs
get_local_models
get_local_agents
get_agent_tools
call_agent_tool
get_chat_history
edit_chat_history
ipfs_version
ipfs_add
ipfs_get
ipfs_pin
ipfs_unpin
ipfs_pinned
docker_version
docker_build
docker_manage
```

Notes on a few of these:

- `get_bids` also gates `GET /blockchain/models/prices` and `GET /blockchain/models/:id/session/estimate`; `get_sessions` gates `GET /blockchain/sessions/duration`.
- `set_wallet` covers `POST /wallet/privateKey`, `POST /wallet/mnemonic`, and `POST /wallet/derivationPath` (HD account switching). `get_wallet` is `GET /wallet`; `remove_wallet` is `DELETE /wallet`.
- `system_config` covers `GET /config`, `POST /config/ethNode`, and `POST /config/models/reload`.
- `video_jobs` covers `POST /v1/video/quote`, `POST /v1/video/jobs`, `GET /v1/video/jobs` and `GET /v1/video/jobs/:id`. A video client also needs `open_session` and `close_session` for the session its jobs run on, and `get_bids` for the stake estimate.
- `add_user`, `remove_user`, and `agent_requests` are **delegable without becoming admin**. A caller whose whitelist is not `*` may only create, update, confirm, decline, or remove users whose permissions are a subset of its own. It can never grant `*`, never touch `admin`, never touch a user that carries a spending allowance, never approve an agent request that asks for one, and never re-confirm an already confirmed agent. Such requests return `403`. `admin` cannot be removed through the API by anyone (that would lock the node until `proxy.conf` is edited by hand).
- Usernames may not contain whitespace, `:` or `$`, and permission names may not contain whitespace, `,` or `:`, because `proxy.conf` is line-oriented. Such requests return `400`.
- `ipfs_get` writes the downloaded file to the caller-supplied `dest`, and `ipfs_add` reads the caller-supplied `filePath`. A caller with `*` may use any absolute path (that is how the desktop app's "pick a folder" flow works). Any other caller is confined to the directories in `IPFS_ALLOWED_DIRS` and refused with `403` when that variable is unset. Downloads never overwrite an existing file.

## Agent spending allowances

Beyond method whitelists, an **agent user** can carry per-token spending allowances — a cap on how much ETH or MOR the proxy-router will move on that user's behalf. Allowances are separate from the on-chain ERC-20 allowance to the Diamond contract; they live in the node's local store.

| Endpoint | Permission | Purpose |
|----------|------------|---------|
| `POST /auth/users/request` | *(none — the agent calls it)* | Agent asks for a user with `perms` and `allowances` (`{"<token>": "<wei>"}`, token is `eth` or the MOR contract address) |
| `GET /auth/users` · `POST /auth/users/confirm` | `agent_requests` | Admin lists and approves / declines pending agent users. A caller without `*` may only approve requests whose perms it holds itself and that carry no allowance |
| `POST /auth/allowance/requests` | `request_allowance` | Agent asks for a top-up for its own username (another username needs `*`) |
| `GET /auth/allowance/requests` | `agent_requests` | List pending top-ups |
| `POST /auth/allowance/confirm` · `POST /auth/allowance/revoke` | `agent_requests` **and** `*` | Approve, decline, or revoke a cap. Allowances are money, so only a full-access user changes them |
| `GET /auth/users/:username/txs` | `agent_requests` | Transactions the node sent for that agent |

The allowance is consumed by `POST /blockchain/send/eth`, `POST /blockchain/send/mor`, and session opens (the stake). It is spendable only once the agent is **confirmed**; the amount in a pending request has no effect. The `admin` user is never subject to allowances.

**When the cap is debited.** The amount is held from the allowance **before** the transaction is broadcast and released only if the transaction was provably never sent (a pre-broadcast build or signing failure, or a session open whose attempts all failed without a pending transaction). A transaction that was broadcast but **timed out** waiting to be mined **keeps the debit**, because it may still mine. Earlier releases debited only after a successful mine, which let a retry after a timeout spend past the cap; that is closed. `send/eth` and `send/mor` that revert after broadcast also keep the debit — the cap is conservative by design; an admin re-grants via `POST /auth/allowance/confirm` if that leaves an agent short. Concurrent debits against one agent are serialised inside a single store transaction, so two simultaneous opens cannot both pass a cap that only covers one.

## Recommendations

- For **prosumer / agent** setups, add **per-agent users with restricted whitelists** — never share the admin password with agents.
- For **TEE / SecretVM** deployments, set `COOKIE_CONTENT=admin:<strong-pw>` in your encrypted env so the `.cookie` is seeded from secrets.
- Rotate the admin password regularly. Keep `proxy.conf` out of version control.
