This project demonstrates how to implement RSA-based JWT authentication in a Django project using djangorestframework-simplejwt. It provides a secure token authentication system with public/private key encryption and a clean user registration/profile management API.
- Django 5+
- Django REST Framework
- SimpleJWT (with RSA)
- Redis (for caching user profiles)
- PostgreSQL (or SQLite for dev)
- Python 3.11+
Instead of using a shared secret (HS256), this project uses RSA public/private key pair (RS256) for signing JWTs:
- 🔑 Private Key: Signs the token (kept secret in your backend).
- 🧾 Public Key: Verifies the token (can be shared with other services/microservices).
This ensures asymmetric encryption, better for microservice communication.
git clone https://github.com/shoaibatmaca/RSA_Django.git
cd RSA_Djangopython -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
pip install -r requirements.txtpython generate_keys.pyThis creates keys/private.pem and keys/public.pem.
✅ Don't commit these keys to GitHub! Add them to .gitignore.
python manage.py makemgrations
python manage.py migratepython manage.py runserver📬 API Endpoints Endpoint Method Auth Required Description /api/users/register/ POST -Register new user /api/token/ POST -Get JWT access/refresh /api/token/refresh/ POST -Refresh JWT access token /api/users/profile/ GET/PUT -View or update profile
🧠 Redis Caching The /api/users/profile/ view is cached with a TTL of 300s (5 minutes).
Reduces DB hits on repeated profile fetches.
🛑 .gitignore Suggestions Make sure these files are ignored:
keys/private.pem keys/public.pem
generate_keys.py
SIMPLE_JWT = {
"ALGORITHM": "RS256",
"SIGNING_KEY": open(BASE_DIR / "keys/private.pem").read(),
"VERIFYING_KEY": open(BASE_DIR / "keys/public.pem").read(),
...
}This project is open-source and free to use under the MIT License.