Build a secure REST API authentication system with Python and FastAPI using JWT and RSA encryption. This step-by-step tutorial covers user registration, login with encrypted passwords, JWT-protected routes, and full user management (CRUD) backed by MySQL. Includes setup, security best practices, and Postman testing. Perfect for building professional, production-ready APIs.
📌 Tech Stack
-
Python 3.10+
-
FastAPI
-
MySQL
-
JWT (python-jose)
-
RSA Encryption (Public/Private Key)
-
bcrypt
-
Postman
🧠 Authentication Flow
-
Client encrypts password using RSA Public Key.
-
Server decrypts password using RSA Private Key.
-
Password verified using bcrypt.
-
JWT token issued.
-
Protected routes secured with JWT dependency.
📦 Requirements
-
Python 3.10+
-
MySQL 8+
-
OpenSSL
-
Postman
1️⃣ Create Python Project
Open the project folder in your preferred editor (e.g., VS Code).
2️⃣ Install Dependencies
3️⃣ Create Project Folder Structure
| Folder | Purpose |
|---|---|
app/api | API endpoints |
app/api/auth | Auth related APIs |
app/api/users | User Management CRUD APIs |
app/middleware | JWT auth middleware |
app/utils | JWT & RSA helper utilities |
app/db | Database connection |
storage/rsa | RSA key storage |
4️⃣ Create Required Files
Create these files (you can use your editor or terminal):
5️⃣ Configure .gitignore
Add:
⚠️ Never commit your private RSA key!
6️⃣ Set Environment Variables (.env)
7️⃣ MySQL Database Setup
Using your MySQL client (like DBeaver, MySQL Workbench, or CLI), run:
8️⃣ Generate RSA Keys (Using OpenSSL)
-
public.pem→ Used on frontend for password encryption. -
private.pem→ Used on backend for password decryption.
9️⃣ MySQL Connection (app/db/mysql.py)
🔟 JWT Helper (app/utils/jwt.py)
1️⃣1️⃣ RSA Helper (app/utils/rsa.py)
1️⃣2️⃣ Register API (app/api/auth/register.py)
1️⃣3️⃣ Login API (app/api/auth/login.py)
1️⃣4️⃣ JWT Middleware (app/middleware/auth.py)
1️⃣5️⃣ Profile API (app/api/auth/profile.py)
1️⃣6️⃣ Logout API (app/api/auth/logout.py)
1️⃣7️⃣ User Management CRUD (app/api/users/user_crud.py)
1️⃣8️⃣ Main App (app/main.py)
▶️ Run Application
Base URL:
http://localhost:8000/api
🚀 Postman Testing
1. Register
-
Method:
POST -
URL:
http://localhost:8000/api/auth/register -
Body (JSON):
2. Login
-
Method:
POST -
URL:
http://localhost:8000/api/auth/login -
Body (JSON):
Copy the returned "access_token".
3. Access Protected APIs
Set header:
-
Authorization:Bearer YOUR_ACCESS_TOKEN
4. Get Profile
-
Method:
GET -
URL:
http://localhost:8000/api/auth/profile
5. List Users
-
Method:
GET -
URL:
http://localhost:8000/api/users
6. Get Single User
-
Method:
GET -
URL:
http://localhost:8000/api/users/{user_id}
7. Update User
-
Method:
PUT -
URL:
http://localhost:8000/api/users/{user_id} -
Body (JSON):
8. Delete User
-
Method:
DELETE -
URL:
http://localhost:8000/api/users/{user_id}
✅ Production Best Practices
-
Use HTTPS everywhere.
-
Never commit
private.pem. -
Rotate RSA keys periodically.
-
Use short-lived JWT tokens.
-
Rate-limit login attempts.
-
Secure
.envfiles properly.
🎯 Final Result
You now have a secure, production-ready Python FastAPI REST API with:
-
🔐 RSA-encrypted passwords
-
🔑 JWT Authentication
-
🛡 MySQL backend
-
👤 User management CRUD operations
🔗 Source Code
Want the full source code?
👉 Download the complete Python JWT example from my GitHub repository

