Skip to content

etsuko-io/aiohttp-session

This branch is 453 commits behind aio-libs/aiohttp-session:master.

Folders and files

NameName
Last commit message
Last commit date
Jan 30, 2020
May 12, 2018
Oct 26, 2019
Sep 14, 2019
Oct 26, 2019
Jan 19, 2015
Feb 12, 2018
Sep 17, 2017
Oct 12, 2018
Nov 4, 2019
Jan 3, 2018
Aug 7, 2015
Aug 3, 2016
May 30, 2019
Feb 12, 2018
Aug 17, 2020
Sep 12, 2018

Repository files navigation

aiohttp_session

https://travis-ci.com/aio-libs/aiohttp-session.svg?branch=master https://codecov.io/github/aio-libs/aiohttp-session/coverage.svg?branch=master https://readthedocs.org/projects/aiohttp-session/badge/?version=latest

The library provides sessions for 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:

import time
import base64
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()
    # secret_key must be 32 url-safe base64-encoded bytes
    fernet_key = fernet.Fernet.generate_key()
    secret_key = base64.urlsafe_b64decode(fernet_key)
    setup(app, EncryptedCookieStorage(secret_key))
    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 aioredis pool object, created by await aioredis.create_redis_pool(...) call.

    Requires aioredis library (only versions 1.0+ are supported):

    $ pip install aiohttp_session[aioredis]
    

Developing

Install for local development:

$ pip install -r requirements-dev.txt

Run tests with:

$ pytest -sv tests/

Third party extensions

License

aiohttp_session is offered under the Apache 2 license.

Packages

No packages published

Languages

  • Python 98.9%
  • Makefile 1.1%