3.2 KiB
3.2 KiB
memberapi.ose.tw 後端架構(FastAPI)
1. 目標與邊界
- 網域:
memberapi.ose.tw - 角色:會員中心後端真相來源(User + Permission)
- 範圍:
- user upsert(以
authentik_sub為跨系統主鍵) - permission grant/revoke
- permission snapshot 提供給其他系統
- user upsert(以
- 不在本服務處理:
- Authentik OIDC 流程頁與 UI
- 前端互動邏輯
2. 技術棧
- Python 3.12
- FastAPI
- SQLAlchemy 2.0
- PostgreSQL(psycopg)
- Pydantic Settings
3. 後端目錄(已建立)
backend/app/main.pybackend/app/api/internal.pyadmin.py
backend/app/core/config.pybackend/app/db/session.pybackend/app/models/user.pypermission.pyapi_client.py
backend/app/repositories/users_repo.pypermissions_repo.py
backend/app/security/api_client_auth.pybackend/scripts/init_schema.sqlbackend/.env.examplebackend/.env.production.example
4. 資料模型
usersid,authentik_sub(unique),email,display_name,is_active, timestamps
permissionsid,user_id,scope_type,scope_id,module,action,created_at- unique constraint:
(user_id, scope_type, scope_id, module, action)
api_clients(由docs/API_CLIENTS_SQL.sql建立)client_key,api_key_hash,status, allowlist, expires/rate-limit 欄位
5. API 設計(MVP)
- 健康檢查
GET /healthz
- 使用者路由(Bearer token)
GET /meGET /me/permissions/snapshot- Bearer token 由 Authentik JWT + JWKS 驗證,並以
sub自動 upsert user
- 內部路由(系統對系統)
POST /internal/users/upsert-by-subGET /internal/permissions/{authentik_sub}/snapshotPOST /internal/authentik/users/ensure- header:
X-Internal-Secret
- 管理路由(後台/API client)
POST /admin/permissions/grantPOST /admin/permissions/revoke- headers:
X-Client-Key,X-API-Key
6. 安全策略
admin路由強制 API client 驗證:- client 必須存在且
status=active expires_at未過期api_key_hash驗證(支援sha256:<hex>與 bcrypt/argon2)- allowlist 驗證(origin/ip/path)
- client 必須存在且
internal路由使用X-Internal-Secret做服務間驗證me路由使用 Authentik Access Token 驗證:- 使用
AUTHENTIK_JWKS_URL或AUTHENTIK_ISSUER推導 JWKS - 可選
AUTHENTIK_AUDIENCE驗證 aud claim
- 使用
- Authentik Admin 整合:
- 使用
AUTHENTIK_BASE_URL + AUTHENTIK_ADMIN_TOKEN - 可透過
/internal/authentik/users/ensure建立或更新 Authentik user
- 使用
- 建議上線前:
- 將
.env範本中的明文密碼改為部署平台 secret - API key 全部改為 argon2/bcrypt hash
- 將
7. 與其他系統資料流
- mkt/admin 後端登入後,以 token
sub呼叫/internal/users/upsert-by-sub - 權限調整走
/admin/permissions/grant|revoke - 需要授權判斷時,呼叫
/internal/permissions/{sub}/snapshot - mkt 系統可本地快取 snapshot,並做定時補償
8. 下一階段(建議)
- 加入 Alembic migration
- 為 permission/action 加 enum 與驗證規則
- 增加 audit log(誰在何時授權/撤銷)
- 加入 rate-limit 與可觀測性(metrics + request id)