aiohttp-session

Web sessions for aiohttp.web

Latest version: 2.12.1 registry icon
Maintenance score
100
Safety score
97
Popularity score
39
Check your open source dependency risks. Get immediate insight about security, stability and licensing risks.
Security
  Vulnerabilities
Version Suggest Low Medium High Critical
2.12.1 0 0 0 0 0
2.12.0 0 0 0 0 0
2.11.0 0 0 0 0 0
2.10.0a0 0 0 0 0 0
2.10.0 0 0 0 0 0
2.9.0 0 0 0 0 0
2.8.0 0 0 0 0 0
2.7.0 0 0 0 0 0
2.6.0 0 0 1 0 0
2.5.1 0 0 1 0 0
2.4.0 0 0 1 0 0
2.3.0 0 0 2 0 0
2.2.0 0 0 2 0 0
2.1.0 0 0 2 0 0
2.0.1 0 0 2 0 0
2.0.0 0 0 2 0 0
1.2.1 0 0 2 0 0
1.2.0 0 0 2 0 0
1.1.0 0 0 2 0 0
1.0.1 0 0 2 0 0
1.0.0 0 0 2 0 0
0.8.0 0 0 2 0 0
0.7.1 0 0 2 0 0
0.7.0 0 0 2 0 0
0.5.0 0 0 2 0 0
0.4.0 0 0 2 0 0
0.3.0 0 0 2 0 0
0.2.0 0 0 2 0 0
0.1.2 0 0 2 0 0
0.1.1 0 0 2 0 0
0.1.0 0 0 2 0 0
0.0.1 0 0 2 0 0

Stability
Latest release:

2.12.1 - This version is safe to use because it has no known security vulnerabilities at this time. Find out if your coding project uses this component and get notified of any reported security vulnerabilities with Meterian-X Open Source Security Platform

Licensing

Maintain your licence declarations and avoid unwanted licences to protect your IP the way you intended.

Apache-1.0   -   Apache License 1.0

Not a wildcard

Not proprietary

OSI Compliant



aiohttp_session

.. image:: https://github.com/aio-libs/aiohttp-session/actions/workflows/ci.yaml/badge.svg?branch=master :target: https://github.com/aio-libs/aiohttp-session/actions/workflows/ci.yaml .. image:: https://codecov.io/github/aio-libs/aiohttp-session/coverage.svg?branch=master :target: https://codecov.io/github/aio-libs/aiohttp-session .. image:: https://readthedocs.org/projects/aiohttp-session/badge/?version=latest :target: https://aiohttp-session.readthedocs.io/ .. image:: https://img.shields.io/pypi/v/aiohttp-session.svg :target: https://pypi.python.org/pypi/aiohttp-session

The library provides sessions for aiohttp.web__.

.. _aiohttp_web: https://aiohttp.readthedocs.io/en/latest/web.html

__ aiohttp_web_

Usage

The library allows us to store user-specific data into a session object.

The session object has a dict-like interface (operations like session[key] = value, value = session[key] etc. are present).

Before processing the session in a web-handler, you have to register the session middleware in aiohttp.web.Application.

A trivial usage example:

.. code:: python

import time
from cryptography import fernet
from aiohttp import web
from aiohttp_session import setup, get_session
from aiohttp_session.cookie_storage import EncryptedCookieStorage


async def handler(request):
    session = await get_session(request)
    last_visit = session['last_visit'] if 'last_visit' in session else None
    session['last_visit'] = time.time()
    text = 'Last visited: {}'.format(last_visit)
    return web.Response(text=text)


def make_app():
    app = web.Application()
    fernet_key = fernet.Fernet.generate_key()
    f = fernet.Fernet(fernet_key)
    setup(app, EncryptedCookieStorage(f))
    app.router.add_get('/', handler)
    return app


web.run_app(make_app())

All storages use an HTTP Cookie named AIOHTTP_SESSION for storing data. This can be modified by passing the keyword argument cookie_name to the storage class of your choice.

Available session storages are:

  • aiohttp_session.SimpleCookieStorage() -- keeps session data as a plain JSON string in the cookie body. Use the storage only for testing purposes, it's very non-secure.

  • aiohttp_session.cookie_storage.EncryptedCookieStorage(secret_key) -- stores the session data into a cookie as SimpleCookieStorage but encodes it via AES cipher. secrect_key is a bytes key for AES encryption/decryption, the length should be 32 bytes.

    Requires cryptography library::

    $ pip install aiohttp_session[secure]
    
  • aiohttp_session.redis_storage.RedisStorage(redis_pool) -- stores JSON encoded data in redis, keeping only the redis key (a random UUID) in the cookie. redis_pool is a redis object, created by await aioredis.from_url(...) call.

    $ pip install aiohttp_session[aioredis]
    

Developing

Install for local development::

$ make setup

Run linters::

$ make lint

Run tests::

$ make test

Third party extensions

  • aiohttp_session_mongo <https://github.com/alexpantyukhin/aiohttp-session-mongo>_

  • aiohttp_session_dynamodb <https://github.com/alexpantyukhin/aiohttp-session-dynamodb>_

License

aiohttp_session is offered under the Apache 2 license.