Skip to main content

Hyperdiv Session

Adds the support for sessions to Hyperdiv. This is an essential plugin that enables Hyperdiv users to create authorization flows, data persistence across browser sessions and multiuser support.

Getting Started

  1. pip install hyperdiv-session
  2. Import this plugin from hyperdiv_session import session.
  3. Initialize this plugin and provide the secret string for cookie signing to work.
  4. Handle non-authenticated state, create new session after authentication, persist sessions if required.

Demo app

The example.py contains a basic Hyperdiv application that can handle authentication or log in, persist user across browser windows, persist user data to the filesystem and have log out feature.

import hyperdiv as hd
from hyperdiv_session import session

from _storage import connect, persist, load, delete


def main():
    # Create a session object with a secret key
    sid = session(secret_key="some very secret")

    # Create some view state to store a count
    counter = hd.state(count=0)

    with hd.box(padding=8, gap=2):
        if not sid.is_authenticated():
            hd.text("Not authenticated yet.")

            if hd.button("Authenticate").clicked:
                # create new session
                sid.create_new()
                sid.gdpr_flag = True  # GDPR consent

                # save session into storage
                persist(sid.session_id, counter.count)

        else:
            # load state for given session_id from storage
            counter.count = load(sid.session_id)

            hd.text("Session demo app.")
            hd.text(sid.session_id)
            hd.text(counter.count)

            if hd.button("Increment").clicked:
                counter.count += 1

                # update session state in storage
                persist(sid.session_id, counter.count)

            if hd.button("Log out").clicked:
                sid.clear()
                delete(sid.session_id)


connect()  # open connection to storage or create a new one

hd.run(main)

https://github.com/vladignatyev/hyperdiv-session/assets/513940/abdf89f6-9d38-48a3-89d2-2d9166bdfddc

Notes on implementation

The client-side persistence implemented using localStorage (see: MDN Web Docs). We use signed cookie as session token. The cookie signing mechanism is derived from Django. We use salted HMAC with SHA-256 hasher for timestamped cookies.

Warning

This is a work-in-progress software! It may lack required features, contain bugs or breaches. Please create new issue for feature request and bug report.

TODO

  • Test coverage
  • Make the XSS testing stage
  • Create documentation and samples
  • Implement GDPR compliance

Metadata

Release files for hyperdiv-session 0.1.1

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

Source distribution (sdist)

Source distribution for hyperdiv-session 0.1.1
File Size Uploaded
hyperdiv_session-0.1.1.tar.gz 7.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for hyperdiv-session 0.1.1
File Interpreter ABI Platform
hyperdiv_session-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 16.8 kB

Release files / hyperdiv_session-0.1.1.tar.gz

Download URL hyperdiv_session-0.1.1.tar.gz
Size 7.7 kB
Tags Source
SHA-256 checksum
How to use checksums
b354c1516ad3217462fe3075a2cec534b771fae3ad69aecff4a5de26b47d6eeb
BLAKE2b-256 checksum
How to use checksums
5d0929bae4886816d0393caf9175069bb7377b3ef150b89817107ecab68d45e4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.10.13 Linux/5.15.0-97-generic

Release files / hyperdiv_session-0.1.1-py3-none-any.whl

Download URL hyperdiv_session-0.1.1-py3-none-any.whl
Size 9.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f2dba8fe29a0aed04fe6e9bd090b306aa86bf66a08e329a220c2df54f2676a08
BLAKE2b-256 checksum
How to use checksums
7473a57822e101453a496c671d6e7bc8905fa49793aecc3ebb87fb5ad7e1cb71
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.10.13 Linux/5.15.0-97-generic

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

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