Impersonation – Attribute to Denote it’s Impersonation of Token

Viewed 142

We have impersonation roles for a number of admins -- it's working as expected.

A Client application, for audit purposes, would like to be informed when a token issued is via impersonation -- is there a way we can send an attribute/claim to the Client informing that the token is an Admin impersonating a user?

1 Answers

Yes, Keycloak stores that information in user session notes. The information can be mapped via builtin mappers.

Follow these steps:

  • Navigate to Clients -> <your client> -> Mappers
  • Click Add Builtin
  • Choose Impersonator Username and/or Impersonator User ID (depends on whether you want the username or id as a claim in the token)
  • Click Add selected

That's it. Your tokens contain the information as shown below:

{
  ...
  "sub": "9ab9bfd1-f95d-4aa1-a8b2-0d1fb06b365a",
  "preferred_username": "test",
  ...
  "impersonator": {
    "id": "2d2f4b4a-716c-4428-97cd-22fa731c0d9a",
    "username": "admin"
  }
  ...
}
Related