Skip to main content

JWT for Bottle

Project description

BottleJWT

JWT Plugin for Bottle. Adds functionality to protect routes via JWT and adds options for role based authentication.

Installation

Source

wget https://raw.githubusercontent.com/thepure12/bottle-jwt/main/src/bottle_jwt/bottle_jwt.py

Usage

Install Plugin

from bottle import Bottle
from bottle_jwt import JWTPlugin

app = Bottle()
plugin = JWTPlugin(jwt_key="changeme")
app.install(plugin)
@app.route("/token", method="POST")
def token():
    pass

Getting a Token

The default route for getting a token is "/token". Token routes only except POST requests and will pass post data as keyword arguments into the authentication (auth) function. The auth function creates the JWT payload which is encoded and returned to the requester.

$ curl localhost/token
{"message": "Method Not Allowed, use POST for authentication"}
$ curl -X POST localhost/token
{"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlcyI6WyJndWVzdCJdLCJleHAiOjE2ODMxMjcxODd9.5OkXOdRGbloRr4oI2pjeJoBqQvSaE-pkuaZoaAtKKSU"}

Custom Auth

def myAuth(**kwargs):
    token = {
        "exp": datetime.utcnow() + timedelta(minutes=5),
        "roles": ["user", "admin"]
    }
    return token

plugin = JWTPlugin(jwt_key="changeme", auth_func=myAuth)

Failed authenetication can be handled by raising an AuthFailed exception.

from bottle_jwt import 
def myAuth(username, password):
    if myHash(username, password) != expected_hash:
        raise AuthFailed()

Additional Token Paths

It might be useful to have multiple token paths to enable the use of multiple auth functions. Each token path is mapped to one auth function but one auth function may be mapped to multiple token paths.

plugin = JWTPlugin(jwt_key="changeme")
plugin.addTokenPath("token2")
plugin.addTokenPath("token3", auth_func=myAuth)

Protecting Routes

Protecting a route is handled via Bottle's route config. This simplest way to protect a route is to add a "roles" option to a route's config and set it to True. Roles can all be set to a list of strings (the roles required to access the route) or a callable that returns a boolean or list of strings.

@route("/protected1", roles=True)
def protected():
    pass

@route("/protected2", roles=["user", "admin"])
def protected():
    pass

@route("/protected3", roles=myRolesFunc)
def protected():
    pass

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

bottle-jwt3-0.0.1.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

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

bottle_jwt3-0.0.1-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file bottle-jwt3-0.0.1.tar.gz.

File metadata

  • Download URL: bottle-jwt3-0.0.1.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.5

File hashes

Hashes for bottle-jwt3-0.0.1.tar.gz
Algorithm Hash digest
SHA256 9a306f456d4a1ebcadcc704b39596770fbb56273716e3d97393aaa8df2b5f2da
MD5 80ec222ebb0096d1002037b0bba1ee1d
BLAKE2b-256 21f2b56fd906c387f0990b488a43a52788ed37de37368bf1a28093e07dc5084d

See more details on using hashes here.

File details

Details for the file bottle_jwt3-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: bottle_jwt3-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.5

File hashes

Hashes for bottle_jwt3-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6649b16f2bedf1d375d732adb7c3de1631cc781210cc38840e591ce034a60b75
MD5 1c8cfe65de4fc47e291654a560d1eddb
BLAKE2b-256 2270246e214ab6f4ddd506873ad23bc3b2e0e7b38059d62efc12b921667a787e

See more details on using hashes here.

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