{"id":15567226,"url":"https://github.com/iml1111/django-jwt-extended","last_synced_at":"2025-04-24T00:03:09.325Z","repository":{"id":44910194,"uuid":"449548132","full_name":"iml1111/django-jwt-extended","owner":"iml1111","description":"An open source Django extension that provides JWT Authentication","archived":false,"fork":false,"pushed_at":"2024-10-02T20:47:15.000Z","size":40,"stargazers_count":7,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-24T00:02:53.842Z","etag":null,"topics":["authentication","django","hacktoberfest","jwt","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/iml1111.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-01-19T04:36:07.000Z","updated_at":"2024-10-03T09:52:13.000Z","dependencies_parsed_at":"2025-03-06T22:41:32.771Z","dependency_job_id":null,"html_url":"https://github.com/iml1111/django-jwt-extended","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iml1111%2Fdjango-jwt-extended","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iml1111%2Fdjango-jwt-extended/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iml1111%2Fdjango-jwt-extended/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iml1111%2Fdjango-jwt-extended/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iml1111","download_url":"https://codeload.github.com/iml1111/django-jwt-extended/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250535088,"owners_count":21446507,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["authentication","django","hacktoberfest","jwt","python"],"created_at":"2024-10-02T17:10:26.583Z","updated_at":"2025-04-24T00:03:09.288Z","avatar_url":"https://github.com/iml1111.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Django-JWT-Extended\n\n![Python versions](https://img.shields.io/pypi/pyversions/django-jwt-extended) ![License](https://img.shields.io/badge/license-MIT-green) ![Release](https://img.shields.io/pypi/v/django-jwt-extended)\n\nImplement JWT authentication with Django quickly and easily!\n**Inspired by [flask-jwt-extended](https://github.com/vimalloc/flask-jwt-extended).**\n\n\n\n# Installation\n\n**Pip**: `pip install django-jwt-extended`\n\nAfter that, add `django_jwt_extended` to `INSTALLED_APPS` settings.\n\n```\nINSTALLED_APPS = [\n    ...\n    'django_jwt_extended',\n]\n```\n\n\n\n# Get Started\n\n`django-jwt-extended` makes it easy and simple to create authentication feature.\n\n## Startup your project\n\n```shell\n$ pip install django\n$ pip install django-jwt-extended\n$ django-admin startproject example .\n$ python manage.py migrate\n$ python manage.py createsuperuser\n```\n\n## Edit your views\n\n```python\n# views.py\nfrom django.http import JsonResponse\nfrom django_jwt_extended import jwt_required\nfrom django_jwt_extended import create_access_token\nfrom django_jwt_extended import get_jwt_identity\n\ndef login(request):\n    \"\"\"Create JWT Token API\"\"\"\n    return JsonResponse({\n        \"access_token\": create_access_token(identity=\"iml\"),\n    })\n\n@jwt_required()\ndef user(request):\n    \"\"\"JWT Authentication API\"\"\"\n    identity = get_jwt_identity(request) # \"iml\"\n    return JsonResponse({'id': identity,})\n```\n\n\n\n# Advanced Usage\n\nThis section goes into more detail about django-jwt-extended.\n\n## Return with refresh token\n\nIf you want to return not only the access token but also the refresh token, you can use it as follows. `Identity` is input as an argument to generate tokens. \n\nThis `Identity` can contain any object that **can be serialized as json**, and is stored in \"sub\" of JWT Schema.\n\n```python\nfrom django_jwt_extended import create_access_token\nfrom django_jwt_extended import create_refresh_token\n\n# Login and issue tokens\ndef login(request):\n    return JsonResponse({\n        \"access_token\": create_access_token(\"iml\"),\n        'refresh_token': create_refresh_token('iml'),\n    })\n```\n\n## Refresh Token Authentication\n\nWhen you want to perform authentication through refresh token, Set the refresh argument to `True` as shown below.\n\n```python\n# Refresh tokens\n@jwt_required(refresh=True) # refresh token check\ndef refresh(request):\n    identity = get_jwt_identity(request)\n    return JsonResponse({\n        \"access_token\": create_access_token(identity),\n        'refresh_token': create_refresh_token(identity),\n    })\n```\n\n## Parse JWT Payload\n\nThere are two ways to get the contents of jwt token. These are `get_jwt_identity` and `get_jwt`. \n\n `get_jwt_identity` returns the identity value given when creating the token as it is.\n\n`get_jwt` returns the full payload that decoded the jwt token.\n\n```python\n# Authentication access token\n@jwt_required()\ndef user(request):\n    identity = get_jwt_identity(request)\n    payload = get_jwt(request)\n    return JsonResponse({\n        'id': identity,\n        'raw_jwt': payload,\n    })\n```\n\n## Optional Authentication\n\n If the optional argument is `True`, the verification step is passed even if the corresponding token does not exist. However, in this case, even if **identity or jwt payload** is called, `None` is returned.\n\n```python\n# Optional Login example\n@jwt_required(optional=True)\ndef user_optional(request):\n    identity = get_jwt_identity(request)\n    return JsonResponse({'id': identity})\n```\n\n## Custom Decorator Pattern\n\nIf it is cumbersome to implement the `jwt_required` logic repeatedly every time, you can implement a custom decorator as shown below. This is only an example, and more various methods may exist.\n\n```python\n# Authentication access token with Decorator\ndef login_required(func):\n    @jwt_required()\n    def wrapper(request, **path):\n        identity = get_jwt_identity(request)\n        request.META['logined_identity'] = identity # before request\n        response = func(request, **path)\n        request.META.pop('logined_identity') # after request\n        return response\n    return wrapper\n\n@login_required\ndef decorator_user(request):\n    identity = request.META['logined_identity']\n    payload = get_jwt(request)\n    return JsonResponse({\n        'id': identity,\n        'raw_jwt': payload,\n    })\n```\n\n\n\n# Configuration\n\nEven if you don't configure anything, your app works.\n\nBut in `settings.py` in your app, You can customize your app through the following settings. \n\nHere's a good sample.\n\n```python\n# settings.py\n\nSECRET_KEY = \"super-secret\"\n\nJWT_CONFIG = {\n  'ALGORITHM': 'HS256',\n  'LOCATION': ['headers'],\n  'ACCESS_TOKEN_EXPIRES': timedelta(days=2),\n  'REFRESH_TOKEN_EXPIRES': timedelta(days=30),\n  'JWT_NOT_FOUND_MSG': {'msg': \"can't find JWT token.\"}\n}\n...\n```\n\n\n\n## SECRET_KEY\n\nThis is the secret key setting that Django supports by default. \n\n`Django-jwt-extended` also, the key is used when encoding/decoding JWT.\n\n## JWT_CONFIG\n\n`JWT_CONFIG` is a setting added for `django_jwt_extended`. \n\nAdditional settings can be added as follows in the form of a dictionary.\n\n### ALGORITHM\n\n`ALGORITHM: \"HS256\" `\n\n- Default: `HS256`\n- Allowed_values: `HS256`\n\nSelect the encode/decode algorithm to issue tokens. (Currently only '**HS256**' is supported)\n\n### LOCATION\n\n`LOCATION: [\"headers\", ...]`\n\n- default: `[\"headers\"]`\n- allowed_values: `headers`, `cookies`\n\nThis setting determines where to collect the Tokens. The thing to note is that **input is received as a list, not as a single string**. You can pass in a list to check more then one location, for example `[\"headers\", \"cookies\"]`. The order of the list sets the precedence of where JWTs will be looked for.\n\n- **headers**\n\nFor headers, the header name is fixed to **\"Authorization\"**, and the token format is **\"Bearer [token]\"**.\n\n- **cookies**\n\n In the cookie, you can directly specify **the cookie name for the access token** and **the cookie name for the refresh token.**\n\n### ACCESS_TOKEN_COOKIE_NAME\n\n`ACCESS_TOKEN_COOKIE_NAME: access_token`\n\n- Default: `access_token`\n- Allowed_types: `string`\n\nThe name of the cookie that will store the access token.\n\n### REFRESH_TOKEN_COOKIE_NAME\n\n`REFRESH_TOKEN_COOKIE_NAME: refresh_token`\n\n- Default: `refresh_token`\n- Allowed_types: `string`\n\nThe name of the cookie that will store the refresh token.\n\n### ACCESS_TOKEN_EXPIRES\n\n`ACCESS_TOKEN_EXPIRES: 60 * 24 * 2 # 2days`\n\n- Default: `60 * 24 * 2`\n- Allowed_types: `integer`, `datetime.timedelta`\n\nHow long an access token should be valid before it expires. This can be a a number of seconds (`Integer`).\n\n### REFRESH_TOKEN_EXPIRES\n\n`REFRESH_TOKEN_EXPIRES: 60 * 24 * 30 # 1month`\n\n- Default: `60 * 24 * 30`\n- Allowed_types: `integer`, `datetime.timedelta`\n\nHow long a refresh token should be valid before it expires. This can be a number of seconds (`Integer`).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiml1111%2Fdjango-jwt-extended","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fiml1111%2Fdjango-jwt-extended","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiml1111%2Fdjango-jwt-extended/lists"}