> ## 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.

# Apple: tokens and notifications

> Why Sign in with Apple needs more than Google and Microsoft: client secret, encrypted refresh token, revocation on account deletion, Apple notifications and hidden e-mails.

Sign in with Apple comes back in 2026.11.3 because App Review rejected the app under **guideline 4.8**:
an app that offers sign-in with Google must also offer an equivalent option such as Apple. Apple also
brings obligations that Google and Microsoft do not have. This page explains each one and how
horizon-api handles it (ADR 0135). On-screen behaviour is in
[Google, Apple and Microsoft](https://docs.bbrands.io/maihuego/authentication/oauth.md).

## What Apple requires

> [card] Revoke on account deletion
>
> Guideline 5.1.1(v): if the person deletes their account, their Apple tokens must be revoked.
> Revoking needs a refresh token, which only the exchange of an authorization code returns.

> [card] Listen to Apple
>
> Apple sends server-to-server notifications when someone stops using Sign in with Apple, deletes
> their Apple ID or changes the forwarding of their hidden e-mail.

> [card] Hidden e-mail
>
> "Hide My Email" hands out a `@privaterelay.appleid.com` address that only accepts mail from domains
> registered with Apple.

> [card] The name arrives once
>
> Apple sends the first and last name only on the app's first authorization. Later sign-ins carry no
> name.

## Native on iOS, Services ID on Android and the web

| | iOS | Android and web |
| - | - | - |
| Flow | Apple's native sheet | Browser (Supabase authorize) |
| Client towards Apple | The app's bundle ID (`com.maihuego.application`, `.cert`, `.dev`, etc.) | The environment's Services ID (`com.maihuego.signin`, `.cert`, `.dev`) |
| Endpoint | `POST /api/v3/auth/login/oauth/apple` | `GET /api/v3/auth/login/oauth/apple` (app) or `GET /api/v3/auth/oauth/start` (web) |
| Sign-in | `signInWithIdToken` with the nonce | Supabase and horizon-api callbacks |
| Source of the refresh token | Exchange of the `authorization_code` in horizon-api | Supabase's `provider_refresh_token`, **when it returns it** |

That is why Supabase lists as Client IDs the Services ID **and** every bundle ID of the environment (the
native identity token carries the bundle ID as audience), and horizon-api uses the same ids in
`APPLE_CLIENT_IDS`, in the opposite order: the bundle IDs first and the Services ID last, with the
Services ID also in `APPLE_SERVICES_ID` ([Rollout](https://docs.bbrands.io/maihuego/authentication/setup.md)).

## The client secret

Apple does not use a fixed secret: the client secret is an **ES256 JWT** signed with the environment's
`.p8` key (`APPLE_TEAM_ID`, `APPLE_KEY_ID`, `APPLE_PRIVATE_KEY`), with audience
`https://appleid.apple.com` and a validity of **at most 6 months**.

| Who uses it | How it gets it | Expiry |
| - | - | - |
| horizon-api (code exchange and revocation) | Signs it on every call from the `.p8` | No rotation while the key is valid |
| Supabase (browser flow) | Generated by hand and pasted into the Apple provider | After 6 months: regenerate it before then |

> [!WARNING]
>
> If the Supabase secret expires, **the browser Apple sign-in fails** on Android and the web. The
> procedure and the five-month reminder are in
> [Operations and maintenance](https://docs.bbrands.io/maihuego/authentication/operations.md). Rotating it signs nobody out.

## The refresh token, encrypted

Apple's refresh token is stored in the `auth_provider_credential` table, one row per login and
provider:

| Column | What it holds |
| - | - |
| `provider`, `provider_subject` | `apple` and the person's identifier at Apple |
| `client_id` | The bundle ID (native) or the Services ID (browser) Apple issued the token to. Apple only revokes with that same client id. |
| `refresh_token_encrypted` | The token sealed with AES-256-GCM |
| `user` | The auth user (`auth.users`) |

* Encryption uses its own keyring, `AUTH_PROVIDER_TOKEN_KEYRING` (current and previous keys, re-encrypted
  on read), **never** the GoTrue bridge keyring.
* The table has RLS and only the service role reads it.

## Revocation on account deletion

When the person deletes their account in Settings, `softDeleteLogin` revokes the stored token of each
Apple login at `appleid.apple.com/auth/revoke` and then deletes the row. If Apple fails, the error is
logged and **the deletion continues**: our data is deleted anyway. To check it (MT-09): after deleting
the account, the Apple ID settings no longer list MaihueGO.

## Server-to-server notifications

Apple calls `POST /api/v3/auth/oauth/apple/notifications`, configured as the **Server-to-Server
Notification Endpoint** on each environment's primary App ID. The endpoint is public and verifies the
JWS against Apple's public keys (issuer `https://appleid.apple.com`, audience within
`APPLE_CLIENT_IDS`). An invalid JWS answers `400` before any rate-limit budget is spent.

| Event | Meaning | What horizon-api does |
| - | - | - |
| `consent-revoked` | The person stopped using Sign in with Apple for the app | Revokes every session of the login and deletes the stored credential |
| `account-delete` | The person deleted their Apple ID | Same as `consent-revoked` |
| `email-disabled` | The person turned off forwarding for their hidden e-mail | Suppresses the relay address on the e-mail channel, so e-mails do not bounce |
| `email-enabled` | The person turned forwarding back on | Lifts **only** the relay suppressions Apple caused |

* An event for an Apple user without a login here is acknowledged and ignored, so Apple does not retry
  it.
* The `auth_provider_notification` table keeps the last applied event per Apple identifier and group
  (`consent` or `email`). An event whose time is not newer than the last applied one is ignored: a
  retried old `email-enabled` never undoes a newer `email-disabled`, and the same JWS posted twice does
  nothing. If the effect fails, the record goes back to the event applied before it (or is removed when
  there was none), so Apple's retry applies it and an older event still cannot undo a newer one.
* Limit: 20 per 10 minutes per Apple identifier, never per IP.

## Hidden e-mail (Private Email Relay)

With "Hide My Email", the `@privaterelay.appleid.com` address becomes the login e-mail and
`user_metadata.is_private_email` is set to `true` (from the identity token's `is_private_email` claim).
Support uses it to explain why an e-mail looks random.

For those e-mails to arrive, the sending domains and addresses (Resend, its return-path domain and each
Supabase project's SMTP sender) must be registered in **Sign in with Apple for Email Communication**.
Apple only accepts a domain whose SPF passes. See [Rollout](https://docs.bbrands.io/maihuego/authentication/setup.md).

If that person later declares a document that already has an account, they can link their login to that
account instead of creating another one (see
[Identity linking](https://docs.bbrands.io/maihuego/authentication/identity-linking.md)).

## The name, only the first time

If Apple sends the name (first authorization), it is used. If it does not and the profile has no name,
the response carries `needs_name` and the client asks for it: "Confirma tus datos" in the app,
`auth/oauth/welcome` on the web. An existing name is never replaced.

> [!WARNING]
>
> **Pending: spike T-01.** It is not yet confirmed that Supabase returns Apple's
> `provider_refresh_token` in the browser flow. If it does not, Apple logins created on **Android and
> the web** have no stored token and cannot be revoked by token on account deletion: revocation is
> limited to what Apple's notifications allow, although our data is deleted anyway. On iOS (native) the
> token always comes from the code exchange.

## See also

* [Google, Apple and Microsoft](https://docs.bbrands.io/maihuego/authentication/oauth.md)
* [Step-by-step rollout](https://docs.bbrands.io/maihuego/authentication/setup.md)
* [Operations and maintenance](https://docs.bbrands.io/maihuego/authentication/operations.md)
* [ADR 0135](https://github.com/bbrandslab/bbrands-monorepo/blob/develop/docs/decisions/0135-apple-token-lifecycle.md)
* [OAuth providers runbook](https://github.com/bbrandslab/bbrands-monorepo/blob/develop/docs/operations/oauth-providers.md)


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