# SSO integration

Pulse is connected to the company SSO system as the portal **`pulse`**. The SSO system decides who may enter Pulse; Pulse keeps its own roles and permissions.

The existing email/password login page is unchanged and keeps working.

## How it works

| When | What happens in Pulse |
| --- | --- |
| A super admin gives someone Pulse access in SSO | SSO calls `POST /passport/users`. Users are matched by **email**: if a Pulse user with that email exists, it is linked and keeps its role. Otherwise a new user is created with Pulse's **lowest role, Member**, so nobody is in Pulse without a role. SSO also sends the employee ID; Pulse ignores it. |
| The user opens Pulse from SSO "My Apps" | Their browser lands on `GET /passport/sso?token=…`. Pulse checks the token and starts a normal Pulse session. |
| A Pulse admin wants a different role | Edit the user in **Users**, pick the role (and department, position) and save, as for any user. |
| Access is removed in SSO | SSO calls `POST /passport/users/:passportUserId/revoke`. Pulse sets `ssoRevoked`; their next request is refused and password login is blocked too. |
| Access is given again | `POST /passport/users` clears `ssoRevoked`. The role they had is kept. |
| Anyone logs out of Pulse | Pulse's normal logout, back to Pulse's own login page. The SSO session is not ended (portals keep their own login and logout for now). |

The `/passport/pending` page and the **Needs role (SSO)** badge remain only for users created before this change who never got a role.

## Configuration

Set in the server's environment (`.env` / `.env.production`):

| Variable | Value |
| --- | --- |
| `SSO_URL` | Public URL of the SSO system, e.g. `https://sso.miaro.com`. Must equal the SSO's `PUBLIC_URL` (it is the token issuer). |
| `SSO_PORTAL_KEY` | `pulse` |
| `SSO_SHARED_SECRET` | The secret SSO showed when the portal was registered |
| `NEXT_PUBLIC_SSO_URL` | Same as `SSO_URL` (not used while logout stays Pulse-only) |
| `NEXT_PUBLIC_SSO_PORTAL_KEY` | `pulse` |

`NEXT_PUBLIC_*` values are baked in at build time, so rebuild after changing them. If `SSO_*` is not set, the three endpoints answer 503 and nothing else changes.

## Deploying

1. Run the migration `20261112120000_passport_sso` (`prisma migrate deploy`). It adds `passportUserId`, `ssoPending`, `ssoRevoked` to `users` and the `passport_used_tokens` table.
2. Set the variables above and rebuild.
3. In SSO, the portal's base URL must be the URL where Pulse serves `/passport/...` (currently `https://pulse.miaro.com`).

## Security notes

- `/passport/users` and `/revoke` accept only requests signed with the shared secret (HMAC-SHA256 over `timestamp.body`, 5-minute window).
- Login tokens are HS256, 60 seconds, for audience `pulse` and issuer `SSO_URL`, and each can be used once (`passport_used_tokens`).
- New SSO users get a random password nobody knows, so they can only enter through SSO.
