# Service authentication and client safety

Start locally without an account. When needed, authenticate a service:

```sh
op-sys auth login --endpoint https://records.example.com
op-sys auth status
op-sys workspaces list
op-sys selection update --workspace <workspace-id>
```

Managed and customer-hosted services use the same operations. Use the service
address supplied by your workspace operator. Browser login advances a resumable flow: sign-in,
workspace discovery, explicit selection when there are several choices, or
creation when the service permits it. Interactive CLI use guides those steps.
SDK/MCP/JSON calls return the next step. Login uploads no records. Successful
selection associates the verified identity. Ledger permissions are granted separately.

On a service configured for Record-issued device credentials, the browser
approval uses the verification code shown by the CLI. SDK and MCP calls report
the approval URL and code while sign-in is pending. Resolve the approval with
the user's browser and resume the operation using the same connection.
The device credential stays in the connection's private storage.

Both [workspace invitations](/docs/raw/manuals/team-onboarding.md) and
[ledger invitations](/docs/raw/manuals/ledger-core.md#invite-a-participant-to-a-ledger)
guide interactive recipients through sign-in as part of acceptance. For a
workspace invitation, structured clients handle the returned
`auth.login` next action with `sign_in_only` before retrying acceptance.

Named connections remember endpoints and isolate private credential stores.
Use `connections.create/list/get/update/remove` to manage them. `selection.update`
chooses the connection, workspace and ledger. Existing clients retain their
pinned identity until explicitly reconfigured. Changing an endpoint requires
fresh authentication; credentials never follow an edited endpoint or redirect.

## Machine authentication

```sh
op-sys auth login --endpoint https://records.example.com \
  --credential-env VEYLAR_API_KEY --credential-scope connection
# Alternatively, use --credential-file /private/path/service-key.
```

The scope is connection, read, ingest or admin and must be supported by the
credential. All interfaces use the same reference inputs. The environment variable
or private file belongs to the client host, not the remote service. A file must be
private, regular and single-link. Credentials are one bounded nonempty line.
Literal key arguments and unverified-login flags are not public capabilities.
MCP never reads its protocol input stream as a credential.

Each machine login replaces that connection's credential set with the explicitly
supplied identity, after checking the service's capability response and principal.
Verification failure or a concurrent edit preserves the previous configuration.
Sign out before changing between browser and machine authentication. Machine
credentials are workspace-bound. Selecting another workspace requires suitable
credentials, usually in another named connection. Use separate connections for
separate machine identities. Credential scope and ledger grants both apply.

Verify a specific ledger after machine sign-in. Use `ledgers inspect --ledger-id
<ledger-id>` to check ledger inspection access or `records limits --ledger-id
<ledger-id>` to check append access without creating a record.

```python
from op_sys import OpSys
client = OpSys()
client.auth.login(endpoint="https://records.example.com",
                  credential_env="VEYLAR_API_KEY", credential_scope="admin")
client.selection.update(ledger="ledger_...")
```

## Sign out versus remove a connection

`auth.logout` revokes a browser session remotely and removes its local credentials
when revocation completes. If revocation is pending, repeat logout; explicit
`local_only=true` discards local retry credentials without claiming remote success.
For machine credentials, logout removes the saved key locally; it does not revoke
that key at the service. Connection configuration and local ledger data remain.

`connections.remove` removes the destination name. It does not delete remote
records or revoke credentials at the service. Log out first when retiring a
connection. `auth.status` shows saved authentication information, never secrets;
Machine status describes the saved configuration. Record device credentials
have a fixed lifetime. Run `auth login` again after expiry.

## Private persistence

Credentials and non-secret configuration are committed in one verified, locked,
revision-checked private generation. Incomplete generations are never selected.
Logout retires prior credential generations. Logical removal is not physical
erasure of independent backups or storage snapshots. Portability never exports
credentials. Local identity and historical record provenance remain unchanged.

Publishing and cloning compose these public operations; see
[connections](/docs/raw/manuals/client-connections.md) and [local to shared](/docs/raw/manuals/ledger-quickstart.md).
Direct HTTP clients use auth discovery to find the sign-in flow, and the
session verification and revocation endpoints to manage credentials. They
keep credentials and connection selection on the client. Workspace discovery
uses `/v1/control-plane/organizations`.
