Python API Security App – FastAPI + JWT + CustomTkinter GUI | FuzzuTech Project
Demo :
Click Video πππ
π Features :
-
Embedded YouTube Short
-
Code snippets (highlight important parts)
-
Viral hashtags
-
SEO keywords like "FastAPI JWT authentication Python", "Python GUI API Client"
Code :
import customtkinter as ctk
import requests
from fastapi import FastAPI, Depends, HTTPException
from fastapi.security import OAuth2PasswordRequestForm
import threading
import uvicorn
# ==============================
# ⚡ FastAPI Backend (Embedded)
# ==============================
app = FastAPI()
valid_users = {
"admin": "admin123",
"user1": "pass123",
"developer": "dev456"
}
@app.post("/token")
async def login(form_data: OAuth2PasswordRequestForm = Depends()):
if valid_users.get(form_data.username) == form_data.password:
return {"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."}
else:
raise HTTPException(status_code=400, detail="Incorrect username or password")
def run_fastapi():
uvicorn.run(app, host="127.0.0.1", port=8000)
# ==============================
# ✅ GUI Client Code
# ==============================
API_URL = "http://127.0.0.1:8000/token"
def authenticate_user(username, password):
data = {"username": username, "password": password}
try:
response = requests.post(API_URL, data=data)
return response.json()
except Exception as e:
return {"detail": f"Connection error: {str(e)}"}
class APISecurityApp(ctk.CTk):
def __init__(self):
super().__init__()
self.title("π API Security – JWT Auth + Rate Limiting")
self.geometry("400x450")
self.resizable(False, False)
# Header
self.label = ctk.CTkLabel(self, text="π Secure API Authentication Client", font=("Arial", 20, "bold"))
self.label.pack(pady=25)
# Username Entry
self.username_entry = ctk.CTkEntry(self, placeholder_text="Enter Username")
self.username_entry.pack(pady=15, ipadx=10, ipady=8)
# Password Entry
self.password_entry = ctk.CTkEntry(self, placeholder_text="Enter Password", show="*")
self.password_entry.pack(pady=15, ipadx=10, ipady=8)
# Authenticate Button
self.auth_button = ctk.CTkButton(self, text="Authenticate π", command=self.authenticate)
self.auth_button.pack(pady=25, ipadx=10, ipady=8)
# Result Display
self.result_label = ctk.CTkLabel(self, text="", wraplength=450, justify="center")
self.result_label.pack(pady=15)
def authenticate(self):
username = self.username_entry.get().strip()
password = self.password_entry.get().strip()
if not username or not password:
self.result_label.configure(text="❗ Please fill both Username & Password fields.", text_color="red")
return
result = authenticate_user(username, password)
if "access_token" in result:
self.result_label.configure(
text=f"✅ Authentication Successful!\n\nAccess Token:\n{result['access_token']}",
text_color="green"
)
else:
self.result_label.configure(
text=f"❌ Authentication Failed:\n{result.get('detail', 'Unknown error')}",
text_color="red"
)
# ==============================
# ✅ Run Everything
# ==============================
if __name__ == "__main__":
# Start FastAPI in background thread
threading.Thread(target=run_fastapi, daemon=True).start()
# Set CTk Theme
ctk.set_appearance_mode("Dark") # Options: "System", "Light", "Dark"
ctk.set_default_color_theme("blue") # Options: "blue", "green", "dark-blue"
# Start GUI
app = APISecurityApp()
app.mainloop()
Comments
Post a Comment