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

# IdP, machines and events

> The rest of the authentication domain: the identity provider, the horizon-authenticate sign-in app, machine authentication, authentication events and the Better Auth migration.

The previous pages cover what a person using MaihueGO goes through. This page gathers the platform
pieces that share the same domain (`authenticate`, ADR 0131) and that an operator or an integrating
team needs to know. Each section is short and links to the full documentation when it already
exists.

## Identity provider (IdP)

The B Brands IdP is the holding's OAuth 2.1 / OpenID Connect provider. It only authenticates: it
answers **who** the person is and **whether they are enabled** for a platform (Odoo first; then CRM,
analytics and partners). It uses Supabase OAuth 2.1 Server as the engine behind a horizon-api facade,
with the environment's canonical `auth.*` issuer.

It has two route families in horizon-api:

| Family | Purpose | Examples |
| - | - | - |
| `/api/v3/oauth/*` | Protocol: authorization, consent, tokens and exchange | `authorize`, `token`, `userinfo`, `revoke`, `exchange` (OIDC → Horizon JWT, 900 s by default), `decision`, `client-hints`, `organization` |
| `/api/v3/idp/*` | Management from Horizon Enterprise, by `document_id` | `client` (create, edit, `suspend`, `revoke`, `rotate-secret`), `access`, `account/[document_id]/revoke`, `audit`, `provisioning-event` (`retry`, `discard`) |

Discovery lives at the app root: `GET /.well-known/openid-configuration` and
`GET /.well-known/jwks.json`.

> [!NOTE]
>
> `rotate-secret` rotates the **provisioning webhook** secret, not the OAuth `client_secret`.
> `client_secret` rotation does not exist yet (see [Security and limits](https://docs.bbrands.io/maihuego/authentication/security.md)).
> As of 2026-09-23, Odoo is registered **in development only**.

The detail lives in the Documentation tab and is not repeated here:

> [card] [OAuth & Identity (IdP)](https://docs.bbrands.io/oauth/introduction.md)
>
> What the IdP is, its layers and decisions.

> [card] [OAuth / OIDC Endpoints](https://docs.bbrands.io/oauth/endpoints.md)
>
> Discovery, JWKS, authorize, consent, token, userinfo and revoke.

> [card] [Token Exchange](https://docs.bbrands.io/oauth/token-exchange.md)
>
> From the OIDC id token to the internal B Brands JWT.

> [card] [Client Integration](https://docs.bbrands.io/oauth/client-integration.md)
>
> How a platform connects as an OAuth client.

## Sign-in app: horizon-authenticate

`horizon-authenticate` is the one place where B Brands people sign in on the web
(`https://auth.bbrands.io` in production, `https://<environment>.auth.bbrands.io` elsewhere). It is a
Next.js 16 app with `next-intl` and a split layout with a brand panel (hidden below the `lg`
breakpoint).

**Locales.** `es` and `en`. The `fr`, `it` and `pt` routes are retired and redirect temporarily to the
same path under `es`. A URL without a locale redirects to the visitor's locale.

| Pages (`app/[locale]/…`) | Use |
| - | - |
| `auth/login`, `auth/register`, `auth/verify-account` | E-mail-first sign-in, registration and verification |
| `auth/recovery-account`, `auth/reset-password` | Recover and reset the password |
| `auth/magic-link` | Consume the sign-in link |
| `auth/oauth/callback`, `auth/oauth/welcome` | Return from the provider; terms and missing names |
| `auth/context`, `auth/no-access`, `auth/logout` | Choose a context, no access, sign out |
| `auth/2fa/setup`, `auth/2fa/verify` | Second factor (answer 404 while the `authenticate-two-factor-ui` flag is off) |
| `oauth/consent` | IdP consent screen |

| Internal routes (`app/api/…`) | Use |
| - | - |
| `auth/session` | Session bridge for the `*.bbrands.io` apps |
| `auth/logout` | Sign-out |
| `auth/magic-link`, `auth/magic-link/resend` | Consume and resend the link |
| `auth/oauth/[provider]/start`, `auth/oauth/complete` | Start and finish a provider sign-in |
| `auth/context/auto`, `auth/context/staff` | Automatic context selection and staff step |

**Hand-off to `next`.** When sign-in ends (password, provider or link), the app goes through the 2FA
gate and redirects to the requested `next`. It only accepts targets in `AUTH_ALLOWED_REDIRECT_HOSTS`.
The `*.bbrands.io` apps read the session through the `auth/session` bridge; apps on other domains (for
example `*.maihuego.com`) receive the `bbx` exchange. The detail is in
[Session](https://docs.bbrands.io/maihuego/authentication/session.md).

> [!NOTE]
>
> **Pending.** The 2FA gate exists, but the `authenticate-two-factor-ui` flag is still `false` in every environment
> and there is no second-factor backend. No second factor is requested today.

## Machine authentication

Two mechanisms that do not involve MaihueGO end customers:

| Mechanism | How it works |
| - | - |
| Cron | Every cron route requires `Authorization: Bearer` with `CRON_SECRET`. Neither `x-vercel-cron`, the user agent, the query nor cookies authenticate. If `CRON_SECRET` is missing or shorter than 32 characters, every call answers `401 Unauthorized cron invocation`. The comparison is constant-time and runs before any side effect. |
| Maihue Lunch worker | `POST /api/v3/auth/login/worker` requires the user to be linked to an active, non-deleted worker; it returns a lightweight payload without accounts, otherwise `403`. `POST /api/v3/auth/login/worker/document` records the worker's RUT declaration. |

Limits: `login-worker` 10 per 10 min per IP; `login-worker-document` 5 per hour per user and IP,
always enforced.

> [!WARNING]
>
> **Pending (T-112, p0).** Rotating `CRON_SECRET` in every environment after the F-024 exposure is in
> progress.

## Authentication events

Supabase Auth notifies horizon-api in two ways:

| Route | What it does |
| - | - |
| `POST /api/v3/webhook/supabase/send-email` | Supabase **Send Email Hook** (Authentication → Hooks → Send Email). Verifies the Standard Webhooks signature with `SUPABASE_AUTH_SEND_EMAIL_HOOK_SECRET` (or the previous one during a rotation) and turns Supabase e-mails (sign-up confirmation, recovery) into catalogue notifications. `webhook-id` makes retries idempotent. |
| `POST /api/v3/webhook/supabase/auth` | Database webhook on `auth.users`. When an e-mail becomes confirmed, it sends the welcome (self-service sign-up) or a magic link to the portal (accounts created by an operator), unless `receive_creation_email=false` or the user is an active contract signer. Requires `x-bbrands-webhook-secret` (`SUPABASE_AUTH_WEBHOOK_SECRET`); without configuration it answers `503`. `GET` describes the route. |

Authentication notification catalogue (`lib/notification/event/registry/auth.events.ts`). None can be
opted out of; security notifications ignore quiet hours.

| Key | When | Producer | Category |
| - | - | - | - |
| `auth.signup.confirmation-requested` | E-mail registration or confirmation resend | Send Email Hook | security |
| `auth.account.welcomed` | The login confirmed its e-mail (replaces `auth.welcome`) | `auth.users` webhook | transactional |
| `auth.magic-link.requested` | Link for a user created by an operator (replaces `auth.magic-link`) | `auth.users` webhook | security |
| `auth.password.recovery-requested` | "Forgot password" with a password | Send Email Hook | security |
| `auth.sign-in.magic-link-requested` | Sign-in link for a login without a password | `POST /auth/magic-link/request` | security |
| `auth.password.created` | The optional password is created | `POST /auth/password` | security |
| `auth.password.changed` | The password is changed | `POST /auth/password/change` | security |
| `auth.login-link.code-requested` | Code to link a pending login | `POST /auth/oauth/link/start` | security |

Migration `0417_notification_event_catalog_authenticate.sql` seeds these keys. Apple notifications
(consent revoked, relay off) send no e-mail: they revoke sessions or mark the channel unavailable (see
[Sign in with Apple](https://docs.bbrands.io/maihuego/authentication/apple.md)).

## Better Auth migration

**Goal.** Replace Supabase Auth (GoTrue) with Better Auth embedded in horizon-api on our own
Postgres, as an engine transplant that keeps the contracts and the user UUIDs. The driver is cost: at
1 million MAU, GoTrue would cost about USD 2,925 per month.

**Status as of 2026-09-23.**

* The documentation is GO. Wave 0 on the current engine is built, deployed and certified (v3 routes,
  2FA gate, Horizon Enterprise on v3, route telemetry); its gate is still formally open.
* **Better Auth itself does not exist yet.** The credential cutover, the IdP engine and the GoTrue
  decommission are NO-GO; the IdP engine is blocked by advisory GHSA-p2fr-6hmx-4528.
* Account-centric identity goes first.

> [!NOTE]
>
> Nothing changes for MaihueGO today: everything described in this section runs on Supabase Auth.

## See also

* [Security and limits](https://docs.bbrands.io/maihuego/authentication/security.md)
* [Operations and maintenance](https://docs.bbrands.io/maihuego/authentication/operations.md)
* [Session](https://docs.bbrands.io/maihuego/authentication/session.md)
* [IdP Security & Governance](https://docs.bbrands.io/oauth/security.md)
* [Social Login & Internal SSO](https://docs.bbrands.io/oauth/social-login.md)


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