# Lilya Simple JWT
<p align="center">
<a href="https://lilya.dev"><img src="https://res.cloudinary.com/dymmond/image/upload/v1707501404/lilya/logo_quiotd.png" alt='Lilya'></a>
</p>
<p align="center">
<em>🚀 Yet another ASGI toolkit that delivers. 🚀</em>
</p>
<p align="center">
<a href="https://github.com/dymmond/lilya/actions/workflows/test-suite.yml/badge.svg?event=push&branch=main" target="_blank">
<img src="https://github.com/dymmond/lilya/actions/workflows/test-suite.yml/badge.svg?event=push&branch=main" alt="Test Suite">
</a>
<a href="https://pypi.org/project/lilya" target="_blank">
<img src="https://img.shields.io/pypi/v/lilya?color=%2334D058&label=pypi%20package" alt="Package version">
</a>
<a href="https://pypi.org/project/lilya" target="_blank">
<img src="https://img.shields.io/pypi/pyversions/lilya.svg?color=%2334D058" alt="Supported Python versions">
</a>
</p>
---
**Documentation**: [https://lilya-simple-jwt.dymmond.com](https://lilya-simple-jwt.dymmond.com) 📚
**Source Code**: [https://github.com/dymmond/lilya-simple-jwt](https://github.com/dymmond/lilya-simple-jwt)
**The official supported version is always the latest released**.
---
This package serves the purpose of facilitating a simple JWT integration of Lilya and any application
that requires JWT.
Based on the standards and security explanations of the [official documentation](https://esmerald.dev/configurations/jwt/),
a simple jwt approach was designed to facilitate the life of the developers and **it is 100% custom**.
Lilya Simple JWT aims to simplify the generation of the `access_token` and `refresh_token` by
providing out of the box mechanisms and views that can be imported directly into your application.
This package uses Pydantic for its own schemas.
## Installation
```shell
$ pip install lilya-simple-jwt
```
## What does it bring
Lilya Simple JWT comes with a simple way of using the package.
1. Via [Include](https://esmerald.dev/routing/routes/#include) where you can simply import directly
the views into your routing system.
This is not all what the package brings to you. It also brings scaffolds for your custom backend
authentication and schemas to represent your token on response. All of this can be found in the
documentation and in more details.
## How does it work
It is very simple, actually. Like everything in Lilya can be done through the [settings](https://esmerald.dev/application/settings/),
this package wouldn't be any different.
In a nutshell, you will need to use the [SimpleJWT](./simple-jwt.md) configuration provided by
the package inside your settings and then import the urls into your package.
## Middleware
The Lilya Simple JWT **does not come** with a middleware for any application and the reason for
this its because you can have your own custom middlewares and your own design without being forced
to use a specific one.
## Quickstart
For the sake of this example, [Edgy](https://esmerald.dev) will be used as ORM but feel free to
use your own and override anything you want and need.
What will we need?
* A [User model](#the-user-model). For this we will be using the [Edgy contrib from Esmerald](https://esmerald.dev/databases/edgy/models/)
since it provides already some out of the box configurations. Feel free to adapt it and use your own
models.
* A [backend authentication](#the-backend-authentication) allowing out user to be validated
for authentication.
* A [backend refresh](./backends.md#the-backend-refresh) that handles with the refresh token of the user
already logged in.
* A [SimpleJWT](#the-simple-jwt-configuration) configuration to be added to the application settings.
Both backend and refresh authentication will be using the default [Token](./token.md) from the
package.
### The user model
Esmerald provides already some out of the box integrations with databases like [Edgy](https://esmerald.dev/databases/edgy/models/)
but the package is not only strict to it. You can change and use whatever it suits you better.
This file will be placed in a `myapp/apps/accounts/models.py`.
```python title="myapp/apps/accounts/models.py"
{!> ../docs_src/quickstart/user.py !}
```
### The backend authentication
The [backend authentication](./backends.md#backend-authentication) does what the names suggests. Validates
and autenticates the user in the system and returns an `access_token` and `refresh_token`.
The backend authentication will be placed inside a `myapps/apps/accounts/backends.py`.
```python title="myapp/apps/accounts/backends.py"
{!> ../docs_src/quickstart/backend_auth.py !}
```
There is a lot to unwrap here right? Well, yes and no.
Although it looks very complex, in fact, it
is only using the [simple_jwt](./simple-jwt.md) settings to populate the necessary fields and get
some defaults from it such as `access_token_lifetime` and `refresh_token_lifetime` as well as
the names that will be displayed in the response for the tokens such as `access_token_name` and
`refresh_token_name`.
The rest is simple python logic to validate the login of a user.
### The backend refresh
The [backend refresh](./backends.md#backend-refresh) as the name suggests, serves the purpose of
refreshing the `access_token` from an existing `refresh_token` only.
The `RefreshAuthentication` on the contrary of the backend authentication, it is already provided
out of the box within **Lilya Simple JWT** but you don't need to use it as well. Everything
can be customisable for your own needs.
The backend refresh will be placed inside a `myapps/apps/accounts/backends.py` as well.
```python title="myapp/apps/accounts/backends.py"
{!> ../docs_src/quickstart/backend_refresh.py !}
```
With the same principle of [backend authentication](#the-backend-authentication), it uses the
[SimpleJWT](./simple-jwt.md) configuration to populate the default values.
### The Simple JWT configuration
This is where we assemble the configurations for the package. The [SimpleJWT](./simple-jwt.md) is
placed inside your application settings file and then used by the application directly.
The configuration will be living inside `myapp/configs/settings.py`.
```python title="myapp/configs/settings.py"
{!> ../docs_src/quickstart/settings.py !}
```
Did you see how simple it was? Basically you just need to implement your own backend and refresh
backends and then import them into the [SimpleJWT](./simple-jwt.md) configuration.
!!! Danger
The settings **must be called** `simple_jwt` or the application will fail to use the
Lilya Simple JWT package.
### Use the Lilya Simple JWT
Now it is time to assemble the application and use the package.
As mentioned at the beginning, there are two different ways.
* Via [Include](#via-include) where you can simply import directly
the views into your routing system.
#### Via Include
This is the simplest approach to almost every application in Esmerald.
```python
{!> ../docs_src/quickstart/via_include.py !}
```
### Starting and accessing the views
With everything assembled, we can now start our application but before
**we need to tell the application to use our custom settings**.
```shell
$ export LILYA_SETTINGS_MODULE=myapp.configs.settings.AppSettings
```
You can now start the application and access the endpoints via **POST**.
* `/auth/signin` - The login view to generate the `access_token` and the `refresh_token`.
* `/auth/refresh-access` - The refresh view to generate the new `access_token` from the `refresh_token`.
### OpenAPI
When Lilya Simple JWT is added into your application, unless specified not to, when `enable_openapi=True`, it will add the
urls automatically to your OpenAPI documentation and so you can also access them via:
* `/docs/swagger` - The default OpenAPI url for the documentation of any Lilya application.
Raw data
{
"_id": null,
"home_page": null,
"name": "lilya-simple-jwt",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": null,
"keywords": "authentication, esmerald, jwt, lilya, lilya-simple-jwt, python, starlette",
"author": null,
"author_email": "Tiago Silva <tiago.silva@dymmond.com>",
"download_url": "https://files.pythonhosted.org/packages/d5/22/ef2a2fbffdd8cc38b55591f9c31af30c243e449d4ce942ffc425c71d2558/lilya_simple_jwt-0.1.0.tar.gz",
"platform": null,
"description": "# Lilya Simple JWT\n\n<p align=\"center\">\n <a href=\"https://lilya.dev\"><img src=\"https://res.cloudinary.com/dymmond/image/upload/v1707501404/lilya/logo_quiotd.png\" alt='Lilya'></a>\n</p>\n\n<p align=\"center\">\n <em>\ud83d\ude80 Yet another ASGI toolkit that delivers. \ud83d\ude80</em>\n</p>\n\n<p align=\"center\">\n<a href=\"https://github.com/dymmond/lilya/actions/workflows/test-suite.yml/badge.svg?event=push&branch=main\" target=\"_blank\">\n <img src=\"https://github.com/dymmond/lilya/actions/workflows/test-suite.yml/badge.svg?event=push&branch=main\" alt=\"Test Suite\">\n</a>\n\n<a href=\"https://pypi.org/project/lilya\" target=\"_blank\">\n <img src=\"https://img.shields.io/pypi/v/lilya?color=%2334D058&label=pypi%20package\" alt=\"Package version\">\n</a>\n\n<a href=\"https://pypi.org/project/lilya\" target=\"_blank\">\n <img src=\"https://img.shields.io/pypi/pyversions/lilya.svg?color=%2334D058\" alt=\"Supported Python versions\">\n</a>\n</p>\n\n---\n\n**Documentation**: [https://lilya-simple-jwt.dymmond.com](https://lilya-simple-jwt.dymmond.com) \ud83d\udcda\n\n**Source Code**: [https://github.com/dymmond/lilya-simple-jwt](https://github.com/dymmond/lilya-simple-jwt)\n\n**The official supported version is always the latest released**.\n\n---\n\nThis package serves the purpose of facilitating a simple JWT integration of Lilya and any application\nthat requires JWT.\n\nBased on the standards and security explanations of the [official documentation](https://esmerald.dev/configurations/jwt/),\na simple jwt approach was designed to facilitate the life of the developers and **it is 100% custom**.\n\nLilya Simple JWT aims to simplify the generation of the `access_token` and `refresh_token` by\nproviding out of the box mechanisms and views that can be imported directly into your application.\n\nThis package uses Pydantic for its own schemas.\n\n## Installation\n\n```shell\n$ pip install lilya-simple-jwt\n```\n\n## What does it bring\n\nLilya Simple JWT comes with a simple way of using the package.\n\n1. Via [Include](https://esmerald.dev/routing/routes/#include) where you can simply import directly\nthe views into your routing system.\n\nThis is not all what the package brings to you. It also brings scaffolds for your custom backend\nauthentication and schemas to represent your token on response. All of this can be found in the\ndocumentation and in more details.\n\n## How does it work\n\nIt is very simple, actually. Like everything in Lilya can be done through the [settings](https://esmerald.dev/application/settings/),\nthis package wouldn't be any different.\n\nIn a nutshell, you will need to use the [SimpleJWT](./simple-jwt.md) configuration provided by\nthe package inside your settings and then import the urls into your package.\n\n## Middleware\n\nThe Lilya Simple JWT **does not come** with a middleware for any application and the reason for\nthis its because you can have your own custom middlewares and your own design without being forced\nto use a specific one.\n\n## Quickstart\n\nFor the sake of this example, [Edgy](https://esmerald.dev) will be used as ORM but feel free to\nuse your own and override anything you want and need.\n\nWhat will we need?\n\n* A [User model](#the-user-model). For this we will be using the [Edgy contrib from Esmerald](https://esmerald.dev/databases/edgy/models/)\nsince it provides already some out of the box configurations. Feel free to adapt it and use your own\nmodels.\n* A [backend authentication](#the-backend-authentication) allowing out user to be validated\nfor authentication.\n* A [backend refresh](./backends.md#the-backend-refresh) that handles with the refresh token of the user\nalready logged in.\n* A [SimpleJWT](#the-simple-jwt-configuration) configuration to be added to the application settings.\n\nBoth backend and refresh authentication will be using the default [Token](./token.md) from the\npackage.\n\n### The user model\n\nEsmerald provides already some out of the box integrations with databases like [Edgy](https://esmerald.dev/databases/edgy/models/)\nbut the package is not only strict to it. You can change and use whatever it suits you better.\n\nThis file will be placed in a `myapp/apps/accounts/models.py`.\n\n```python title=\"myapp/apps/accounts/models.py\"\n{!> ../docs_src/quickstart/user.py !}\n```\n\n### The backend authentication\n\nThe [backend authentication](./backends.md#backend-authentication) does what the names suggests. Validates\nand autenticates the user in the system and returns an `access_token` and `refresh_token`.\n\nThe backend authentication will be placed inside a `myapps/apps/accounts/backends.py`.\n\n```python title=\"myapp/apps/accounts/backends.py\"\n{!> ../docs_src/quickstart/backend_auth.py !}\n```\n\nThere is a lot to unwrap here right? Well, yes and no.\n\nAlthough it looks very complex, in fact, it\nis only using the [simple_jwt](./simple-jwt.md) settings to populate the necessary fields and get\nsome defaults from it such as `access_token_lifetime` and `refresh_token_lifetime` as well as\nthe names that will be displayed in the response for the tokens such as `access_token_name` and\n`refresh_token_name`.\n\nThe rest is simple python logic to validate the login of a user.\n\n### The backend refresh\n\nThe [backend refresh](./backends.md#backend-refresh) as the name suggests, serves the purpose of\nrefreshing the `access_token` from an existing `refresh_token` only.\n\nThe `RefreshAuthentication` on the contrary of the backend authentication, it is already provided\nout of the box within **Lilya Simple JWT** but you don't need to use it as well. Everything\ncan be customisable for your own needs.\n\nThe backend refresh will be placed inside a `myapps/apps/accounts/backends.py` as well.\n\n```python title=\"myapp/apps/accounts/backends.py\"\n{!> ../docs_src/quickstart/backend_refresh.py !}\n```\n\nWith the same principle of [backend authentication](#the-backend-authentication), it uses the\n[SimpleJWT](./simple-jwt.md) configuration to populate the default values.\n\n### The Simple JWT configuration\n\nThis is where we assemble the configurations for the package. The [SimpleJWT](./simple-jwt.md) is\nplaced inside your application settings file and then used by the application directly.\n\nThe configuration will be living inside `myapp/configs/settings.py`.\n\n```python title=\"myapp/configs/settings.py\"\n{!> ../docs_src/quickstart/settings.py !}\n```\n\nDid you see how simple it was? Basically you just need to implement your own backend and refresh\nbackends and then import them into the [SimpleJWT](./simple-jwt.md) configuration.\n\n!!! Danger\n The settings **must be called** `simple_jwt` or the application will fail to use the\n Lilya Simple JWT package.\n\n### Use the Lilya Simple JWT\n\nNow it is time to assemble the application and use the package.\n\nAs mentioned at the beginning, there are two different ways.\n\n* Via [Include](#via-include) where you can simply import directly\nthe views into your routing system.\n\n#### Via Include\n\nThis is the simplest approach to almost every application in Esmerald.\n\n```python\n{!> ../docs_src/quickstart/via_include.py !}\n```\n\n### Starting and accessing the views\n\nWith everything assembled, we can now start our application but before\n**we need to tell the application to use our custom settings**.\n\n```shell\n$ export LILYA_SETTINGS_MODULE=myapp.configs.settings.AppSettings\n```\n\nYou can now start the application and access the endpoints via **POST**.\n\n* `/auth/signin` - The login view to generate the `access_token` and the `refresh_token`.\n* `/auth/refresh-access` - The refresh view to generate the new `access_token` from the `refresh_token`.\n\n### OpenAPI\n\nWhen Lilya Simple JWT is added into your application, unless specified not to, when `enable_openapi=True`, it will add the\nurls automatically to your OpenAPI documentation and so you can also access them via:\n\n* `/docs/swagger` - The default OpenAPI url for the documentation of any Lilya application.\n",
"bugtrack_url": null,
"license": null,
"summary": "The Simple JWT integration with Esmerald",
"version": "0.1.0",
"project_urls": {
"Changelog": "https://lilya-simple-jwt.dymmond.com/release-notes/",
"Documentation": "https://lilya-simple-jwt.dymmond.com",
"Funding": "https://github.com/sponsors/tarsil",
"Homepage": "https://github.com/dymmond/lilya-simple-jwt",
"Source": "https://github.com/dymmond/lilya-simple-jwt"
},
"split_keywords": [
"authentication",
" esmerald",
" jwt",
" lilya",
" lilya-simple-jwt",
" python",
" starlette"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "9353e327456f135cf9567519f9328a441423783480f2290bffef3a53af8c59b5",
"md5": "f1b9f1391b22d2f8d4ae3df14f9f6101",
"sha256": "a3bccec7e02ee53a4316528b4a1ff76188105950743d6cf9a018097266482fa4"
},
"downloads": -1,
"filename": "lilya_simple_jwt-0.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "f1b9f1391b22d2f8d4ae3df14f9f6101",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 13321,
"upload_time": "2025-07-28T10:25:06",
"upload_time_iso_8601": "2025-07-28T10:25:06.468283Z",
"url": "https://files.pythonhosted.org/packages/93/53/e327456f135cf9567519f9328a441423783480f2290bffef3a53af8c59b5/lilya_simple_jwt-0.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "d522ef2a2fbffdd8cc38b55591f9c31af30c243e449d4ce942ffc425c71d2558",
"md5": "4f8278fd3bf4fc7e5494ceb0f42a5d3f",
"sha256": "d89ccb94727029aa4fb9443327e7bde33b5471f2277b14f17eee7d65a0148294"
},
"downloads": -1,
"filename": "lilya_simple_jwt-0.1.0.tar.gz",
"has_sig": false,
"md5_digest": "4f8278fd3bf4fc7e5494ceb0f42a5d3f",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 11201,
"upload_time": "2025-07-28T10:25:07",
"upload_time_iso_8601": "2025-07-28T10:25:07.812250Z",
"url": "https://files.pythonhosted.org/packages/d5/22/ef2a2fbffdd8cc38b55591f9c31af30c243e449d4ce942ffc425c71d2558/lilya_simple_jwt-0.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-07-28 10:25:07",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "sponsors",
"github_project": "tarsil",
"github_not_found": true,
"lcname": "lilya-simple-jwt"
}