# Base

Base is the front door for a small business. Your team signs in with their
work email, you invite the people you work with, and you install apps from a
Store. Base runs on a CARLOS Platform account, and each app you install runs
as its own instance on that account.

Base only says who someone is. Each app decides what that person may do.

## Run it on your machine

Copy `.env.local.example` to `.env.local` and fill it in, then:

    ./run.sh

It builds Base and listens on `127.0.0.1:8830` (change it with `BASE_ADDR`,
which must stay on `127.0.0.1`). The database is `base.db` next to the script.

Sign-in assertions only work between `https://` hostnames, and the verifier
rejects IP addresses, so `http://127.0.0.1:8830` is no good as `BASE_ORIGIN`
once you install an app. Share the port and use the origin it prints:

    carlos devbox share --port 8830

Put that origin in `BASE_ORIGIN`, and set `BASE_ALLOW_LOGGED_MAIL=1` only if
the origin is on loopback and you want sign-in links printed to the log
instead of emailed.

## Settings

Everything is an environment variable. `BASE_ORIGIN`, `BASE_OWNER` and
`BASE_INSTANCE_KEY` are required.

- `BASE_ORIGIN` is Base's public address: `https://` and a hostname, no path.
  (`http://localhost` is allowed for local runs.)
- `BASE_OWNER` is the email address of the owner, who can invite everyone else.
- `BASE_INSTANCE_KEY` is a long random secret that seals sign-in state.
  Keep it and don't change it: changing it breaks sign-in links in flight.
  `openssl rand -base64 32` makes a good one.
- `BASE_NAME` is what your team calls Base, such as "Acme" (default `Base`).
- `BASE_SLUG` names the apps' hostnames: `<slug>-<appid>.<BASE_APPS_DOMAIN>`.
  Lowercase letters, digits and `-`, at most 19 characters (default `base`).
- `BASE_TIMEZONE` is where dates are shown, and the time zone Ask sends Equipo so the Assistant writes times in it ("Paused until 21:00"); default `Europe/Dublin`.
- `BASE_ASSERTION_KEY` is Base's signing key. `go run ./tools/keygen` prints
  a fresh one. Without it, no one can open an installed app. Keep it stable
  across restarts: apps check sign-ins against its public half.
- `BASE_ALLOW_LOGGED_MAIL=1` prints sign-in links instead of sending them.
  Local only; Base refuses it on a non-loopback origin.
- `BASE_EQUIPO_ORIGIN` is the Equipo whose Assistant answers "Ask Base
  anything", such as `https://equipo.example.com`. Without it the Ask field
  stays off. Each person connects Equipo once, from Settings or the Ask
  panel; Base keeps their Equipo key sealed with `BASE_INSTANCE_KEY`, so
  changing that key disconnects everyone from Equipo.
  When a person connects, Base tells Equipo its own address (`BASE_ORIGIN`)
  and gives it a token for that person alone. Equipo's Assistant uses the
  token to read Base's apps, people and settings as that person. Connecting
  again replaces the token, and disconnecting withdraws it. Equipo takes the
  address only if its operator lists it in `EQUIPO_ASSISTANT_HOSTS`. Until
  then the chat still works, but the Assistant can't see Base: Base logs
  `host_not_allowed` and tries again the next time the person opens the chat.
- `BASE_OFICINA_APPS` lists your team's Oficina apps, which Home shows first
  in its grid of apps, each opening in a new tab. Write each as
  `id=address`, separated by commas, in the order you want them, such as
  `calendar=https://calendar.oficina.works,fichas=https://fichas.oficina.works`.
  The ids are `calendar`, `docs`, `sheets`, `slides`, `memoria`, `calls`,
  `fichas` and `equipo`, and each address is `https://` and a hostname only.
  Base refuses to start with an id it doesn't know. Without it, Home lists no
  Oficina apps.

To let your team sign in with Oficina ID, set all four of these. Without them
Base has only its own sign-in. With some but not all, it refuses to start.
Oficina ID needs `https://` in `BASE_ORIGIN`.

- `BASE_HOME_ISSUER` is Oficina ID's address, such as `https://id.oficina.works`.
- `BASE_HOME_CONSUMER_ID` is the id Oficina ID lists Base under, with Base's
  origin.
- `BASE_HOME_KEYS_BASE64` is Oficina ID's public keys: base64 of a JSON object
  that maps each key id to the base64 of that public key.
- `BASE_HOME_DEK_BASE64` is base64 of 32 random bytes that seal Oficina
  sign-ins in flight and the links between accounts. Give Base a key of its
  own and keep it: changing it breaks every connected account.
  `openssl rand -base64 32` makes a good one.
- `HOME_DIRECTORY_ORIGIN` is optional: the address of the Oficina directory.
  Base checks it but doesn't link to it yet.

With Oficina ID on, the sign-in page leads with "Sign in with Oficina", and
email and passkeys stay below it. Everyone signs in with their email once and
connects their Oficina account from Settings; after that, Oficina signs them
in. Only members get in, and inviting people is still by email.

Each connection is tied to `BASE_ORIGIN`. If you move Base to a new address,
Oficina ID stops signing people in until they connect again: each person
signs in with their email once and connects their Oficina account, from
Settings or straight from "Sign in with Oficina". The old connection is
cleared then. Nothing else is needed.

Base's Mac and iPhone app signs in with Oficina ID too, through
`POST /native/oficina-id`; its session lasts seven days, and the app renews
it. Someone must have signed in to Base on the web with Oficina ID once
before the app lets them in. The app reads and calls what
`docs/api/base-native-v1.md` describes.

The app connects Ask Base with Oficina ID, so there's no code to approve in
Equipo. Base signs the person in to Equipo with Oficina ID on their behalf
and keeps the Equipo key sealed, as it does one from the code. This works
only for someone Equipo has already linked to their Oficina ID (they've
signed in to Equipo with it); for anyone else the app shows that Ask isn't
set up for them yet, and they can connect it with a code from Base on the
web. A key made this way lasts seven days, not thirty: it ends with the
Equipo session it was made with, or sooner if the person signs out of
Equipo or leaves the team there. The app makes a new one after five days,
without asking. Base never signs that Equipo session out, because doing so
would end the key.

To install apps, Base also needs to reach the Platform. Without these five and
`BASE_ASSERTION_KEY`, it still runs and logs that installs are off.

- `BASE_PLATFORM_CONSOLE` is the console's address.
- `BASE_PLATFORM_ACCOUNT` is the account Base runs on.
- `BASE_PLATFORM_INSTANCE_TOKEN` is a service credential with the `instance`
  role.
- `BASE_PLATFORM_OPERATE_TOKEN` is a second one with the `operate` role.
  A credential holds one role, so you need two. Both are account-wide:
  create them without `--app`.
- `BASE_APPS_DOMAIN` is the domain apps live under. Base checks at start-up
  that it and `BASE_ORIGIN` make addresses an app can accept sign-ins from:
  a hostname, not an IP address, and no `:443`.

Base installs Welcome when it starts. If the owner uninstalls Welcome, Base
remembers and leaves it off; installing it from the Store brings it back.
An installed app keeps the address it was installed at, even if you change
`BASE_SLUG` or `BASE_APPS_DOMAIN` later.

Outside loopback, Base sends mail through `CARLOS_SMTP_HOST` and
`CARLOS_SMTP_FROM`.

## Deploy

Build the binaries, then deploy Base to a channel:

    make release
    carlos deploy --app base --channel edge releases/base-linux-arm64-<version>

`make release` refuses to build from a dirty tree. It fetches Timeless Sans
if it isn't there yet (this needs the network), and refuses to build unless
the font is exactly the pinned file. It builds `base`,
`scratchpad` and `welcome` for linux/arm64 into `releases/`, named with the
commit. Set the `BASE_*` settings with `carlos env set --app base KEY=value`.

## Before anyone can install an app

This is a one-time job for whoever runs the account, and you repeat it when
you add an app to the catalogue. The Platform needs each app to exist, have a
release, and have instances enabled before Base is allowed to create one for a
team:

    scripts/prepare-apps.sh <account> releases [<version>]

Pass the version (the commit in the binary names) if `releases/` holds more
than one build; the script refuses to guess. For every app in `internal/catalog/catalog.json` it claims the name, ships
the binary and promotes it to `edge`, and enables instances. Run it again if something
fails halfway; steps already done are skipped. It stops if the account
doesn't list an app but the name can't be claimed, because someone else owns it. You need to be logged in with
`carlos auth login` first.

Not every catalogue app is built here. Contra's binary comes from its own
repository (`carlos/contra`), and it must be shipped with mail submission on,
which this script doesn't do. So before running the script:

1. Run `make release` in Contra. It writes `releases/contra-linux-arm64` and
   prints the version it stamped.
2. Ship it yourself on Base's account (`BASE_PLATFORM_ACCOUNT`; `oficina` in
   production): `carlos ship --app contra --account <account> --mail submit
   --version <version> releases/contra-linux-arm64`. Claim the name first with
   `carlos apps create` if the account doesn't have `contra` yet. An app
   called `contra` on another account is a different app and doesn't matter.
3. Copy the binary into Base's `releases/` as `contra-linux-arm64-<version>`,
   the same version. The script then finds it already shipped, promotes it to
   `edge` and enables instances.

Contra's version is its own commit, not Base's, so keep one build of each app
in `releases/` rather than passing a version: a version you pass must match
every app's file name.

## Writing an app Base can install

Base starts your app with four variables. Read them, and verify sign-ins
against them:

- `BASE_APP_ORIGIN` is your app's own address.
- `BASE_APP_ISSUER` is Base's address. Accept assertions only from this issuer.
- `BASE_APP_CONSUMER_ID` is the id of your app in the catalogue.
- `BASE_APP_KEYS` is base64 of a JSON object that maps each key id (`kid`) to
  the base64 of that key's 65-byte public key. Check each assertion's
  signature against the key its `kid` names.

`internal/consumer` does all of this for a Go app, and adds a sign-out
(`POST /auth/signout`) that ends the app's own session. Give people a button
for it: signing out of Base doesn't sign them out of your app.

Nothing in Base is specific to one app. `internal/scratchpad` is the small
example to copy, and its README says who can use it: anyone the Base that
installed it vouches for, with no roles of their own.

## Checks

`make ci` is the gate. `make browser` drives Chromium through Base and the
apps it installs; set `RASTRILLO_CHROME` to a Chromium first. `make fonts`
fetches Timeless Sans, which the licence keeps out of the repository. Without
it development and test builds fall back to DM Sans; a release never does.

`docs/live-check.md` is the by-hand check against a real account.
