Build a powerful authentication and authorization system with FastAPI using JWT tokens, RSA encryption, and a full Admin CRUD interface.
This step-by-step tutorial covers securing your FastAPI backend with JWT signed by RSA keys, protecting routes based on user roles, and implementing admin features to manage users.
This tutorial walks you through creating a production-ready REST API using FastAPI with:
-
RSA-encrypted login passwords
-
JWT authentication with roles (admin, user)
-
Admin-only user management (CRUD)
-
MySQL database integration
-
Token blacklist for logout support
-
Full Postman testing
Tech Stack
-
Python 3.10+
-
FastAPI
-
MySQL 8+
-
JWT (
python-jose) -
RSA Encryption (OpenSSL)
-
bcrypt (password hashing)
-
Postman (API testing)
Authentication & Authorization Flow Overview
-
Client encrypts login password with RSA Public Key.
-
Server decrypts password with RSA Private Key.
-
Password is verified using bcrypt.
-
JWT token issued containing user role.
-
Middleware validates token and user role.
-
Admin-only APIs protected by role.
-
Logout adds tokens to a blacklist to revoke access.
Step 1: Create Project and Setup Virtual Environment
Step 2: Install Dependencies
Step 3: Project Structure
Step 4: Configure .env file
Create .env in your project root:
Step 5: MySQL Database Setup
Run these SQL commands:
Step 6: Generate RSA Keys
Generate RSA key pair:
-
Use
public.pemon frontend to encrypt passwords. -
Use
private.pemon backend to decrypt.
Warning: Never commit
private.pemto source control!
Step 7: Setup MySQL Connection
Create app/db/mysql.py:
Step 8: JWT Helper
Create app/utils/jwt.py:
Step 9: RSA Helper
Create app/utils/rsa.py:
Step 🔟 Middleware for Authentication and Role-based Access
Create app/middleware/auth.py:
Step 1️⃣1️⃣ Register API
Create app/api/auth/register.py:
Step 1️⃣2️⃣ Login API
Create app/api/auth/login.py:
Step 1️⃣3️⃣ Profile API
Create app/api/auth/profile.py:
Step 1️⃣4️⃣ Logout API with Token Blacklist
Create app/api/auth/logout.py:
Step 1️⃣5️⃣ Admin User Management CRUD
Create app/api/users/user_crud.py:
Step 1️⃣6️⃣ Main Application
Create app/main.py:
Step 1️⃣7️⃣ Run Application
Start the server:
API Base URL:
Step 1️⃣8️⃣ Postman Testing (Step-by-Step)
-
Register Admin
POST /api/auth/register
Body (raw JSON):
-
Login Admin
POST /api/auth/login
Body (raw JSON):
Copy access_token from response.
-
List Users (Admin only)
GET /api/users
Headers:
-
Test Normal User Access
Try GET /api/users with a non-admin user token → should return 403 Forbidden
-
Get User Profile
GET /api/auth/profile
Headers:
Accessible by all logged-in users.
-
Logout
POST /api/auth/logout
Headers:
The token will be blacklisted and cannot be used further.
Production Best Practices
-
Use HTTPS
-
Never commit private RSA keys
-
Use short-lived JWT tokens
-
Rotate RSA keys regularly
-
Secure your
.envfile -
Rate-limit login endpoints
Final Result
You now have a secure, role-based FastAPI REST API with:
-
RSA-encrypted login
-
JWT authentication with role control
-
Admin-only user management
-
Token blacklisting (logout)
-
MySQL backend
-
Fully tested with Postman
🔗 Source Code
Want the full source code?
👉 Download the complete Python JWT example from my GitHub repository

