> ## Documentation Index
> Fetch the complete documentation index at: https://latchkey.getgalaxy.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Latchkey security model

> How Latchkey authenticates its own callers, what each caller may do, how tenants stay isolated, and how it fails closed.

End-user authentication is out of scope, because Latchkey receives a subject
already verified upstream. Latchkey still authenticates whoever calls its own
API. That caller is an Admin, a product service checking on behalf of users,
the directory owner, or a subject asking about itself. Everything fails
closed.

## Caller authentication

A pluggable `identity.Authenticator` turns each incoming request into a
caller, which is a subject and the tenant it belongs to.
Two ship built in, and a deployment can supply its own.

| Authenticator | Verifies | Subject from | Typical callers |
| - | - | - | - |
| OIDC token | Bearer JWT signature, issuer, audience and expiry, against the configured issuer's JWKS | The configured subject claim, `sub` by default. A token is a service token only when its issuer's service rule says so, never because it carries `client_id`. | Admin UI and CLI standalone, users asking about themselves, product services, Management |
| Edge assertion | The caller assertion the Galaxy Cloud edge signs, against the edge's published keys | The platform user id in the assertion | Admin UI and users in Galaxy Cloud |
| Mutual TLS, after V1 | Client certificate against a configured CA | SPIFFE ID or certificate SAN, mapped to a service subject | Product services where a mesh issues certificates |
| Custom | Whatever the deployer implements as `Authenticate(ctx, http.Header) (Caller, error)` | The deployer's choice | Galaxy Cloud, through `ResolveCaller` |

* In V1, product services and Management authenticate with OIDC tokens from
  their platform, not mutual TLS. In Galaxy Cloud that is a projected
  Kubernetes service account token with audience `latchkey`, verified against
  the cluster's OIDC issuer. Each product pod proves its own identity, such as
  `service:system:serviceaccount:<namespace>:<name>`, so network policy is
  never the only check.
* Unauthenticated calls are rejected, except health checks and the SCIM
  endpoint that comes after V1 with its own bearer token.
* A caller acting inside a tenant, such as an Admin or a user asking about
  itself, must be an active subject of that tenant. That is a known subject
  or a user signing in for the first time, whom checks treat as known. A
  deactivated subject is refused even while its token is still valid. An
  Admin must also be known.
* Whether a caller is the tenant's Admin is read from the database in the
  same transaction as each management request, and before explaining
  another subject's access, never from a replica's snapshot. A revoked Admin
  is refused from the next request on, on every replica. Instance-level
  callers, which are domain owners, the directory owner and operators, are
  identified by their authenticated service identity and are not tenant
  subjects. Each must be a service, and a caller from inside a tenant never
  holds their rights.
* With `LATCHKEY_OIDC_SERVICE_RULE` set, an app token from the users'
  identity provider that carries no tenant is an instance-level service. An
  app registered there whose subject equals a configured owner, operator or
  directory owner holds that identity's rights, so treat app registration in
  that provider as privileged.
* The subject id the authenticator produces must match the id directory sync
  stores. A mismatched IdP mapping shows up as surprising denies, and
  `ExplainAccess` reports the subject as unknown.
* In embedded mode the interceptor calls in process, so no authenticator runs
  on the hot path. The product's exposed management API still uses one.

## Who may do what

| Caller | May |
| - | - |
| Tenant Admin | Manage roles, bindings, manual groups and subjects in the tenant, explain access for any subject in it, and read its audit log |
| Any tenant subject | Check and explain its own access |
| Domain owner, from `LATCHKEY_DOMAINS`, or in embedded mode the product itself, in process only | Register its own kinds, purge bindings on its own kinds, provision tenants with `EnsureTenant`, and check any subject with `Check` and `CheckBatch` |
| Directory owner, from `LATCHKEY_DIRECTORY_OWNER` | Call `DirectoryService`, which accepts no other caller. Unset, nobody may call it. See [Directory sync](https://latchkey.getgalaxy.io/pages/guides/usage/directory-sync.md). |
| Operator, from `LATCHKEY_OPERATORS` | Retire and restore kinds, create tenants by hand, bind an Admin to a tenant that has none with `EnsureTenant`, and read the instance-level audit log. A tenant Admin never writes domains or kinds, because every tenant on the instance shares them. |

**Domains are claimed by configuration, not by whoever registers first.**
A domain is the namespace a product registers its kinds under, such as
`filament`. `RegisterDomain` for an unclaimed name, or from anyone but its
owner, returns `PERMISSION_DENIED`. Domain names match `^[a-z][a-z0-9_]*$`,
kind names are exactly `<domain>.<type>` under the same rule, and the domain
name `latchkey` is reserved. Owners are service identities as the
authenticator reports them, not tenant subjects, so the domains table has no
foreign key to subjects.

**Configuration is the only source of owners.** There is no ownership RPC.
When it starts, Latchkey writes the owners in `LATCHKEY_DOMAINS` to the
domains table. A changed owner bumps the instance revision and is audited
with the actor `config`. To move a domain to a new identity, change the
variable and redeploy. A domain removed from the variable can no longer
register, but its kinds and bindings keep working until an operator retires
the kinds. During a rolling deploy, old and new replicas briefly accept
different owners. That is harmless for registration, which only adds and is
ordered by version. Purging bindings cannot be undone, so a purge also checks
the owner the domains table records, in the same transaction, as well as the
answering replica's own configuration. Every replica records its owners when
it starts, so once a new replica has started, the former owner can purge
nothing on any replica, until a replica with the old configuration starts
again, as in a rollback. Finish a rollout before relying on the move, and
roll back an owner change with a new deploy rather than old pods.

### Domain authority covers the whole install

A domain owner's rights are not limited to particular tenants. On an
instance with several tenants, a product can check any subject in any of them
and provision new ones. This is safe when every product on an instance serves
all of its tenants. That holds for Galaxy Cloud, which runs one tenant per
instance, for installs Galaxy runs for a customer, which hold one company
each, and for a self-hosted SaaS whose products are its own services.

> [!WARNING]
>
> Do not put a product that serves only some tenants on a shared instance. Run
> it against its own instance instead.

## Tenant isolation

Tenant isolation holds in the schema, not only in handlers. Group members
carry the group's tenant through a composite foreign key. A binding's custom
role must belong to the binding's tenant, which is checked on write and again
when the snapshot loads. A binding's member must already exist, either synced
from the directory or added through `SubjectService.AddSubject`. That catches
a mistyped IdP id at write time instead of as a silent deny.

## Database boundary

Product code cannot read or write Latchkey's tables. They live in their own
schema behind their own roles, runtime code holds no DDL rights, and the
audit log only accepts inserts, partitions included. The one crossing,
`purge_resource()`, refuses a kind that does not belong to the calling
product's database role, the one the product names with
`embedded.WithDBRole`. See
[Deployment](https://latchkey.getgalaxy.io/pages/guides/deployment/overview.md#database-boundary) for the
roles.

`NOTIFY` is only a hint. Postgres has no privileges on notification channels,
so any role could send one on the `latchkey` channel. Replicas treat a
notification as a prompt to read the access revision, which only Latchkey
roles can write, and reload from the tables only if it has moved. A forged
notification costs at most one revision read.

## Failing closed

* Deny is the default, and only a matching binding allows.
* Errors, timeouts and an unreachable service all deny. Misconfiguration
  errors are counted separately in metrics.
* An unannotated RPC stops the product from starting, and anything the
  startup check missed is denied at request time.
* A replica that cannot confirm a tenant's revision within
  `LATCHKEY_MAX_STALENESS`, 5 minutes by default, answers `UNAVAILABLE` for
  that tenant. A longer setting keeps revoked access honored for longer
  during a database outage. See
  [Configuration](https://latchkey.getgalaxy.io/pages/guides/deployment/configuration.md).
* `LATCHKEY_MODE` must be explicit once an identity provider is configured. A
  typo stops the product from starting instead of turning enforcement off.

## Impersonation

When staff act as a subject, the check carries an `actor`. Until
capabilities are designed, a check with an actor allows only `read`. A caller
acting as someone else may make read calls only: every write is denied,
whether it manages a tenant, registers a domain, provisions a tenant or
purges a resource's bindings, and their checks never save a user seen for
the first time. Management reads are allowed. The audit log records the
staff member.

## Admin UI

The UI holds no authorization logic. Write-time validation, the guard against
removing the last Admin and the read-only rules for directory records all
come back from the server as errors. A management write that would leave a
tenant with no active Admin is refused with `FAILED_PRECONDITION`, whether it
deletes a binding, deactivates a subject or removes one from a group. A
tenant that never had an Admin can still be written, so a product can purge
its bindings. Directory sync is not held to the guard, since the directory
is the source of truth. So the directory can leave a tenant with no Admin.
Bind Admin to a directory group, so the directory can always name the next
Admin. Otherwise only an operator can appoint one, by calling `EnsureTenant`
with `first_admin` for a tenant that has no Admin. A domain owner's call never
does, and a tenant that has an Admin is left alone.

The UI signs in as a public client with PKCE, so it holds no client secret.
It keeps its token in memory and sends it as a bearer header. The one-time
PKCE verifier and state are all it stores, so they outlive the redirect to
the identity provider. It sets no cookies, so a cross-site request carries no
credentials. Management paths send no CORS headers. The server serves the
UI's files without authentication, since they hold no data, and every call
the UI makes is authenticated.

The page ships a strict Content Security Policy. It allows the UI's own
scripts, styles and API calls, the identity provider's origins for sign-in,
the design system's fonts, https images for the logos products register and
the UI's own inlined images, and no other inline code or origin. The
identity provider's origins are the issuer's and those of the token, userinfo
and revocation endpoints its discovery document names, read when the server
starts. Every page sends no referrer, so a logo's host never learns the
page's URL.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.