Skip to content

Commit

Permalink
Merge branch 'master' of github.com:aio-libs/aiohttp_session
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Nov 24, 2015
2 parents b9a699f + 70666f7 commit 1c2c6ed
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ Changes

- Add optional NaCl encrypted storage #20

- Relax EncryptedCookieStorage to accept base64 encoded string,
e.g. generated by Fernet.generate_key.

- Add setup() function

0.3.0 (2015-11-20)
------------------

Expand Down
6 changes: 6 additions & 0 deletions aiohttp_session/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ def middleware(request):
return factory


def setup(app, storage):
"""Setup the library in aiohttp fashion."""

app.middlewares.append(session_middleware(storage))


class AbstractStorage(metaclass=abc.ABCMeta):

def __init__(self, *, cookie_name="AIOHTTP_SESSION",
Expand Down
6 changes: 5 additions & 1 deletion aiohttp_session/cookie_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ def __init__(self, secret_key, *, cookie_name="AIOHTTP_SESSION",
max_age=max_age, path=path, secure=secure,
httponly=httponly)

self._fernet = fernet.Fernet(base64.urlsafe_b64encode(secret_key))
if isinstance(secret_key, str):
pass
elif isinstance(secret_key, (bytes, bytearray)):
secret_key = base64.urlsafe_b64encode(secret_key)
self._fernet = fernet.Fernet(secret_key)

@asyncio.coroutine
def load_session(self, request):
Expand Down
7 changes: 4 additions & 3 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ A trivial usage example::
import asyncio
import time
from aiohttp import web
from aiohttp_session import get_session, session_middleware
from aiohttp_session import get_session, setup
from aiohttp_session.cookie_storage import EncryptedCookieStorage

@asyncio.coroutine
Expand All @@ -39,8 +39,9 @@ A trivial usage example::

@asyncio.coroutine
def init(loop):
app = web.Application(middlewares=[session_middleware(
EncryptedCookieStorage(b'Thirty two length bytes key.'))])
app = web.Application()
setup(app,
EncryptedCookieStorage(b'Thirty two length bytes key.'))
app.router.add_route('GET', '/', handler)
srv = yield from loop.create_server(
app.make_handler(), '0.0.0.0', 8080)
Expand Down
27 changes: 24 additions & 3 deletions docs/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@ Public functions

.. seealso:: :ref:`aiohttp-session-storage`

.. note:: :func:`setup` is new-fashion way for library setup.

.. function:: setup(app, storage)

Setup session support for given *app*.

The function is shortcut for::

app.middlewares.append(session_middleware(storage))

*app* is :class:`aiohttp.web.Application` instance.

*storage* is a session storage instance (object used to store
session data into cookies, Redis, database etc., class is derived
from :class:`AbstractStorage`).

.. seealso:: :ref:`aiohttp-session-storage`


.. _aiohttp-session-session:

Expand Down Expand Up @@ -245,7 +263,7 @@ To use the storage you should push it into
The class is inherited from :class:`~aiohttp_session.AbstractStorage`.

*secret_key* is :class:`bytes` secret key with length of 32, used
for encoding.
for encoding or base-64 encoded :class:`str` one.

Other parameters are the same as for
:class:`~aiohttp_session.AbstractStorage` constructor.
Expand Down Expand Up @@ -318,8 +336,11 @@ To use the storage you need setup it first::

The class is inherited from :class:`~aiohttp_session.AbstractStorage`.

*redis_storage* is a :class:`~aioredis.RedisPool` which should be
created by :func:`~aioredis.create_pool` call.
*redis_pool* is a :class:`~aioredis.RedisPool` which should be
created by :func:`~aioredis.create_pool` call, e.g.::

redis = yield from aioredis.create_pool(('localhost', 6379))
storage = aiohttp_session.redis_storage.RedisStorage(redis)

Other parameters are the same as for
:class:`~aiohttp_session.AbstractStorage` constructor.

0 comments on commit 1c2c6ed

Please sign in to comment.