Skip to main content

tremolo-login is basically an extension of tremolo-session.

Project description

tremolo-login

Coverage Quality Gate Status

tremolo-login is basically an extension of tremolo-session.

You can use it just like tremolo-session but with additional methods like login(), logout(), and is_logged_in().

Usage

#!/usr/bin/env python3

from hmac import compare_digest

from tremolo import Application
from tremolo_login import Session

app = Application()

# this is a session middleware
# that enables you to use request.ctx.session
Session(app, expires=1800)


@app.route('/')
async def index(request, **server):
    session = request.ctx.session

    if session is None or not session.is_logged_in():
        return b'You are not logged in. <a href="/login">Login</a>.'

    return b'Welcome to Dashboard. <a href="/logout">Logout</a>.'


@app.route('/login')
async def login(request, **server):
    session = request.ctx.session

    if request.method == b'POST':
        form_data = await request.form()

        if ('password' in form_data and
                compare_digest(form_data['password'][0], 'mypass')):
            # password match! set current session as logged in:
            session.login()
            # the return value is a `token`, if you want to use
            # `Authorization: Bearer <token>`

            return b'Login success! Go to <a href="/">Dashboard</a>.'

    return (b'<form action="/login" method="post"><div>'
            b'<label for="password">Password:</label> '
            b'<input type="text" name="password" placeholder="mypass" /> '
            b'<input type="submit" value="Login" /></div>'
            b'</form>')


@app.route('/logout')
async def logout(request, response, **server):
    session = request.ctx.session

    session.logout()

    # if you want the session data to be deleted:
    # the session id will also rotate on the next login
    # session.delete()

    response.set_status(302, b'Found')
    response.set_header(b'Location', b'/')

    return b''


if __name__ == '__main__':
    app.run('0.0.0.0', 8000, debug=True, reload=True)

Installing

python3 -m pip install --upgrade tremolo_login

Testing

Just run python3 -m tests.

Or if you also want measurements with coverage:

coverage run -m tests
coverage combine
coverage report
coverage html # to generate html reports

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tremolo_login-1.1.0.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tremolo_login-1.1.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file tremolo_login-1.1.0.tar.gz.

File metadata

  • Download URL: tremolo_login-1.1.0.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tremolo_login-1.1.0.tar.gz
Algorithm Hash digest
SHA256 5de5d84b8d59d9c292628531aaf14439112374e450defeae2a631082511f76e4
MD5 1f2d3c24961c440b552f068130c070e4
BLAKE2b-256 a9ea69fc0d7385dc8908822e5d1494f48ba79d5f9c99c176a7f3eaf312f95d77

See more details on using hashes here.

Provenance

The following attestation bundles were made for tremolo_login-1.1.0.tar.gz:

Publisher: build_and_release.yml on nggit/tremolo-login

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tremolo_login-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: tremolo_login-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tremolo_login-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cb27dfa784e03dc6c454e52714ea17e24adb784d16d8efbbdc5dbc7a7f2b7f11
MD5 2b39f41e10f72067900d6386edb2bda6
BLAKE2b-256 fd9c33450b03613e6ca87bc8bdfec404542d50799b284bfef4805ce703b5679c

See more details on using hashes here.

Provenance

The following attestation bundles were made for tremolo_login-1.1.0-py3-none-any.whl:

Publisher: build_and_release.yml on nggit/tremolo-login

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page