Pyjoyment.Sessions - Session manager based on signed cookies

from Pyjoyment.Sessions import Sessions

sessions = Sessions()
sessions.cookie_name = 'myapp'
sessions.default_expiration = 86400

# Or import the module, as in Google Python Style Guide

import Pyjoyment.Sessions

sessions = Pyjoyment.Sessions.new()
sessions.cookie_name = 'myapp'
sessions.default_expiration = 86400

Pyjoyment.Sessions manages sessions based on signed cookies for Pyjoyment. All data gets serialized with Pyjo.JSON and stored Base64 encoded on the client-side, but is protected from unwanted changes with a HMAC-SHA256 signature.

See also

Pyjoyment, https://mojolicious.org.

Ported from Mojolicious::Sessions of Mojolicious 9.49.

class Pyjoyment.Sessions.Pyjoyment_Sessions[source]

Pyjoyment.Sessions inherits all methods from Pyjo.Base and implements the following new ones.

cookie_domain: str | None
domain = sessions.cookie_domain
sessions.cookie_domain = '.example.com'

Domain for session cookies, not defined by default.

cookie_name
name = sessions.cookie_name
sessions.cookie_name = 'session'

Name for session cookies, defaults to pyjoyment.

cookie_path
path = sessions.cookie_path
sessions.cookie_path = '/foo'

Path for session cookies, defaults to /.

default_expiration
time = sessions.default_expiration
sessions.default_expiration = 3600

Default time for sessions to expire in seconds from now, defaults to 3600. The expiration timeout gets refreshed for every request. Setting the value to 0 will allow sessions to persist until the browser window is closed, this can have security implications though. For more control you can also use the expiration and expires session values.

# Expiration date in seconds from now (persists between requests)
c.session(expiration=604800)

# Expiration date as absolute epoch time (only valid for one request)
c.session(expires=time.time() + 604800)

# Delete whole session by setting an expiration date in the past
c.session(expires=1)
deserialize: Callable[[bytes], Any]
cb = sessions.deserialize
sessions.deserialize = lambda data: ...

A callback used to deserialize sessions, defaults to Pyjo.JSON.decode_json().

sessions.deserialize = lambda data: {}
load(c: Pyjoyment.Controller.object) → None[source]
sessions.load(Pyjoyment.Controller.new())

Load session data from signed cookie.

partitioned: bool | None
boolean = sessions.partitioned
sessions.partitioned = boolean

Partitioned flag, this is to be used in accordance to the CHIPS amendment to RFC 6265.

Note that this attribute is EXPERIMENTAL because even though most commonly used browsers support the feature, there is no specification yet besides this draft.

Partitioned cookies are held within a separate cookie jar per top-level site.

samesite: str | None
samesite = sessions.samesite
sessions.samesite = 'Strict'

Set the SameSite value on all session cookies, defaults to Lax.

# Disable SameSite feature
sessions.samesite = None
secure: bool | None
boolean = sessions.secure
sessions.secure = boolean

Set the secure flag on all session cookies, so that browsers send them only over HTTPS connections.

serialize: Callable[[dict[str, Any]], bytes]
cb = sessions.serialize
sessions.serialize = lambda session: ...

A callback used to serialize sessions, defaults to Pyjo.JSON.encode_json().

sessions.serialize = lambda session: b''
store(c: Pyjoyment.Controller.object) → None[source]
sessions.store(Pyjoyment.Controller.new())

Store session data in signed cookie.