bottlejwt


Namebottlejwt JSON
Version 1.0.2 PyPI version JSON
download
home_pagehttps://github.com/agalera/bottlejwt
SummaryJWT plugin for bottle
upload_time2023-12-13 08:18:57
maintainer
docs_urlNone
authorAlberto Galera Jimenez
requires_python
licenseGPL
keywords bottlejwt
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage No coveralls.
            [![pythonversions](https://img.shields.io/pypi/pyversions/bottlejwt.svg)](https://pypi.python.org/pypi/bottlejwt)
[![Codecov](https://img.shields.io/codecov/c/github/agalera/bottlejwt.svg)](https://codecov.io/github/agalera/bottlejwt)
[![Travis](https://img.shields.io/travis/agalera/bottlejwt.svg)](https://travis-ci.org/agalera/bottlejwt)

# bottlejwt
JWT plugin for bottle

## installation

Via pip:
```pip install bottlejwt```

Or clone:
```git clone https://github.com/agalera/bottlejwt.git```


## example server:
```python
import time

from bottlejwt import JwtPlugin
from bottle import Bottle, request


permissions = {"user": 0, "service": 1, "admin": 2}
jwt_secret_key = "s3cr3tk3y!!ch@ng3m3"

def validation(auth, auth_value):
    return permissions[auth["type"]] >= permissions[auth_value]

app = Bottle()
app.install(JwtPlugin(validation, jwt_secret_key, algorithm="HS512"))

@app.post("/login")
def login():
    """
    receive:
    {'client_id': 'user',
     'client_secret': 'password'
    }

    response:
    {'access_token': 'token',
     'type': 'bearer'}

    """
    # example for mongodb
    '''
    user = db.users.find_one(
        {
            "client_id": request.json["client_id"],
            "client_secret": hash_password(request.json["client_secret"]),
        },
        {"_id": False, "client_secret": False},
    )
    '''
    # Any data we consider good, implement a logic instead of doing this
    user = {
        "client_id": request.json["client_id"],
        "type": "user"
    }
    if not user:
        raise HTTPError(403, "Invalid user or password")
    user["exp"] = time.time() + 86400  # 1 day
    return {"access_token": JwtPlugin.encode(user), "type": "bearer"}

@app.get('/jwt_info', auth='user')
def jwt_info(auth):
    return auth

if __name__ == '__main__':
    app.run(host='127.0.0.1', port=9999)
```

## Test by curl:
```bash
curl http://localhost:9988/?access_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ
```
## Example client:
```python
import requests

response = requests.post(
    'http://localhost:9999/login',
    json={
        'client_id': 'user',
        'client_secret': 'password'
    }
).json()

token = f"{response['type']} {response['access_token']}"

# option 1 - Headers
requests.get(
    'http://localhost:9999/jwt_info',
    headers={'Authorization': token}
)
# response
'''
{'client_id': 'user',
 'type': 'user',
 'exp': 1670421559.047136,
 'token': '...'
}
'''

# option 2 - url argument
requests.get(
    f'http://localhost:9999/jwt_info?access_token={response["access_token"]}',
)

'''
{'client_id': 'user',
 'type': 'user',
 'exp': 1670421559.047136,
 'token': '...'
}
'''
```
## Create Token:
```python
from bottlejwt import JwtPlugin

# is a singleton, you only need to initialize once.
# * If you did install () also work
JwtPlugin(validation, 'secret', algorithm='HS256')

print(JwtPlugin.encode({'name': 'pepito'}))
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/agalera/bottlejwt",
    "name": "bottlejwt",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "bottlejwt",
    "author": "Alberto Galera Jimenez",
    "author_email": "galerajimenez@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/15/50/2cb67b6e7787cd9ae20cab62ed6444ac8a1be984b55a389d741f007a8814/bottlejwt-1.0.2.tar.gz",
    "platform": null,
    "description": "[![pythonversions](https://img.shields.io/pypi/pyversions/bottlejwt.svg)](https://pypi.python.org/pypi/bottlejwt)\n[![Codecov](https://img.shields.io/codecov/c/github/agalera/bottlejwt.svg)](https://codecov.io/github/agalera/bottlejwt)\n[![Travis](https://img.shields.io/travis/agalera/bottlejwt.svg)](https://travis-ci.org/agalera/bottlejwt)\n\n# bottlejwt\nJWT plugin for bottle\n\n## installation\n\nVia pip:\n```pip install bottlejwt```\n\nOr clone:\n```git clone https://github.com/agalera/bottlejwt.git```\n\n\n## example server:\n```python\nimport time\n\nfrom bottlejwt import JwtPlugin\nfrom bottle import Bottle, request\n\n\npermissions = {\"user\": 0, \"service\": 1, \"admin\": 2}\njwt_secret_key = \"s3cr3tk3y!!ch@ng3m3\"\n\ndef validation(auth, auth_value):\n    return permissions[auth[\"type\"]] >= permissions[auth_value]\n\napp = Bottle()\napp.install(JwtPlugin(validation, jwt_secret_key, algorithm=\"HS512\"))\n\n@app.post(\"/login\")\ndef login():\n    \"\"\"\n    receive:\n    {'client_id': 'user',\n     'client_secret': 'password'\n    }\n\n    response:\n    {'access_token': 'token',\n     'type': 'bearer'}\n\n    \"\"\"\n    # example for mongodb\n    '''\n    user = db.users.find_one(\n        {\n            \"client_id\": request.json[\"client_id\"],\n            \"client_secret\": hash_password(request.json[\"client_secret\"]),\n        },\n        {\"_id\": False, \"client_secret\": False},\n    )\n    '''\n    # Any data we consider good, implement a logic instead of doing this\n    user = {\n        \"client_id\": request.json[\"client_id\"],\n        \"type\": \"user\"\n    }\n    if not user:\n        raise HTTPError(403, \"Invalid user or password\")\n    user[\"exp\"] = time.time() + 86400  # 1 day\n    return {\"access_token\": JwtPlugin.encode(user), \"type\": \"bearer\"}\n\n@app.get('/jwt_info', auth='user')\ndef jwt_info(auth):\n    return auth\n\nif __name__ == '__main__':\n    app.run(host='127.0.0.1', port=9999)\n```\n\n## Test by curl:\n```bash\ncurl http://localhost:9988/?access_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ\n```\n## Example client:\n```python\nimport requests\n\nresponse = requests.post(\n    'http://localhost:9999/login',\n    json={\n        'client_id': 'user',\n        'client_secret': 'password'\n    }\n).json()\n\ntoken = f\"{response['type']} {response['access_token']}\"\n\n# option 1 - Headers\nrequests.get(\n    'http://localhost:9999/jwt_info',\n    headers={'Authorization': token}\n)\n# response\n'''\n{'client_id': 'user',\n 'type': 'user',\n 'exp': 1670421559.047136,\n 'token': '...'\n}\n'''\n\n# option 2 - url argument\nrequests.get(\n    f'http://localhost:9999/jwt_info?access_token={response[\"access_token\"]}',\n)\n\n'''\n{'client_id': 'user',\n 'type': 'user',\n 'exp': 1670421559.047136,\n 'token': '...'\n}\n'''\n```\n## Create Token:\n```python\nfrom bottlejwt import JwtPlugin\n\n# is a singleton, you only need to initialize once.\n# * If you did install () also work\nJwtPlugin(validation, 'secret', algorithm='HS256')\n\nprint(JwtPlugin.encode({'name': 'pepito'}))\n```\n",
    "bugtrack_url": null,
    "license": "GPL",
    "summary": "JWT plugin for bottle",
    "version": "1.0.2",
    "project_urls": {
        "Homepage": "https://github.com/agalera/bottlejwt"
    },
    "split_keywords": [
        "bottlejwt"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "15502cb67b6e7787cd9ae20cab62ed6444ac8a1be984b55a389d741f007a8814",
                "md5": "903d61bac759ba3327d479ffab8633dc",
                "sha256": "9e64d9d782d1e14bec5e82c944046cbcfc1cb6d410b7e00a0de9bb8fa444bf77"
            },
            "downloads": -1,
            "filename": "bottlejwt-1.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "903d61bac759ba3327d479ffab8633dc",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 4640,
            "upload_time": "2023-12-13T08:18:57",
            "upload_time_iso_8601": "2023-12-13T08:18:57.585916Z",
            "url": "https://files.pythonhosted.org/packages/15/50/2cb67b6e7787cd9ae20cab62ed6444ac8a1be984b55a389d741f007a8814/bottlejwt-1.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-12-13 08:18:57",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "agalera",
    "github_project": "bottlejwt",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "bottlejwt"
}
        
Elapsed time: 0.35764s