Skip to main content

viewercontext

Caller identity propagated between Sentry services: who is acting (an Actor), and for which organization and project. Sentry mints it; other services verify it and pass it along.

One repo, two native implementations released together under one version:

Package Import
Python sentry-viewercontext (PyPI, internal PyPI) import sentry_viewercontext
Rust sentry-viewercontext (crates.io) use sentry_viewercontext::...

fixtures/ holds the wire contract. Both test suites run every case in it, so a change that makes the implementations disagree fails CI.

Python

from sentry_viewercontext import (
    Actor,
    ActorType,
    ViewerContext,
    decode_jwt,
    encode_jwt,
    viewer_context_scope,
)

ctx = ViewerContext(Actor(ActorType.SERVICE_ACCOUNT, actor_id=9), organization_id=1)
token = encode_jwt(ctx, key=secret)  # value for the X-Viewer-Context header
ctx = decode_jwt(token, keys=[current, previous])  # picks the key by `kid`

with viewer_context_scope(ctx):
    ...  # get_viewer_context() returns ctx here

Rust

use sentry_viewercontext::{Actor, ActorType, ViewerContext, decode_jwt, encode_jwt, DEFAULT_LEEWAY, DEFAULT_TTL};

let mut ctx = ViewerContext::new(Actor::new(ActorType::ServiceAccount, Some(9)));
ctx.organization_id = Some(1);
let token = encode_jwt(&ctx, secret, DEFAULT_TTL)?;
let ctx = decode_jwt(&token, &[current, previous], DEFAULT_LEEWAY)?;

Rust has no ambient context; carry the ViewerContext in request extensions or task-locals.

Wire format (version 2)

{
  "version": 2,
  "actor": {"type": "agent", "id": 5, "delegated_user_id": 42, "superuser": {"expires_at": 1800000000}},
  "organization_id": 1,
  "project_id": 7,
  "organization_is_early_adopter": true
}

Only version and actor.type are required. Actor types: user, service_account, system, integration, agent, unknown. As a JWT it is signed HS256 with iat, exp, iss: "sentry" and a kid header (first 8 hex chars of the key's SHA-256). Superuser access caps exp at 5 minutes and at superuser.expires_at.

What a service does with a context (required org scope, permission checks, metrics, where signing keys come from) stays in that service.

Development

uv sync
uvx prek install                                   # or pre-commit; runs ruff, mypy, rustfmt, clippy, zizmor
uv run pytest --cov                                # 100% branch coverage required
cargo test
uv run python scripts/generate_token_fixtures.py  # after changing token cases

CI additionally runs Python 3.13 and 3.14 (Sentry's and Seer's versions), Rust on latest stable, cargo doc with warnings denied, and cargo publish --dry-run.

Releasing

Run the Release workflow with the new version. Craft bumps both manifests (scripts/bump-version.sh) and publishes to GitHub, PyPI, internal PyPI and crates.io.

License

FSL-1.1-ALv2, the same license as getsentry/sentry.

Metadata

Release files for sentry-viewercontext 0.2.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 sentry-viewercontext 0.2.0
File Size Uploaded Provenance Actions
sentry_viewercontext-0.2.0.tar.gz 9.4 kB Without provenance Details for sentry_viewercontext-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sentry-viewercontext 0.2.0
File Interpreter ABI Platform Actions
sentry_viewercontext-0.2.0-py3-none-any.whl Python 3 none any Details for sentry_viewercontext-0.2.0-py3-none-any.whl

Total release size: 18.1 kB

Release files / sentry_viewercontext-0.2.0.tar.gz

Download URL sentry_viewercontext-0.2.0.tar.gz
Size 9.4 kB
Tags Source
SHA-256 checksum
How to use checksums
7dc54bd4b3f8d65cc80f4aa1c48cf18397dca51c75665477d1187577892e8bef
BLAKE2b-256 checksum
How to use checksums
ca11beac1b03785c32c7c7cfaff89614299706f130fb79b6d74725a99cf130e3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.2

Release files / sentry_viewercontext-0.2.0-py3-none-any.whl

Download URL sentry_viewercontext-0.2.0-py3-none-any.whl
Size 8.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c9621daade77f995b0c24f602ba11b9c2269f93d6372dc0194ad71a8b1f4c29c
BLAKE2b-256 checksum
How to use checksums
295d5403f857787986afa8a5ee25350707d8c307349f1bcf8b6a274957ec43bf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.2

Security for sentry-viewercontext 0.2.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