|
| 1 | + |
| 2 | +#### Requirements |
| 3 | +- Python >= 3.6 |
| 4 | +- Django >= 2.1 |
| 5 | +- Django-Ninja >= 0.16.1 |
| 6 | +- Ninja-Schema >= 0.12.2 |
| 7 | +- Django-Ninja-Extra >= 0.11.0 |
| 8 | + |
| 9 | +These are the officially supported python and package versions. Other |
| 10 | +versions will probably work. You\'re free to modify the tox config and |
| 11 | +see what is possible. |
| 12 | + |
| 13 | +Installation |
| 14 | +============ |
| 15 | + |
| 16 | +Ninja JWT can be installed with pip: |
| 17 | + |
| 18 | + pip install ninja-jwt |
| 19 | + |
| 20 | +Also, you need to register `SimpleJWTDefaultController` controller to you Django-Ninja api. |
| 21 | +The `SimpleJWTDefaultController` comes with three routes `obtain_token`, `refresh_token` and `verify_token` |
| 22 | + |
| 23 | +``` {.sourceCode .python} |
| 24 | +from ninja_jwt.controller import SimpleJWTDefaultController |
| 25 | +from ninja_extra import NinjaExtraAPI |
| 26 | +
|
| 27 | +api = NinjaExtraAPI() |
| 28 | +api.register_controller(SimpleJWTDefaultController) |
| 29 | +
|
| 30 | +``` |
| 31 | + |
| 32 | +The `SimpleJWTDefaultController` comes with three routes `obtain_token`, `refresh_token` and `verify_token`. |
| 33 | +It is a combination of two subclass `TokenVerificationController` and `TokenObtainPairController`. |
| 34 | +If you wish to customize these routes, you can inherit from these controllers and change its implementation |
| 35 | + |
| 36 | +``` {.sourceCode .python} |
| 37 | +from ninja_jwt.controller import TokenObtainPairController, router |
| 38 | +
|
| 39 | +@router('token', tags=['Auth'] |
| 40 | +class MyCustomController(TokenObtainPairController): |
| 41 | + """obtain_token and refresh_token only" |
| 42 | +... |
| 43 | +api.register_controller(MyCustomController) |
| 44 | +``` |
| 45 | + |
| 46 | +If you wish to use localizations/translations, simply add `ninja_jwt` to |
| 47 | +`INSTALLED_APPS`. |
| 48 | + |
| 49 | +``` {.sourceCode .python} |
| 50 | +INSTALLED_APPS = [ |
| 51 | + ... |
| 52 | + 'ninja_jwt', |
| 53 | + ... |
| 54 | +] |
| 55 | +``` |
| 56 | + |
| 57 | +Usage |
| 58 | +===== |
| 59 | + |
| 60 | +To verify that Ninja JWT is working, you can use curl to issue a couple |
| 61 | +of test requests: |
| 62 | + |
| 63 | +``` {.sourceCode .bash} |
| 64 | +curl \ |
| 65 | + -X POST \ |
| 66 | + -H "Content-Type: application/json" \ |
| 67 | + -d '{"username": "davidattenborough", "password": "boatymcboatface"}' \ |
| 68 | + http://localhost:8000/api/token/pair |
| 69 | +
|
| 70 | +... |
| 71 | +{ |
| 72 | + "access":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX3BrIjoxLCJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiY29sZF9zdHVmZiI6IuKYgyIsImV4cCI6MTIzNDU2LCJqdGkiOiJmZDJmOWQ1ZTFhN2M0MmU4OTQ5MzVlMzYyYmNhOGJjYSJ9.NHlztMGER7UADHZJlxNG0WSi22a2KaYSfd1S-AuT7lU", |
| 73 | + "refresh":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX3BrIjoxLCJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImNvbGRfc3R1ZmYiOiLimIMiLCJleHAiOjIzNDU2NywianRpIjoiZGUxMmY0ZTY3MDY4NDI3ODg5ZjE1YWMyNzcwZGEwNTEifQ.aEoAYkSJjoWH1boshQAaTkf8G3yn0kapko6HFRt7Rh4" |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +You can use the returned access token to prove authentication for a |
| 78 | +protected view: |
| 79 | + |
| 80 | +``` {.sourceCode .bash} |
| 81 | +curl \ |
| 82 | + -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX3BrIjoxLCJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiY29sZF9zdHVmZiI6IuKYgyIsImV4cCI6MTIzNDU2LCJqdGkiOiJmZDJmOWQ1ZTFhN2M0MmU4OTQ5MzVlMzYyYmNhOGJjYSJ9.NHlztMGER7UADHZJlxNG0WSi22a2KaYSfd1S-AuT7lU" \ |
| 83 | + http://localhost:8000/api/some-protected-view/ |
| 84 | +``` |
| 85 | + |
| 86 | +When this short-lived access token expires, you can use the longer-lived |
| 87 | +refresh token to obtain another access token: |
| 88 | + |
| 89 | +``` {.sourceCode .bash} |
| 90 | +curl \ |
| 91 | + -X POST \ |
| 92 | + -H "Content-Type: application/json" \ |
| 93 | + -d '{"refresh":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX3BrIjoxLCJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImNvbGRfc3R1ZmYiOiLimIMiLCJleHAiOjIzNDU2NywianRpIjoiZGUxMmY0ZTY3MDY4NDI3ODg5ZjE1YWMyNzcwZGEwNTEifQ.aEoAYkSJjoWH1boshQAaTkf8G3yn0kapko6HFRt7Rh4"}' \ |
| 94 | + http://localhost:8000/api/token/refresh/ |
| 95 | +
|
| 96 | +... |
| 97 | +{"access":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX3BrIjoxLCJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiY29sZF9zdHVmZiI6IuKYgyIsImV4cCI6MTIzNTY3LCJqdGkiOiJjNzE4ZTVkNjgzZWQ0NTQyYTU0NWJkM2VmMGI0ZGQ0ZSJ9.ekxRxgb9OKmHkfy-zs1Ro_xs1eMLXiR17dIDBVxeT-w"} |
| 98 | +``` |
0 commit comments