Skip to main content

Plugin que implementa autenticação com JWT para o aplicações Bottle.

Instalação.

É necessário instalar bottle (por se tratar de um plugin para aplicações com bottle) em seguida pode realizar a instalação do plugin.

$ pip install bottle
$ pip install jwt_token

Caso aconteça algum erro com relação a importação de módulo do python-jose ou pycrypto, pode instalar os pacotes abaixo também.

$ pip install python-jose
$ pip install pycrypto

Exemplo de uso simples:

from bottle import Bottle
from JWTPlugin import JWTPlugin, auth_required, BaseAuth
from peewee import (SqliteDatabase, Model, CharField,
                    DateField, DoesNotExist) # aqui utilizei o peewee, mas a escolha é livre.
from passlib.hash import pbkdf2_sha512 as hsh # aqui utilizei o passlib, mas a escolha é livre.
from hashlib import md5


db = SqliteDatabase(":memory:")


class Users(Model):
    """Classe usuário.
    """
    name = CharField(max_length=50)
    last_name = CharField(max_length=50)
    email = CharField(max_length=200)
    password = CharField(max_length=300)
    birthday = DateField()

    def gen_hash(self):
        _secret = md5("123456".encode()).hexdigest()
        _password = md5(self.password.encode()).hexdigest()
        self.password = hsh.hash(_secret+_password)

    def verify(self, password):
        _secret = md5("123456".encode()).hexdigest()
        _password = md5(password.encode()).hexdigest()
        return hsh.verify(_secret+_password, self.password)

    class Meta:
        database = db


class Auth(BaseAuth):
    """Classe para autenticação.
    Precisa conter um método estático chamado authenticate e outro
    chamado get_user.

    Os parametros de authenticate ficam a critério do método post.

    O padrão é receber uma requisição POST enviando dados no formato JSON.
    Esses dados são empacotados no argumento kwargs do método authenticate.

    para identificar o usuário é necessário realizar a consulta utilizando
    um ID.
    """

    @staticmethod
    def authenticate(*args, **kwargs):
        """Método para autenticação, aqui utilizei uma classe chamada
        Users implementada com o ORM peewee e uma simples regra de 
        autenticação apresentada pelo Eduardo Mendes.
        link: https://www.youtube.com/watch?v=ieGA91ExOH0

        Returns:
            Users: dicionário contendo id para gerar o token.
            OBS: é necessário possuir um atributo "id" para gerar o token.
        """
        try:
            if "email" in kwargs and "password" in kwargs:
                user = Users.get(Users.email==kwargs['email'])
                if user.verify(kwargs['password']):
                    return user
            return None
        except DoesNotExist as err:
            return {"erro": f"Usuário {kwargs['email']} não localizado"}

    @staticmethod
    def get_user(*args, **kwargs):
        """Classe para resgatar usuario autenticado
        utilizando a decodificação de um token.

        Args:
            user_id ([int]): identificador do usuário.

        Returns:
            Users: retorna usuário autenticado pelo Token.
        """
        try:
            user = Users.get_by_id(kwargs["id"])
            if user:
                return user
            return None
        except DoesNotExist as err:
            return {}

class AdminAuth(BaseAuth):
    """Classe para autenticação.
    Precisa conter um método estático chamado authenticate e outro
    chamado get_user.

    Os parametros de authenticate ficam a critério do método post.

    O padrão é receber uma requisição POST enviando dados no formato JSON.
    Esses dados são empacotados no argumento kwargs do método authenticate.

    para identificar o usuário é necessário realizar a consulta utilizando
    um ID.
    """

    @staticmethod
    def authenticate(*args, **kwargs):
        """Método para autenticação, aqui utilizei uma classe chamada
        Users implementada com o ORM peewee e uma simples regra de 
        autenticação apresentada pelo Eduardo Mendes.
        link: https://www.youtube.com/watch?v=ieGA91ExOH0

        Returns:
            Users: dicionário contendo id para gerar o token.
            OBS: é necessário possuir um atributo "id" para gerar o token.
        """
        try:
            if "name" in kwargs:
                user = Users.get(Users.name == kwargs['name'])
                return user
            return None
        except DoesNotExist as err:
            return {"erro": f"Usuário {kwargs['email']} não localizado"}

    @staticmethod
    def get_user(*args, **kwargs):
        """Classe para resgatar usuario autenticado
        utilizando a decodificação de um token.

        Args:
            user_id ([int]): identificador do usuário.

        Returns:
            Users: retorna usuário autenticado pelo Token.
        """
        try:
            if "name" in kwargs:
                user = Users.get(Users.name == kwargs['name'])
                return user
            return None
        except DoesNotExist as err:
            return {"erro": f"Usuário {kwargs['email']} não localizado"}



app = Bottle()

config = {'model': Auth, 'endpoint': '/auth', 'auth_name': "auth", 'refresh_name': 'refresh'}

jwt = JWTPlugin("a-string-secret-at-least-256-bits-long", config=config, payload=['id', 'email', 'name'])

app.install(jwt)


@auth_required
@app.get("/user")
def index(user):
    return f"Usuario: {user.name}"


if __name__ == "__main__":
    Users.create_table()
    user = Users(name="Afonso", last_name="Medeiros", email="afonso@afonso.com", password="123456", birthday="2020-01-01")
    user.gen_hash()
    user.save()
    
    app.run(debug=True, reloader=True)

Teste efetuado utilizando httpie, para instalar basta executar pip install httpie

# /auth é o endpoint padrão para autenticar.
$ http POST http://127.0.0.1:8080/auth email=afonso@afonso.com password=123456

HTTP/1.0 200 OK
Content-Length: 128
Content-Type: application/json
Date: Sun, 12 Jul 2020 03:59:19 GMT
Server: WSGIServer/0.2 CPython/3.8.2

{
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiZXhwIjoxNTk0NTI5OTU5fQ.1hmo_Fkg7-OKs0VDDil6dUnDv5FvmIkIYAjl6nzewwY"
}

$ http http://127.0.0.1:8080/user Authorization:eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiZXhwIjoxNTk0NTI5OTU5fQ.1hmo_Fkg7-OKs0VDDil6dUnDv5FvmIkIYAjl6nzewwY

HTTP/1.0 200 OK
Content-Length: 15
Content-Type: text/html; charset=UTF-8
Date: Sun, 12 Jul 2020 04:01:09 GMT
Server: WSGIServer/0.2 CPython/3.8.2

Usuario: Afonso

É necessário implementar uma classe para a autenticação do usuário, essa classe vai carregar as regras de autenticação de cada aplicação, no caso do exemplo Criei uma classe Auth que seguindo a regra implementa dois métodos estáticos authenticate e get_user. Os métodos podem receber qualquer parametro para autenticação o que flexibiliza como realizar o POST com os dados do usuário e recuperar estes dados.

O método authenticate deve retornar um objeto que possua como atributos os itens passados como payloads, caso nenhum payload seja passado será feita a tentativa de utilizar o atributo id para gerar o token, caso a classe também não possua um atributo id uma exceção será levantada.

Com a mudança feita não é mais obrigatório o uso de uma classe peewee Model, agora pode utilizar o ORM que quiser e implementar sua própria forma de authenticar um usuário.

Exemplo com duas formas de autenticação:

import bottle
from jwt_bottle import JWTPlugin, auth_required, BaseAuth
from peewee import (SqliteDatabase, Model, CharField,
                    DateField, DoesNotExist) # aqui utilizei o peewee, mas a escolha é livre.
from passlib.hash import pbkdf2_sha512 as hsh # aqui utilizei o passlib, mas a escolha é livre.
from hashlib import md5


db = SqliteDatabase(":memory:")


class Users(Model):
    """Classe usuário.
    """
    name = CharField(max_length=50)
    last_name = CharField(max_length=50)
    email = CharField(max_length=200)
    password = CharField(max_length=300)
    birthday = DateField()

    def gen_hash(self):
        _secret = md5("123456".encode()).hexdigest()
        _password = md5(self.password.encode()).hexdigest()
        self.password = hsh.hash(_secret+_password)

    def verify(self, password):
        _secret = md5("123456".encode()).hexdigest()
        _password = md5(password.encode()).hexdigest()
        return hsh.verify(_secret+_password, self.password)

    class Meta:
        database = db


class Auth(BaseAuth):
    """Classe para autenticação.
    Precisa conter um método estático chamado authenticate e outro
    chamado get_user.

    Os parametros de authenticate ficam a critério do método post.

    O padrão é receber uma requisição POST enviando dados no formato JSON.
    Esses dados são empacotados no argumento kwargs do método authenticate.

    para identificar o usuário é necessário realizar a consulta utilizando
    um ID.
    """

    @staticmethod
    def authenticate(*args, **kwargs):
        """Método para autenticação, aqui utilizei uma classe chamada
        Users implementada com o ORM peewee e uma simples regra de 
        autenticação apresentada pelo Eduardo Mendes.
        link: https://www.youtube.com/watch?v=ieGA91ExOH0

        Returns:
            Users: dicionário contendo id para gerar o token.
            OBS: é necessário possuir um atributo "id" para gerar o token.
        """
        try:
            if "email" in kwargs and "password" in kwargs:
                user = Users.get(Users.email==kwargs['email'])
                if user.verify(kwargs['password']):
                    return user
            return None
        except DoesNotExist as err:
            return {"erro": f"Usuário {kwargs['email']} não localizado"}

    @staticmethod
    def get_user(*args, **kwargs):
        """Classe para resgatar usuario autenticado
        utilizando a decodificação de um token.

        Args:
            user_id ([int]): identificador do usuário.

        Returns:
            Users: retorna usuário autenticado pelo Token.
        """
        try:
            user = Users.get_by_id(kwargs["id"])
            if user:
                return user
            return None
        except DoesNotExist as err:
            return {}

class AdminAuth(BaseAuth):
    """Classe para autenticação.
    Precisa conter um método estático chamado authenticate e outro
    chamado get_user.

    Os parametros de authenticate ficam a critério do método post.

    O padrão é receber uma requisição POST enviando dados no formato JSON.
    Esses dados são empacotados no argumento kwargs do método authenticate.

    para identificar o usuário é necessário realizar a consulta utilizando
    um ID.
    """

    @staticmethod
    def authenticate(*args, **kwargs):
        """Método para autenticação, aqui utilizei uma classe chamada
        Users implementada com o ORM peewee e uma simples regra de 
        autenticação apresentada pelo Eduardo Mendes.
        link: https://www.youtube.com/watch?v=ieGA91ExOH0

        Returns:
            Users: dicionário contendo id para gerar o token.
            OBS: é necessário possuir um atributo "id" para gerar o token.
        """
        try:
            if "name" in kwargs:
                user = Users.get(Users.name == kwargs['name'])
                return user
            return None
        except DoesNotExist as err:
            return {"erro": f"Usuário {kwargs['email']} não localizado"}

    @staticmethod
    def get_user(*args, **kwargs):
        """Classe para resgatar usuario autenticado
        utilizando a decodificação de um token.

        Args:
            user_id ([int]): identificador do usuário.

        Returns:
            Users: retorna usuário autenticado pelo Token.
        """
        try:
            if "name" in kwargs:
                user = Users.get(Users.name == kwargs['name'])
                return user
            return None
        except DoesNotExist as err:
            return {"erro": f"Usuário {kwargs['email']} não localizado"}



app = bottle.Bottle()

configs = [
    {'model': Auth, 'endpoint': '/auth', 'auth_name': "auth", 'refresh_name': 'refresh'},
    {'model': AdminAuth, 'endpoint': '/admin/auth', 'auth_name': 'admin_auth', 'refresh_name': 'refresh_admin'}
]

jwt = JWTPlugin("abcde", configs=configs, payload=['id', 'email', 'name'])

app.install(jwt)


@auth_required
@app.get("/user")
def index(user):
    return f"Usuario: {user.name}"


if __name__ == "__main__":
    Users.create_table()
    user = Users(name="Afonso", last_name="Medeiros", email="afonso@afonso.com", password="123456", birthday="2020-01-01")
    user.gen_hash()
    user.save()
    
    app.run(debug=True, reloader=True)

Testes usando httpie:

$ http POST http://127.0.0.1:8080/auth email=afonso@afonso.com password=123456HTTP/1.0 200 OKContent-Length: 768Content-Type: application/jsonDate: Wed, 31 Mar 2021 20:51:49 GMT
Server: W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VHOUROZzZDSGR3dzlMVnE1S1B3Z2VBZkJxbi1FNUg3dndFVjczQ2RQbEkifQ.JK42DVJM4DVaNI5qmFzj92sAoUPC5Ir8Ya4H5L86aPU
HTTP/1.0 200 OKContent-Length: 15
Content-Type: text/html; charset=UTF-8
Date: Wed, 31 Mar 2021 20:52:43 GMT
Server: WSGIServer/0.2 CPython/3.8.5

Usuario: Afonso

$ http POST http://127.0.0.1:8080/auth/refresh Refresh-Jwt:eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiZW1haWwiOiJhZm9uc29AYWZvbnNvLmNvbSIsIm5hbWUiOiJBZm9uc28iLCJ0b2tlbiI6ImV5SmhiR2NpT2lKSVV6STFOaUlzSW5SNWNDSTZJa3BYVkNKOS5leUpwWkNJNk1Td2laVzFoYVd3aU9pSmhabTl1YzI5QVlXWnZibk52TG1OdmJTSXNJbTVoYldVaU9pSkJabTl1YzI4aWZRLjVHOUROZzZDSGR3dzlMVnE1S1B3Z2VBZkJxbi1FNUg3dndFVjczQ2RQbEkifQ.JK42DVJM4DVaNI5qmFzj92sAoUPC5Ir8Ya4H5L86aPU
HTTP/1.0 200 OK
Content-Length: 768
Content-Type: application/json
Date: Wed, 31 Mar 2021 21:02:49 GMT
Server: WSGIServer/0.2 CPython/3.8.5

{
    "refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiZW1haWwiOiJhZm9uc29AYWZvbnNvLmNvbSIsIm5hbWUiOiJBZm9uc28iLCJ0b2tlbiI6ImV5SmhiR2NpT2lKSVV6STFOaUlzSW5SNWNDSTZJa3BYVkNKOS5leUpwWkNJNk1Td2laVzFoYVd3aU9pSmhabTl1YzI5QVlXWnZibk52TG1OdmJTSXNJbTVoYldVaU9pSkJabTl1YzI4aWZRLjVHOUROZzZDSGR3dzlMVnE1S1B3Z2VBZkJxbi1FNUg3dndFVjczQ2RQbEkifQ.JK42DVJM4DVaNI5qmFzj92sAoUPC5Ir8Ya4H5L86aPU",
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiZW1haWwiOiJhZm9uc29AYWZvbnNvLmNvbSIsIm5hbWUiOiJBZm9uc28iLCJ0b2tlbiI6ImV5SmhiR2NpT2lKSVV6STFOaUlzSW5SNWNDSTZJa3BYVkNKOS5leUpwWkNJNk1Td2laVzFoYVd3aU9pSmhabTl1YzI5QVlXWnZibk52TG1OdmJTSXNJbTVoYldVaU9pSkJabTl1YzI4aWZRLjVHOUROZzZDSGR3dzlMVnE1S1B3Z2VBZkJxbi1FNUg3dndFVjczQ2RQbEkifQ.JK42DVJM4DVaNI5qmFzj92sAoUPC5Ir8Ya4H5L86aPU"
}

Código baseado na lib: https://github.com/agile4you/bottle-jwt

Metadata

Release files for jwt_bottle 2025.3.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jwt_bottle 2025.3.2
File Size Uploaded Provenance Actions
jwt_bottle-2025.3.2.tar.gz 6.3 kB Without provenance Details for jwt_bottle-2025.3.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for jwt_bottle 2025.3.2
File Interpreter ABI Platform Actions
jwt_bottle-2025.3.2-py3-none-any.whl Python 3 none any Details for jwt_bottle-2025.3.2-py3-none-any.whl

Total release size: 13.2 kB

Release files / jwt_bottle-2025.3.2.tar.gz

Download URL jwt_bottle-2025.3.2.tar.gz
Size 6.3 kB
Tags Source
SHA-256 checksum
How to use checksums
34834fac26831681bc9de7f207a6c319108666b80f7a59147eba63ecdb9c0cca
BLAKE2b-256 checksum
How to use checksums
bfde8431f5df9f79fac012520403a2b0c44c89f436ec0fd0823074445fe5993a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.1 CPython/3.13.2 Windows/10

Release files / jwt_bottle-2025.3.2-py3-none-any.whl

Download URL jwt_bottle-2025.3.2-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
de9caa50b061526e691dad91d918ffed375d658f529b5a44caf1e3ace76871fd
BLAKE2b-256 checksum
How to use checksums
d5f5c1da1093e3f78e0a4398a153fc6757a5bd0d4b93405d9c5eafa7fdbab097
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.1 CPython/3.13.2 Windows/10

Security for jwt_bottle 2025.3.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

2025.3.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page