# Exchange External SSO Assertion for Pinter Access Token

`POST /api/identity/sso/jwt`

### Overview
Federated identity exchange and **Just-In-Time (JIT) User Provisioning** endpoint. Allows registered client applications (such as Sentra, Identik SSO, or corporate portals) to authenticate their own users and hand Pinter a short-lived assertion proving the user's identity. Pinter verifies the assertion against the registered application credentials and issues native access and refresh tokens.

### Authentication & Headers
- **Auth**: Client Application Credentials (verified via pre-shared `client_secret`)
- **Required Header**: `API-Version: 1`
- **Application Header**: `X-Application-ID: <Application-UUID>`
  - *Strictly required* when sending encrypted `payload`.
  - Highly recommended when sending `external_jwt` (can also resolve app via `app_id`, `aud`, or `iss` claim).

### Request Body & Envelope Formats (`SSOTokenExchangeRequest`)
You must supply **exactly one** of the following envelope fields:
1. **`payload`** *(string, optional, recommended)*:
   - High-security **AES-256-GCM** ciphertext encrypted with a key derived from the application's `client_secret`.
   - The `X-Application-ID` is bound as Additional Authenticated Data (AAD) into the cipher, preventing cross-application replay attacks.
   - Enforces strict lifetime freshness (`ASSERTION_MAX_AGE_SECONDS`).
2. **`external_jwt`** *(string, optional, legacy)*:
   - Standard HS256-signed JWT using the application's `client_secret`.

### Required Assertion Claims
The decrypted assertion or decoded JWT must contain:
- **`username`** *(string, required)*: The user's primary identity handle (e.g. employee NIK for Identik-federated systems, or app-scoped identifier). Bare `sub` is rejected.
- **`name`** *(string, required)*: Full display name of the user.
- **`email`** *(string, required)*: Valid RFC-compliant corporate email address.
- **`exp`** *(number, required)*: Expiration UNIX epoch timestamp (must be in the future and not exceed maximum lifetime).
- **`identity_source`** *(string, optional)*: `identik` (corporate NIK) or `app` (application-scoped). Must match the registered application trust mode.

### Provisioning & Security Behavior
- **JIT Provisioning**: If the user does not exist, Pinter provisions a new account with `role_level = 4` and assigns default quota.
- **Password Locking**: Provisioned accounts receive a hashed password set to `SSO_EXTERNAL_USER`, permanently disabling direct password login.
- **Profile Immutability**: If the account already exists, existing name and email are never overwritten by incoming assertions.
- **Audit Logging**: Inserts or updates application access timestamps in `user_applications.last_access_at`.

## Parameters

| Name | In | Type | Required | Description |
| --- | --- | --- | --- | --- |
| `X-Application-ID` | header | string | no | Registered Client Application UUID used during Single Sign-On (SSO) token exchange. |

## Request body

Content type: `application/json`

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `payload` | string \| null | no |  |
| `external_jwt` | string \| null | no |  |

```json
{
  "payload": "string",
  "external_jwt": "string"
}
```

## Responses

### 200 — Assertion verified; Pinter bearer and refresh tokens issued

### 400 — Missing X-Application-ID header, unknown application, or neither envelope provided

### 401 — Decryption failed, invalid signature, expired assertion, or invalid email claim

### 403 — User account is disabled or application identity_source mismatch

### 409 — Username belongs to an existing non-SSO account

### 422 — Validation Error

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `detail` | object[] | no |  |
| `detail[].loc` | string | integer[] | yes |  |
| `detail[].msg` | string | yes |  |
| `detail[].type` | string | yes |  |
| `detail[].input` | object | no |  |
| `detail[].ctx` | object | no |  |

```json
{
  "detail": [
    {
      "loc": [
        null
      ],
      "msg": "string",
      "type": "string",
      "input": null,
      "ctx": {}
    }
  ]
}
```

## Example

```bash
curl -X POST "$PINTER_URL/api/identity/sso/jwt" \
  -H "Authorization: Bearer $ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"payload":"string","external_jwt":"string"}'
```
