P
P
prikoz2018-10-02 13:35:18
Python
prikoz, 2018-10-02 13:35:18

Session does not live the entire session, error aiohttp_session[secure] - Cannot decrypt cookie value, create a new fresh session?

I can’t understand the essence of the problem, more precisely, how to defeat Cannot decrypt cookie value, create a new fresh session.
There is a layer
Example

@web.middleware
async def auth_session(request, handler):
    session = await get_session(request)
    session_key = session.get('session_key')
    if session_key:
        async with request.app['db'].acquire() as conn:
            result = await conn.execute(sa.select([Session]).where(session_key == session_key))
            session_inst = await result.fetchone()
            if session_inst:
                await conn.execute(Session.update().values(
                    modified_at=datetime.datetime.now()
                ).where(
                    Session.c.session_key == session_key
                ))
                await conn.execute('commit')
    else:
        session_key = generate_session_key()
        session['session_key'] = session_key
        async with request.app['db'].acquire() as conn:
            await conn.execute(Session.insert().values(session_key=session_key, data={}))
            await conn.execute('commit')

    response = await handler(request)
    return response

Пример инициализации приложения
loop = asyncio.get_event_loop()
    app = web.Application(loop=loop, debug=True)
    
    fernet_key = fernet.Fernet.generate_key()
    secret_key = base64.urlsafe_b64decode(fernet_key)
    
    setup(app, EncryptedCookieStorage(secret_key, httponly=True, max_age=60 * 60 * 24 * 5))

I go to the page, update, stupidly click and update, and then there is an error, Cannot decrypt cookie value, create a new fresh session, the session is regenerated, help me sort out the problem. Thank you.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question