Start Impersonation Session
1. What does this feature do? (High-Level Overview)
Section titled “1. What does this feature do? (High-Level Overview)”This feature allows authorized administrators to initiate an impersonation session to access the application as another user. The administrator receives a temporary JWT token that grants them access to act as the target user within a specific location context.
2. Who is this for? (Roles & Permissions)
Section titled “2. Who is this for? (Roles & Permissions)”- Superadmin: Can impersonate any active user in any active location.
- Users with
impersonate_userspermission: Can impersonate users if they have the globalimpersonate_userspermission.
Required Permission: impersonate_users (global scope)
3. Business Rules & Enforcements
Section titled “3. Business Rules & Enforcements”- Rule 1: The impersonator must have the
impersonate_usersglobal permission. - Rule 2: The target user ID must be different from the impersonator’s ID (no self-impersonation).
- Rule 3: The target user must have ACTIVE status.
- Rule 4: The specified location must have ACTIVE status.
- Rule 5: The target user must have active access to the specified location.
- Rule 6: The TTL (time-to-live) must be between 1 minute and 1440 minutes (24 hours). Default is 60 minutes.
- Rule 7: Token scopes must be from the allowed list:
impersonation,read_only,limited(currently onlyimpersonationis implemented).
4. UI Placement
Section titled “4. UI Placement”API Endpoint: POST /api/impersonate/start
This endpoint is typically called from administrative tools or support interfaces.
5. How-To Guide (Step-by-Step)
Section titled “5. How-To Guide (Step-by-Step)”Scenario: Starting an impersonation session
- Authenticate as an administrator with the
impersonate_userspermission. - Send a POST request to
/api/impersonate/startwith the following payload:
{ "user_id": 123, "location": 5, "ttl_minutes": 120, "reason": "Troubleshooting reported issue with BIP access"}Required Fields:
user_id(integer): The ID of the user to impersonatelocation(integer): The location ID for the impersonation context
Optional Fields:
ttl_minutes(integer): Token lifetime in minutes (default: 60, min: 1, max: 1440)reason(string): Reason for impersonation (max 500 characters, recommended for audit purposes)
- Include your admin JWT token in the Authorization header:
Authorization: Bearer {your_admin_jwt_token}- The API will respond with:
{ "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...", "impersonation_id": "550e8400-e29b-41d4-a716-446655440000", "expires_at": "2026-03-31T14:30:00Z", "expires_in": 7200, "user": { "id": 123, "name": "John Doe", "email": "john.doe@example.com", "roles": ["..."], "permissions": ["..."] }, "impersonator": { "id": 1, "name": "Admin User", "email": "admin@example.com" }}-
Store the
tokenvalue securely. Use this token in subsequent API requests to act as the target user. -
Use the impersonation token in the Authorization header for subsequent requests:
Authorization: Bearer {impersonation_token}- The system will respond with custom headers indicating active impersonation:
X-Impersonation-Active: trueX-Impersonation-ID: 550e8400-e29b-41d4-a716-446655440000X-Effective-User-ID: 1236. What happens if…? (Edge Cases / FAQ)
Section titled “6. What happens if…? (Edge Cases / FAQ)”-
Q: What happens if I try to impersonate a user who doesn’t have access to the specified location?
- A: The API will return a 400 Bad Request error with the message: “Target user does not have active access to the specified location.”
-
Q: What happens if I try to impersonate myself?
- A: The API will return a 400 Bad Request error with the message: “You cannot impersonate yourself.”
-
Q: What happens if the target user becomes inactive after I start the session?
- A: Your active session continues until the token expires or is stopped/revoked. However, new impersonation sessions cannot be created for inactive users.
-
Q: Can I extend an active impersonation session?
- A: No, you cannot extend an existing session. When the token expires, you must create a new impersonation session. The maximum TTL is 24 hours (1440 minutes).
-
Q: What information is logged when I start an impersonation session?
- A: The system logs: impersonator ID and name, target user ID and name, location ID, impersonation ID (UUID), TTL, reason (if provided), IP address, user agent, and timestamp. All actions performed during the session are also logged with both actor and effective user information.
-
Q: What permissions does the impersonation token have?
- A: The impersonation token inherits all the roles and permissions that the target user has for the specified location. The response includes the target user’s location-scoped roles and permissions.
Technical Details
Section titled “Technical Details”JWT Token Structure
Section titled “JWT Token Structure”The impersonation token is a JWT with the following custom claims:
{ "sub": 123, // Target user ID "impersonator_id": 1, // Impersonator user ID "impersonation_id": "uuid", // Unique session identifier "scopes": ["impersonation"], // Token scopes "type": "impersonation", // Token type marker "iat": 1711888800, // Issued at timestamp "exp": 1711892400 // Expiration timestamp}Token Storage
Section titled “Token Storage”- The JWT token is hashed using SHA-256 before storage
- Only the hash is stored in the database (not the plain token)
- The token hash is indexed for fast lookup during validation
Security Measures
Section titled “Security Measures”- Permission Check: Validates
impersonate_userspermission before allowing session creation - Business Rule Validation: Enforces all business rules (active user, active location, TTL bounds, etc.)
- Unique Session ID: Each session has a unique UUID to prevent token confusion
- Token Hash Storage: Only hashes stored in database, not plain tokens
- Comprehensive Logging: All actions recorded in multiple audit tables
- IP and User Agent Tracking: Records client information for security analysis