# Kyros integration quickstart for developers and agents

Current contract: **Kyros 5.1.0-alpha.5**, native **SSO v4**. Start with the [integration guide](INTEGRATION_5.1.md). Product versions and protocol versions are independent. Use [OIDC v1](oidc-v1.md) for an OIDC-compatible third-party application. The [SSO v3 guide](sso-guide.md) is historical compatibility documentation.

## Register and configure

Create a client in **Administration → Clients SSO & OIDC**. Choose SSO v4, register exact HTTPS callbacks, allowed scopes and resource audience, and configure access rules if required. Enable `SSO_V4_ENABLED=true` on Kyros with production signing/MFA keys prepared by the operator. Do not convert an existing client until its consumer is ready.

Copy the generated environment block to the consumer backend. Use `KYROS_SSO_VERSION=v4`, the registered edition and application scope, PAR/token/authorize/revoke URLs, the trusted v4 JWKS URL, and configured issuer/global audience/resource audience. SPX uses `KYROS_REQUESTED_SCOPES` (plural); Kyros's generic block exports `KYROS_REQUESTED_SCOPE` (singular).

Keep the client secret on a confidential backend. A public v4 client omits `client_secret`; it must still use PKCE. A supplied invalid secret is rejected. Native Kyros reads credentials from the request body; OIDC's Basic authentication contract is separate. Never distribute a client secret or HS256 global signing secret to Harmonix, a TV or browser JavaScript.

## Authorization Code with PAR and PKCE

1. Generate a random `state` and a PKCE verifier. In Node.js, `crypto.randomBytes(32).toString('base64url')` provides a 43-character verifier. Compute its SHA-256 base64url challenge with no padding.
2. Store a short-lived transaction **bound to the browser session**, including state, verifier, expected issuer and exact callback. Support atomic one-time consumption in the consumer's session/database layer.
3. Send the `par` body from [sso-v4-requests.json](examples/sso-v4-requests.json) to `POST /par`. Replace all `${…}` values. The example is for `standard`; `enterprise`/`both` require their registered scope and `kyros_edition=enterprise`. For a public client remove `client_secret`.
4. On HTTP **201**, redirect the browser to `/authorize` with only the encoded `client_id` and returned `request_uri`. Read `expires_in`; do not construct a request URI locally or downgrade on failure.
5. On the callback, validate the session-bound transaction, its expiry, unique `state`/`iss`/`code` or `error` parameters, state equality and expected issuer **before exchanging anything or trusting an error**. Consume the transaction atomically. Reject missing, foreign and replayed transactions. Kyros echoes state; it cannot validate the consumer's private session.
6. Send the `token` body from the same example file to `POST /token`, retaining the original callback/verifier and **all three handshake fields**. A callback error is not an authorization code. Handle `login_required`/`interaction_required` by starting a new interactive transaction.
7. Validate the access token with a maintained JWT library: pinned RS256, signature from the configured trusted JWKS and `kid`, expected `iss`, `aud`, `resource_aud`, `client_id`, valid `exp`/`nbf`, stable nonempty `sub`, and required scopes. Decoding alone is not validation. Do not follow a key URL supplied by the token.
8. Persist credentials safely and create the consumer session only after validation. Use `sub` as the account key. Never use an email address, username or rotating JWT as a stable identity.

The native callback and JWT issuer use Kyros's **`JWT_ISSUER`**, default `kyros`. This can differ from the HTTPS transport origin. Native `aud` uses `JWT_AUDIENCE`; `resource_aud` is the client's resource audience. OIDC instead uses its configured URL issuer. Obtain expected values from trusted configuration, not from the callback or unverified JWT.

The JSON examples are exercised through real Kyros HTTP endpoints by `npm run qa:clients`. They do not provide a consumer session store, callback handler or JWT library; those protections must also be tested in the actual consumer.

## Refresh, logout and availability

Use the `refresh` example at `POST /token`, with the complete handshake. Serialize refreshes per session/family across workers; replace access and refresh tokens atomically. Reusing an old v4 refresh revokes its family. Refresh preserves `auth_time` and stable identity, and may narrow scopes.

Use the `revoke` example at `POST /revoke` with **`refresh_token`**, not OIDC's `token` field. Logout does not delete local media. An already signed JWT can remain usable by an offline verifier until expiry; do not promise instant invalidation of every consumer session.

On timeout/5xx, preserve local data and unrelated valid sessions, surface temporary unavailability, and keep protected resources subject to valid credentials. A lost refresh response may mean the token has already been consumed: do not blindly retry it. On 429 respect `Retry-After`. Invalid/replayed/revoked credentials require a new authorization, not a password grant fallback.

## Authorization and assurance

- Requesting a scope is a maximum; check the scopes actually granted. SPX administration requires explicit global `spx:admin` on the account; being a Kyros admin or organization member does not grant it. Request `profile spx:admin` but require only `profile` for ordinary sign-in. See [SPX](SPX_DROITS_5.1.md).
- Native v4 rejects the password grant even for a client with an old `direct`/`hybrid` mode. Keep legacy flows only for explicitly retained v3 consumers.
- `securityProfile` defaults to `compatibility`. Native v4 and OIDC clients can opt into `standard` (authentication under 12 h) or `reinforced` (password + TOTP or UV Passkey, under 30 min; recovery excluded). These checks apply to authorization and code/refresh, plus native device issuance. Expect shorter JWTs/`expires_in` near the deadline. Refresh preserves `auth_time` and methods. On native `/token` errors `reauthentication_required` or `mfa_required`, start a new interactive PAR/PKCE transaction. OIDC uses `invalid_grant` at its token endpoint and needs a new OIDC authorization; never replay the consumed credential. OIDC ID tokens carry known `amr`/`acr`, and Reinforced OIDC also requires `SSO_V4_ENABLED=true`. The historical `v4RequireMfa` option remains native only and cumulative. Critical is pending. See the [adoption contract](PROFILS_SECURITE_5.1.md).
- Authorization of device codes, OIDC, Hub APIs and C20 return receipts each have their own contract; use the links in the [integration index](INTEGRATION_5.1.md).

Before activating a real client, complete its [integration acceptance matrix](RECETTE_CLIENTS_5.1.md), including bad state/issuer, callback replay, concurrent refresh, outages and preservation of local files. Never log tokens, secrets, verifiers, codes, receipts or full callback URLs.
