MATIH Platform is in active MVP development. Documentation reflects current implementation status.
6. Identity & Access Management
Impersonation
Starting Sessions

Starting Impersonation Sessions

Impersonation sessions are initiated by administrators through the POST /api/v1/impersonation/start endpoint. The system validates permissions, checks constraints, creates an audit record, and generates a scoped token.


Request Parameters

FieldTypeRequiredConstraints
targetUserIdLongYesMust be a valid user ID
reasonStringYes10-1000 characters
ticketReferenceStringNoMax 100 characters (e.g., SUPPORT-1234)
{
  "targetUserId": 42,
  "reason": "User reports inability to access BI dashboard after recent permission changes",
  "ticketReference": "SUPPORT-5678"
}

Validation Checks

The service performs the following checks before starting a session:

CheckFailure Behavior
Caller has users:impersonate or ADMIN roleReturns 403 UNAUTHORIZED_IMPERSONATION
Target user existsReturns 404 USER_NOT_FOUND
Target does not have PLATFORM_ADMIN roleReturns 409 INVALID_IMPERSONATION
Maximum concurrent sessions not exceededReturns 429 MAX_SESSIONS_EXCEEDED
Caller is not impersonating themselvesReturns 409 INVALID_IMPERSONATION

Response

On success, the endpoint returns an ImpersonationResult:

{
  "sessionId": "imp-session-uuid",
  "impersonationToken": "eyJhbGciOiJIUzI1NiJ9...",
  "targetUser": {
    "id": 42,
    "email": "target@example.com",
    "displayName": "Target User"
  },
  "expiresAt": "2026-02-12T16:00:00Z",
  "maxDurationMinutes": 60
}

The impersonationToken should be used in the Authorization: Bearer header for subsequent API calls. This token includes metadata marking it as an impersonation token.


Context Headers

During an impersonation session, all API requests made with the impersonation token automatically include:

HeaderValue
X-Impersonation-SessionThe session ID
X-Impersonated-ByThe admin's user ID
X-Original-UserThe target user's ID

These headers are injected by the authentication middleware and cannot be forged by the client.


Active Sessions

To view currently active impersonation sessions, use GET /api/v1/impersonation/sessions/active. This returns all sessions initiated by the authenticated admin.