Skip to main content

Signed Cookie-Based HTTP sessions for Muffin framework

Project description

Muffin-Session – Signed Cookie-Based HTTP sessions for Muffin framework

Tests Status PYPI Version

Requirements

  • python >= 3.8

Installation

Muffin-Session should be installed using pip:

pip install muffin-session

Usage

  1. Use it manually

from muffin import Application, ResponseHTML
from muffin_session import Plugin as Session

# Create Muffin Application
app = Application('example')

# Initialize the plugin
# As alternative: session = Session(app, **options)
session = Session()
session.init(app, secret_key='REALLY_SECRET_KEY_FOR_SIGN_YOUR_SESSIONS')

# Use it inside your handlers
@app.route('/update')
async def update_session(request):
    ses = session.load_from_request(request)
    ses['var'] = 'value'
    response = ResponseHTML('Session has been updated')
    session.save_to_response(ses, response)
    return res

@app.route('/load')
async def load_session(request):
    ses = session.load_from_request(request)
    return ses.get('var')
  1. Auto manage sessions (with middleware)

from muffin import Application, ResponseHTML
from muffin_session import Plugin as Session

# Create Muffin Application
app = Application('example')

# Initialize the plugin
# As alternative: session = Session(app, **options)
session = Session()
session.init(app, secret_key='REALLY_SECRET_KEY_FOR_SIGN_YOUR_SESSIONS', auto_manage=True)

# Use it inside your handlers
@app.route('/update')
async def update_session(request):
    request.session['var'] = 'value'
    return 'Session has been updated'

@app.route('/load')
async def load_session(request):
    return request.session.get('var')

Options

Format: Name – Description (default value)

secret_key – A secret code to sign sessions (InsecureSecret)

auto_manage – Load/Save sessions automatically (False). Session will be loaded into request.session

cookie_name – Sessions’s cookie name (session)

cookie_params – Sessions’s cookie params ({‘path’: ‘/’, ‘max-age’: None, ‘samesite’: ‘lax’, ‘secure’: False})

default_user_checker – A function to check a logged user (lambda x: x)

login_url – An URL to redirect anonymous users (it may be a function which accept request and returns a string) (/login)

You are able to provide the options when you are initiliazing the plugin:

session.init(app, secret_key='123455', cookie_name='info')

Or setup it inside Muffin.Application config using the SESSION_ prefix:

SESSION_SECRET_KEY = '123455'

SESSION_COOKIE_NAME = 'info'

Muffin.Application configuration options are case insensetive

Examples

from muffin import Application, ResponseHTML
from muffin_session import Plugin as Session

# Create Muffin Application
app = Application('example')

# Initialize the plugin
# As alternative: session = Session(app, **options)
session = Session()
session.init(app, secret_key='REALLY_SECRET_KEY_FOR_SIGN_YOUR_SESSIONS', auto_manage=True)

@session.user_loader
async def load_user(ident):
    """Define your own user loader. """
    return await my_database_load_user_by_id(ident)

@app.register('/session')
async def get_session(request):
    """ Load session and return it as JSON. """
    return dict(request.session)

@app.register('/admin')
@session.user_pass(lambda user: user.is_admin)
async def admin(request):
    """Awailable for admins only. """
    return 'TOP SECRET'

@app.register('/login')
async def login(request):
    """Save user id into the current session. """
    # ...
    session.login(request, current_user.pk)
    return 'OK'

@app.register('/logout')
async def logout(request):
    """ Logout user. """
    # ...
    session.logout(request)
    return 'OK'

@app.register('/somewhere')
async def somewhere(request):
    """ Do something and leave a flash message """
    # ...
    request.session.clear()
    return 'OK'

Bug tracker

If you have any suggestions, bug reports or annoyances please report them to the issue tracker at https://github.com/klen/muffin-session/issues

Contributing

Development of Muffin-Session happens at: https://github.com/klen/muffin-session

Contributors

  • klen (Kirill Klenov)

License

Licensed under a MIT license.

Project details


Download files

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

Source Distribution

muffin-session-0.7.1.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

muffin_session-0.7.1-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file muffin-session-0.7.1.tar.gz.

File metadata

  • Download URL: muffin-session-0.7.1.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.9.0

File hashes

Hashes for muffin-session-0.7.1.tar.gz
Algorithm Hash digest
SHA256 afd16c59b9517717000ee509014f13e562fe13fce616c510d2f807c6c8b127a5
MD5 58aa5b44a9f6e7ee0e6708c1a0f1e1bb
BLAKE2b-256 c388dc01e8887950a7f540c21079083cc77281e235d606719d5cfff11eea39d5

See more details on using hashes here.

Provenance

File details

Details for the file muffin_session-0.7.1-py3-none-any.whl.

File metadata

  • Download URL: muffin_session-0.7.1-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.9.0

File hashes

Hashes for muffin_session-0.7.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fa38d471b2189a22339fd6306fddfcf0071b8158718313f3fc126ae4e93a6117
MD5 c8ac6112401037ee793a1b4101a3d86d
BLAKE2b-256 9ff7ccd0e60dbe9f63a0eb3b08a747ba9963ba7e3854f9b3937c16e31aa61fc4

See more details on using hashes here.

Provenance

Supported by

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