Skip to main content

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.

PyPI License: Apache-2.0 Docs

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 impersonate permission, 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 (all covers all), and may not hold any Django permission the actor lacks. Checked in services.start, before anything is written, whoever calls it.
  • Impersonating yourself, an inactive account, or starting a second session (IMP004), and starting without a reason when KARNI_IMPERSONATION_REQUIRE_REASON is on (IMP003, the default).
  • Dangerous actions during the session (IMP006), whatever the target may do: KARNI_IMPERSONATION_BLOCKED lists viewset and admin action names, app.model labels, app.model:action permission keys and URL paths. By default it blocks deleting (destroy, *:delete, delete_selected, …), exporting and importing, everything in karni_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 shows batch on 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_BLOCKED narrows or widens what is blocked per project (never empty in production).
  • services.start, stop, can_impersonate, privilege_gap, is_blocked, sweep are 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.html and the start page karni_impersonation/start.html can be overridden in the project's templates.
  • services.find_user resolves 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.

Metadata

Release files for karni-impersonation 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for karni-impersonation 0.6.0
File Size Uploaded Provenance Actions
karni_impersonation-0.6.0.tar.gz 27.5 kB Without provenance Details for karni_impersonation-0.6.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for karni-impersonation 0.6.0
File Interpreter ABI Platform Actions
karni_impersonation-0.6.0-py3-none-any.whl Python 3 none any Details for karni_impersonation-0.6.0-py3-none-any.whl

Total release size: 65.6 kB

Release files / karni_impersonation-0.6.0.tar.gz

Download URL karni_impersonation-0.6.0.tar.gz
Size 27.5 kB
Tags Source
SHA-256 checksum
How to use checksums
ae5137b21c4084d6a76297d55223032eb6f21d882ca3f5b0c1cbe19d397646cd
BLAKE2b-256 checksum
How to use checksums
742cf8ab550e7c48ea7196a288b6da7cf8022b7183f9bc7428ce8616b35aa7a3
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.6.0-py3-none-any.whl

Download URL karni_impersonation-0.6.0-py3-none-any.whl
Size 38.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0e119f91313b5e161279e51bb8a1acb6ca2a44641c9d6ea5ee15378ea22b0d8c
BLAKE2b-256 checksum
How to use checksums
3a3761b4f9fe115c685f7aab6363530537ffc2a998e7a1d8f07f7d5f90535c49
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.6.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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page