Skip to main content

larzsession

Signed cookie sessions + CSRF tokens. Pure Python, zero dependencies.

Store session data in a tamper-proof cookie the client can't forge — it's serialized, timestamped, and HMAC-signed, so any change invalidates it and you can expire it by age. No database, no session store: the signature is the security. Also generates/validates CSRF tokens and formats hardened Set-Cookie headers.

from larzsession import Serializer

s = Serializer("my-secret-key")
token = s.dumps({"user_id": 42, "role": "admin"})   # -> cookie value
s.loads(token, max_age=3600)                        # {'user_id': 42, ...}, or raises

Why

  • No server-side store. The signed cookie is the session — stateless auth that scales horizontally with nothing shared.
  • Tamper-proof + expirable. HMAC-SHA256 with a derived key; a timestamp lets you enforce max_age. Tampering raises BadSignature; old tokens raise SignatureExpired.
  • CSRF built in. CSRF(secret).generate() / .validate(token) for signed, expiring CSRF tokens.
  • Hardened cookies. cookie() emits HttpOnly; Secure; SameSite=Lax by default.
  • Zero dependencies. Pure hmac/hashlib — no itsdangerous.

Install

pip install larzsession

Usage

from larzsession import Serializer, CSRF, cookie

s = Serializer("secret")
tok = s.dumps({"uid": 1})
data = s.loads(tok, max_age=86400)

csrf = CSRF("secret")
t = csrf.generate(); csrf.validate(t)

set_cookie = cookie("session", tok, max_age=86400, samesite="Strict")
# 'session=...; Max-Age=86400; Path=/; Secure; HttpOnly; SameSite=Strict'

Lower-level Signer / TimestampSigner are exposed too. Built on the same HMAC primitives as larzcrypt.

Tests

python -m unittest discover -s tests -v   # 13 tests

The Larz stack

One of 30+ pure-Python, zero-dependency libraries at github.com/larz-scripter.

License

MIT © larz-scripter

Metadata

Release files for larzsession 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 larzsession 0.1.0
File Size Uploaded
larzsession-0.1.0.tar.gz 5.6 kB Details

Built distribution (wheel)

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

Total release size: 11.2 kB

Release files / larzsession-0.1.0.tar.gz

Download URL larzsession-0.1.0.tar.gz
Size 5.6 kB
Tags Source
SHA-256 checksum
How to use checksums
09329a8efaa6e631555924576b29cd6bfca0d0766b34fcd6620c4d1aaa3ea46d
BLAKE2b-256 checksum
How to use checksums
85cec6665a06dfe0ecd9b37220b4b8ab9617d55d53fdd7777de749cfb1e0b2a8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

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

Download URL larzsession-0.1.0-py3-none-any.whl
Size 5.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6cbadb09161972a9e29e89aec63cd8d6e3d5473c7902da3ebeb1dc88dee1178a
BLAKE2b-256 checksum
How to use checksums
0bde8ff242a0098c889d82644b4f61f679b4b6ffd151d70f7a702cc4a2569b23
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

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