Skip to main content

drf-impersonation

PyPI version Python versions Django versions CI Coverage: 100% Typed License: MIT

Header-based user impersonation for Django REST Framework. A permitted user sends one extra header and the request runs as someone else. This works with every DRF authentication class: Token, Session, JWT and OAuth.

GET /api/orders/ HTTP/1.1
Authorization: Token 9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b
X-Impersonate-User: 42

The view sees request.user as user 42. The real caller is still available on request.impersonator.

Contents

Why

Support staff often need to see what a customer sees. Packages like django-impersonate and django-hijack do this by changing the session, so they don't fit token-authenticated APIs that have no session to change.

drf-impersonation runs inside the DRF authentication class. The base class authenticates the real caller as normal. Then the mixin decides whether to swap in the target user. Impersonation lasts for one request only, and the server keeps no state.

Features

  • Any auth backend. Mix it into Token, Session, SimpleJWT, OAuth or a custom class.
  • Stateless. One header on one request. Nothing to start, stop or clean up.
  • Read-only by default. While impersonating, write requests are refused unless you turn this off.
  • Safe defaults. Only superusers can impersonate. Inactive users and privilege escalation are refused.
  • No user enumeration. A target that doesn't exist and a target you may not impersonate give the same 404.
  • Flexible lookups. Find the target by primary key, UUID, email or any other field.
  • Audit hooks. Each impersonated request is logged and sends a Django signal.
  • OpenAPI support. The header appears in drf-spectacular schemas and Swagger UI.
  • Typed. Ships py.typed.

Requirements

Dependency Supported versions
Python 3.10 – 3.15
Django 4.2, 5.2, 6.0, 6.1
Django REST Framework 3.14+
drf-spectacular (optional) 0.27+

CI runs the tests across the supported Python, Django and DRF versions.

Installation

pip install drf-impersonation

To include OpenAPI support:

pip install "drf-impersonation[openapi]"

Quick start

  1. Add the app. This is only needed for OpenAPI support, but adding it is harmless:

    INSTALLED_APPS = [
        # ...
        "rest_framework",
        "drf_impersonation",
    ]
    
  2. Use the ready-made token class:

    REST_FRAMEWORK = {
        "DEFAULT_AUTHENTICATION_CLASSES": [
            "drf_impersonation.authentication.ImpersonationTokenAuthentication",
        ],
    }
    

    For any other auth class, put ImpersonationMixin before it:

    # myproject/authentication.py
    from drf_impersonation.authentication import ImpersonationMixin
    from rest_framework_simplejwt.authentication import JWTAuthentication
    
    
    class ImpersonationJWTAuthentication(ImpersonationMixin, JWTAuthentication):
        pass
    
  3. Send the header as a superuser:

    curl https://api.example.com/api/me/ \
      -H "Authorization: Token <superuser token>" \
      -H "X-Impersonate-User: 42"
    

In a view, request.user is the target and request.impersonator is the real caller. When there is no impersonation, request.impersonator is None:

@api_view(["GET"])
def me(request):
    impersonator = request.impersonator
    return Response(
        {
            "user": request.user.pk,
            "impersonated_by": impersonator.pk if impersonator else None,
        }
    )

How it works

Base auth class (Token / JWT / Session / ...)
  │  no credentials ─────────────────────────▶ None. Bad credentials raise as usual.
  ▼  actor authenticated
X-Impersonate-User header?
  │  no ─────────────────────────────────────▶ actor, request.impersonator = None
  ▼  yes
can_impersonate(actor)?
  │  no ─────────────────────────────────────▶ header ignored, actor
  ▼  yes
READ_ONLY and method is not GET/HEAD/OPTIONS?
  │  yes ────────────────────────────────────▶ 403 Impersonated requests are read-only
  ▼  no
target found and can_impersonate_target(actor, target)?
  │  no ─────────────────────────────────────▶ 404 Impersonation target not found
  ▼  yes
on_impersonate() logs, user_impersonated signal ─▶ target, request.impersonator = actor

request.auth (the token, JWT payload and so on) still belongs to the actor.

Behaviour

Situation Result
No header No change. request.impersonator is None.
Caller may not impersonate Header ignored. The request runs as the caller.
Write method (POST, PUT, PATCH, DELETE) while READ_ONLY is on 403 Impersonated requests are read-only
No user matches the header value 404 Impersonation target not found
Target is the caller, is inactive, or is a superuser while the caller is not 404
Success request.user is the target, request.impersonator is the caller, a warning is logged and the signal is sent.

The read-only check happens before the target lookup. A write request gets 403 whether or not the target exists.

Settings

All settings are optional. Put them in a DRF_IMPERSONATION dict:

DRF_IMPERSONATION = {
    "READ_ONLY": True,
    "SIGNALS": True,
    "OPENAPI": True,
}
Key Default Effect
READ_ONLY True Refuse POST, PUT, PATCH and DELETE with 403 while impersonating.
SIGNALS True Send user_impersonated for each impersonated request.
OPENAPI True Document the header in drf-spectacular schemas.

Settings are read on every request, so override_settings works in tests. An unknown key raises ImproperlyConfigured, which catches typos.

Customising the authentication class

For behaviour that differs per class, subclass and override any of these:

Attribute / method Default Purpose
impersonation_header "X-Impersonate-User" Header that names the target.
impersonation_lookup_fields ("pk",) Fields to match the header value against. All valid fields are tried together with OR, for example ("uuid", "email__iexact").
can_impersonate(request, actor) actor.is_superuser Whether the caller may impersonate anyone.
can_impersonate_target(request, actor, target) Target is active, and no escalation to superuser Whether the caller may impersonate this specific target.
on_impersonate(request, actor, target) Logs a warning on the drf_impersonation logger Runs for each impersonated request, before the signal.
from drf_impersonation.authentication import ImpersonationTokenAuthentication


class SupportImpersonation(ImpersonationTokenAuthentication):
    impersonation_lookup_fields = ("pk", "email__iexact")

    def can_impersonate(self, request, actor):
        return actor.has_perm("accounts.impersonate_user")

Signals

drf_impersonation.signals.user_impersonated is sent after each impersonated request is authenticated. The sender is the authentication class. The keyword arguments are request, actor and target.

from django.dispatch import receiver
from drf_impersonation.signals import user_impersonated


@receiver(user_impersonated)
def record_impersonation(sender, request, actor, target, **kwargs):
    AuditEvent.objects.create(actor=actor, target=target, path=request.path)

Set "SIGNALS": False to stop sending it.

OpenAPI

With drf-spectacular installed and drf_impersonation in INSTALLED_APPS, every endpoint that uses an impersonation class documents the header. The header is a security scheme called impersonation, and it is optional. Each operation lists the base scheme alone and the base scheme together with the header:

security:
  - tokenAuth: []
  - tokenAuth: []
    impersonation: []

Swagger UI then shows an X-Impersonate-User field under Authorize. If READ_ONLY is on, the scheme description says that only safe methods are allowed.

The extension wraps whatever scheme drf-spectacular already uses for the base class, such as Token, SimpleJWT or Session. If the base class has no scheme, nothing is added. Set "OPENAPI": False to turn the extension off.

Type checking

The package ships py.typed, so mypy and pyright check calls to its hooks. request.impersonator is set at runtime on DRF's Request, which type checkers don't know about. Use getattr(request, "impersonator", None) if your checker complains.

Recipes

Allow writes for some callers only

Turn off READ_ONLY and make the decision in a permission class:

from rest_framework.permissions import SAFE_METHODS, BasePermission


class ImpersonationWritesForSuperusers(BasePermission):
    def has_permission(self, request, view):
        impersonator = getattr(request, "impersonator", None)
        if impersonator is None or request.method in SAFE_METHODS:
            return True
        return impersonator.is_superuser

Protect staff accounts from support agents

def can_impersonate_target(self, request, actor, target):
    if not super().can_impersonate_target(request, actor, target):
        return False
    return actor.is_superuser or not target.is_staff

Route the default log

LOGGING = {
    "version": 1,
    "handlers": {"audit": {"class": "logging.FileHandler", "filename": "impersonation.log"}},
    "loggers": {"drf_impersonation": {"handlers": ["audit"], "level": "WARNING"}},
}

Allow the header through CORS

Browsers only send custom headers that the server allows. With django-cors-headers:

from corsheaders.defaults import default_headers

CORS_ALLOW_HEADERS = (*default_headers, "x-impersonate-user")

Security considerations

  • Keep can_impersonate narrow. Anyone it allows can act as almost any user.
  • Leave READ_ONLY on unless support staff really need to make changes on a user's behalf.
  • Keep the escalation check. If you override can_impersonate_target, call super() so that inactive users and superusers stay protected.
  • Keep a durable audit record. The default log line is a minimum. Use the signal to store each event.
  • request.auth is the actor's. Token scopes and claims belong to the real caller, not the target.

To report a vulnerability, see SECURITY.md.

Contributing

Bug reports and pull requests are welcome. See CONTRIBUTING.md for the development setup and the release process, and CHANGELOG.md for the history.

License

MIT © Ahmad Ali

Metadata

Release files for drf-impersonation 0.1.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 drf-impersonation 0.1.0
File Size Uploaded Provenance Actions
drf_impersonation-0.1.0.tar.gz 16.7 kB With provenance Details for drf_impersonation-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for drf-impersonation 0.1.0
File Interpreter ABI Platform Actions
drf_impersonation-0.1.0-py3-none-any.whl Python 3 none any Details for drf_impersonation-0.1.0-py3-none-any.whl

Total release size: 28.3 kB

Release files / drf_impersonation-0.1.0.tar.gz

Download URL drf_impersonation-0.1.0.tar.gz
Size 16.7 kB
Tags Source
SHA-256 checksum
How to use checksums
238f23613fb1b3d20d0f7735b8f4dfafbf3e4ad07c438ed2080cd3583a350420
BLAKE2b-256 checksum
How to use checksums
191885b313055c188908558d6d73d9bb962768a3276702c3fbacaca08432425b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 19, 2026.

Transparency log

Release files / drf_impersonation-0.1.0-py3-none-any.whl

Download URL drf_impersonation-0.1.0-py3-none-any.whl
Size 11.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e3a6e2e7f2566b08c3f8b04d418fcb6d37dbb5fd66637cda8131b1e614c5baea
BLAKE2b-256 checksum
How to use checksums
4aa58b7477ac94f9ab3232894cfa355268134dfea7ad62b47d4ed2660cd369cb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 19, 2026.

Transparency log

Security for drf-impersonation 0.1.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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for drf_impersonation-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 19, 2026.

Transparency log

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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

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