/uHTTP-Session

Session support for µHTTP

Primary LanguagePythonMIT LicenseMIT

µHTTP Session

Session support for µHTTP

Sessions are implemented as Javascript Web Signatures. Which means that:

  1. Sessions are stored in the client's browser.
  2. Sessions are not secret.
  3. Sessions cannot be tempered with.

Installation

µHTTP Session is on PyPI.

pip install uhttp-session

Usage

First, you must set the secret key as an environment variable:

export APP_SECRET='<your secret key goes here>'

Don't have one?

python -c 'import secrets; print(secrets.token_hex())'

Then:

from uhttp import App
from uhttp_session import app as session_app

app = App()
app.mount(session_app)

@app.post('/login')
def login(request):
    request.state['session']['user'] = request.form.get('user', 'john')


@app.get('/'):
def account(request):
    if 'user' not in request.state['session']:
        return 401
    else:
        return f'Hello, {request.state["session"]["user"]}!'

License

Released under the MIT license.