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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

larzsession-0.1.0.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

larzsession-0.1.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file larzsession-0.1.0.tar.gz.

File metadata

  • Download URL: larzsession-0.1.0.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.13

File hashes

Hashes for larzsession-0.1.0.tar.gz
Algorithm Hash digest
SHA256 09329a8efaa6e631555924576b29cd6bfca0d0766b34fcd6620c4d1aaa3ea46d
MD5 b0049342d1161323841e52be48241822
BLAKE2b-256 85cec6665a06dfe0ecd9b37220b4b8ab9617d55d53fdd7777de749cfb1e0b2a8

See more details on using hashes here.

File details

Details for the file larzsession-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: larzsession-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.13

File hashes

Hashes for larzsession-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6cbadb09161972a9e29e89aec63cd8d6e3d5473c7902da3ebeb1dc88dee1178a
MD5 7a8bb996343b2c0bda3a1ef79a69025b
BLAKE2b-256 0bde8ff242a0098c889d82644b4f61f679b4b6ffd151d70f7a702cc4a2569b23

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page