A production-grade template demonstrating JWT authentication and Role-Based Access Control (RBAC) with Flask.
---
---
The ser on by default.
Set SECRET_KEY and PORT for production.
---
| Method | Path | Description |
|--------|-------------|----------------------|
| GET | /health | Health-check |
| POST | /register | Create a new account |
| POST | /login | Obtain a JWT |
| Method | Path | Required Role |
|--------|----------------|------------------|
| POST | /logout | user or admin |
| GET | /profile | user or admin |
| GET | /users | admin only |
| DELETE | /user/<id> | admin only |
All protected endpoints require the header:
---
---
| Concern | Solution |
|---|---|
| Password storage | bcrypt with 12 rounds (configurable). Salt is embedded in the hash — ne. |
| JWT signing | HS256. In production, switch to RS256 so the public key can without exposing the signing key. |
| Token expiry | 1 hour TTL hardcoded in auth.py → TOKEN_TTL. |
| Token re | In-memory blacklist keyed by jti (UUID4). Replace with Redis in production so all API workers share state. |
| Username enumeration | /login returns the *same* error for "user not found" and "wrong password". is always called to prevent timing attacks. |
| Pri escalation | stacks on — callers must pass *both* checks. |
| Defensi | Every 401/403 event is written to logs/security.log with timestamp, IP, method, and path. |
| Self-deletion | Admins cannot delete their own account (prevents lock-out). |
| Principle of Least Pri | Default role on registration is 'user'. Ele to 'admin' must be explicit. |
---
Tüm 6 gerekli göre 19 test:
sub, role, jti, exp)GET /profile her iki role açık; DELETE /user/<id> sadece admin → kullanıcılar için 403security.log oluşturulur ve UNAUTHORIZED etiketi içerir---
Görev 1 — Güvenli Şifre Depolama (auth.py, database.py)
bcrypt, SQLite'a dokunmadan önce her şifreyi 12 turlu hash ile kodlar. Düz metin dize hiçbir zaman isteğin süresinden daha uzun tutulmaz. Test tarafından doğrulanır; ı satırı okunur ve $2b$ ile başladığı doğrulanır.
Görev 2 — JWT Düzenleme (auth.py → issue_token, routes/auth_routes.py → /login)
Başarılı oturum açma işleminde, sub, role, jti (benzersiz token ID), iat ve exp içeren HS256 imzalı bir JWT döndürülür. Zamanlama tabanlı kullanıcı adı numaralandırmasını önlemek için bilinmeyen kullanıcı adları için de sahte bir hash çalıştırılır.
Görev 3 — Token Doğrulama (auth.py → ) dekoratörü Bearer token'ini çıkartır, imzayı çözer ve doğrular, son kullanma tarihini kontrol eder ve kara listeyi kontrol eder — tümü rota işlevi çalışmadan önce. Çözülen yük, Flask'ın g.current_user nesnesine yerleştirilir.
Görev 4 — Role Tabanlı Yönlendirme (auth.py → , routes/user_routes.py)GET /profile kullanır (her iki rol geçer). DELETE /user/<id> ve GET /users kullanır; bu, üzerine yığınlanır ve role claim'i "admin" olmayan çağıranları 403 ile reddeder.
Görev 5 — Token İptal Etme (auth.py → re / _blacklist, routes/auth_routes.py → /logout)POST /logout çağıranın mevcut token'ından jti okur ve bunu bellek içi bir sözlüğe ekler. decode_token() sonraki her istekte bu sözlüğü kontrol eder — token hemen ölü olur, exp claim'inden bağımsız olarak.
Görev 6 — Saı Günlüğü (middleware.py)log_unauthorized() yardımcısı logs/security.log dosyasına ISO-8601 zaman damgası, önem se, istemci IP'si, HTTP yöntemi, rota yazar. , 'ın after_request kancası tarafından çağrılır; bu nedenle hiçbir 401/403 olayı kaçırılmaz.
开发语言:Python
Star 数:1
该项目属于防护思路方向的开源资源,代码结构清晰,适合安全研究人员学习参考。项目围绕JWT authentication and Role-Based Access Control (RBAC) with Flask — bcrypt password hashing, token blacklisting, and defensive security logging.展开,提供了可运行的实现,对理解相关安全机制、工具原理与攻防技术具有较高的研究价值。
从技术实现角度看,此类项目的核心在于对底层原理的深入理解与工程化落地。开发者在研究过程中可以结合官方文档与社区讨论,梳理项目的初始化流程、核心模块职责与数据流转,逐步掌握其中的技术细节与设计思路。
1. 下载源码压缩包,解压到本地环境。
2. 根据项目类型准备对应的运行环境与依赖。
3. 按照 README 中的步骤完成配置与部署。
4. 在隔离的测试环境中运行验证,避免影响生产系统。
本资源来源于开源社区,仅供安全研究、技术学习与合法授权范围内的攻防演练使用,请勿用于任何非法或侵害他人权益的用途。使用本项目产生的一切后果由使用者自行承担。
| 资源标题 | secure-flask-api - JWT authentication and Role-Based Access Control ( |
| 所属分类 | 防护思路 |
| 发布作者 | admin |
| 发布时间 | 2026-10-06 00:29:03 |
| 下载权限 | 免费下载 |
| 附件数量 | 2 个下载链接 |
| 浏览次数 | 352 |
| 讨论数量 | 0 |
| 主题状态 | 正常讨论中 |