karni-impersonation
Safe 'log in as user' for Karni support staff: an explicit permission, no privilege escalation, a banner, a time limit, both identities in the audit trail.
A support person with the explicit permission acts as a customer to see what they see. The session comes with a banner on every page, a session record, both identities in the audit trail, a time limit and a hard block on dangerous actions.
Install
pip install karni-impersonation
karni add impersonation
karni migrate
karni add impersonation writes the middleware into the # karni:middleware
block of config/settings.py, after Django's own list, so it follows
AuthenticationMiddleware. karni remove impersonation takes it out again.
Without the CLI, add it yourself right after AuthenticationMiddleware.
The banner's exit needs the URL include. karni check fails with IMP007
until the middleware and the include are both in place:
# config/urls.py: the banner's exit
path("impersonation/", include("karni_impersonation.urls")),
If the middleware stays in MIDDLEWARE while the app is out of
INSTALLED_APPS, importing it fails with IMP007 and names both.
Grant the permission to the role that does support:
KARNI_ROLES = {
...
"support": {"grants": {"karni_impersonation.impersonationsession:impersonate": "all", ...}},
}
Write the permission out. A wildcard never reaches it (class Karni: explicit_actions): the default user and customer ("*": "own") and
staff ("*": "all") roles do not hold it, and neither does
"karni_impersonation.impersonationsession:*".
Keep the Celery worker and beat running: the target's notification is a task and an hourly sweep closes abandoned sessions.
Main features
- An explicit
impersonatepermission, never implied by staff - No privilege escalation: a support user never acts as someone with more rights
- A banner naming both identities, with a one-click exit
- A session record and every write attributed to both identities in
karni.audit - A time limit, and a hard block on deleting, exporting and payment changes
- React components in
@karni/impersonation-ui
Threat model
What it allows. A user holding karni_impersonation.impersonationsession:impersonate
(the actor) acts as another user (the target) for at most
KARNI_IMPERSONATION_MAX_MINUTES (60). Every request is evaluated as the
target (their roles, their row scope, their field visibility), through the
admin (session auth) or the API (session or JWT). The actor sees what the
target sees and may change what the target may change, minus the blocked
list.
What it blocks.
- Starting without the permission (
IMP001). Staff status never implies it, and no wildcard grant reaches it. Only a role whose grants name it does. Superusers hold every grant by Karni's convention, so treat superuser accounts accordingly. - Privilege escalation (
IMP002). The target may not be a superuser or staff unless the actor is, may not hold any role grant at a scope the actor does not hold (allcovers all), and may not hold any Django permission the actor lacks. Checked inservices.start, before anything is written, whoever calls it. - Impersonating yourself, an inactive account, or starting a second
session (
IMP004), and starting without a reason whenKARNI_IMPERSONATION_REQUIRE_REASONis on (IMP003, the default). - Dangerous actions during the session (
IMP006), whatever the target may do:KARNI_IMPERSONATION_BLOCKEDlists viewset and admin action names,app.modellabels,app.model:actionpermission keys and URL paths. By default it blocks deleting (destroy,*:delete,delete_selected, …), exporting and importing, everything inkarni_payments, and the credential paths (/api/auth/password/*,/api/auth/email/*,/api/auth/2fa/*,/api/auth/register/). This is enforced through Karni's veto registry at the API gate and the admin's permissions and action list, and by the middleware for paths. It is not enforced in a UI. - Outliving the limit. In session mode the middleware ends the session
on the first request past
expires_at. In JWT mode the tokens issued for the session expire with it and the middleware refuses a token whose session has ended. The hourly sweep closes what no request touched. - Silent use. The target is emailed on start (
KARNI_IMPERSONATION_NOTIFY_TARGET, default on).
What it logs. An ImpersonationSession row per session: actor, target,
reason, start, limit, end and why it ended (stopped, expired, logout,
swept), IP and user agent. The row is itself audited (karni.audit). At
start the module opens a karni.audit ChangeBatch of kind
impersonation whose actor is the impersonator. Every request of the
session runs inside it, so every change made while impersonating has
actor = the target (the row's history) and batch → the
impersonator. Both identities are on every write.
How to tell after the fact whether it was used.
- Admin → Karni impersonation → Impersonation sessions (read-only: nobody can delete a record through the admin or the API).
ImpersonationSession.objects.filter(target=user): who acted as this user, when, why.ChangeLog.objects.filter(batch__kind="impersonation"): every change made under impersonation..filter(batch__actor=support_user)narrows it to one actor. A row's history showsbatchon each entry.ImpersonationSession.objects.filter(ended_at__isnull=True): sessions open right now.
What it does not do. It does not hide the actor: the banner names both
people on every page and GET /api/impersonation-sessions/current/ says so
to a SPA. It does not grant the actor anything the target lacks, and it
does not grant the target anything. It cannot block an action a project
exposes outside Karni's gates (a plain Django view that writes). List its
path in KARNI_IMPERSONATION_BLOCKED.
Where it shows
| Surface | What |
|---|---|
| Admin → user change page → Impersonate (django-unfold) / Impersonation sessions → Impersonate a user | the reason form, then the site as the target with the banner |
| the banner (every HTML page, session mode) | both identities, minutes left, Stop impersonating |
POST /api/impersonation-sessions/start/ {"user": <id or email>, "reason"} |
the session. JWT mode adds access/refresh tokens for the target, bounded by the limit |
POST /api/impersonation-sessions/stop/ |
ends it, and JWT mode returns fresh tokens for the actor |
GET /api/impersonation-sessions/current/ |
the session this request belongs to, or null |
GET /api/impersonation-sessions/ |
the log (needs the view grant, and an actor sees their own) |
@karni/impersonation-ui |
<ImpersonationBanner/> and useImpersonation() for SPAs |
Settings
| Setting | Env var | Default | Meaning |
|---|---|---|---|
KARNI_IMPERSONATION_MAX_MINUTES |
same | 60 |
hard limit on a session |
KARNI_IMPERSONATION_BLOCKED |
same (comma list) | deletes, exports/imports, karni_payments.*, credential paths |
what is blocked while impersonating |
KARNI_IMPERSONATION_REQUIRE_REASON |
same | True |
a reason is required to start |
KARNI_IMPERSONATION_NOTIFY_TARGET |
same | True |
email the impersonated user on start |
KARNI_IMPERSONATION_FROM_EMAIL |
same | "" |
sender ("" uses DEFAULT_FROM_EMAIL) |
KARNI_IMPERSONATION_BANNER |
same | True |
inject the banner into HTML pages |
KARNI_IMPERSONATION_LANDING |
same | / |
where the admin's start page sends the impersonator |
Escape hatches
- Every setting above.
KARNI_IMPERSONATION_BLOCKEDnarrows or widens what is blocked per project (never empty in production). services.start,stop,can_impersonate,privilege_gap,is_blocked,sweepare plain functions for your own views and tasks.karni_impersonation.middleware.current_session(request)tells a view whether it runs impersonated.- The banner template
karni_impersonation/banner.htmland the start pagekarni_impersonation/start.htmlcan be overridden in the project's templates. services.find_userresolves the start form's input. Subclass the viewset (auto_register = False) to change the API.- This module ships an explicit
admin.py(the session log and the start page) and, when the user admin is a django-unfold one, adds an Impersonate button to it.
Links
- The module in the docs: https://cbsshekhawat18-lab.github.io/karni/modules/#modules-in-this-repository
- React components: https://www.npmjs.com/package/@karni/impersonation-ui
- Karni, the framework this package is part of: https://github.com/cbsshekhawat18-lab/karni
- Changelog: https://github.com/cbsshekhawat18-lab/karni/blob/main/CHANGELOG.md
- Issues: https://github.com/cbsshekhawat18-lab/karni/issues
- License: Apache-2.0. karni-impersonation is released with every other Karni package, at one version.
Metadata
Release files for karni-impersonation 0.5.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| karni_impersonation-0.5.0.tar.gz | 27.5 kB | Without provenance | Details for karni_impersonation-0.5.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| karni_impersonation-0.5.0-py3-none-any.whl | Python 3 | none | any | Details for karni_impersonation-0.5.0-py3-none-any.whl |
Total release size: 65.5 kB
Release files / karni_impersonation-0.5.0.tar.gz
| Download URL | karni_impersonation-0.5.0.tar.gz |
|---|---|
| Size | 27.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0d8e36d69ab6942069a50494a26eed353819921b40fbef09dab326629065a568
|
|
BLAKE2b-256 checksum How to use checksums |
a1df9ef5f2e86f75cc855eb7e90c1ee85ff725516bb4a7a09030a40ad64041d5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / karni_impersonation-0.5.0-py3-none-any.whl
| Download URL | karni_impersonation-0.5.0-py3-none-any.whl |
|---|---|
| Size | 38.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d6b1332e1710625b31da99484690072078b667e8e98205709557326d39de44a2
|
|
BLAKE2b-256 checksum How to use checksums |
0ac729fe63d17f3be95561331c7c2b7c787a9296e0046a75f23a0630776674df
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Security for karni-impersonation 0.5.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.