feat(auth): 加 STORY_WEB_NO_AUTH 本地免鉴权开关(默认关,线上不受影响)
设 STORY_WEB_NO_AUTH=1 时跳过登录、署名记「本地」、不校验 STORY_WEB_USERS——仅本地开发用。 线上不设此变量 → 行为完全不变,照常每人一把口令(安全设计未动)。
This commit is contained in:
@ -39,6 +39,10 @@ _STATIC_DIR = os.path.join(_HERE, "static")
|
|||||||
COOKIE = "story_auth"
|
COOKIE = "story_auth"
|
||||||
SESSION_DAYS = 30
|
SESSION_DAYS = 30
|
||||||
|
|
||||||
|
# 免鉴权开关:仅本地开发用。设 STORY_WEB_NO_AUTH=1 时跳过登录(不要在线上设)。
|
||||||
|
# 线上不设此变量 → 行为不变,照常要口令。
|
||||||
|
NO_AUTH = (os.environ.get("STORY_WEB_NO_AUTH") or "").strip().lower() in ("1", "true", "yes", "on")
|
||||||
|
|
||||||
|
|
||||||
def _load_users():
|
def _load_users():
|
||||||
"""解析 STORY_WEB_USERS="名字1:口令1,名字2:口令2"。返回 {口令: 名字}。
|
"""解析 STORY_WEB_USERS="名字1:口令1,名字2:口令2"。返回 {口令: 名字}。
|
||||||
@ -46,6 +50,8 @@ def _load_users():
|
|||||||
未配置/格式错/口令过短/口令重复 → 直接拒绝启动(宁可起不来,不可弱口令裸奔)。
|
未配置/格式错/口令过短/口令重复 → 直接拒绝启动(宁可起不来,不可弱口令裸奔)。
|
||||||
口令即身份:登录只输口令,服务端按口令认人,updated_by 由服务端填写。
|
口令即身份:登录只输口令,服务端按口令认人,updated_by 由服务端填写。
|
||||||
"""
|
"""
|
||||||
|
if NO_AUTH:
|
||||||
|
return {} # 免鉴权模式(仅本地):无需口令,不校验 STORY_WEB_USERS
|
||||||
raw = (os.environ.get("STORY_WEB_USERS") or "").strip()
|
raw = (os.environ.get("STORY_WEB_USERS") or "").strip()
|
||||||
if not raw:
|
if not raw:
|
||||||
sys.exit('[story-web] 未配置 STORY_WEB_USERS,拒绝启动。'
|
sys.exit('[story-web] 未配置 STORY_WEB_USERS,拒绝启动。'
|
||||||
@ -101,6 +107,9 @@ def _session_user(request):
|
|||||||
@app.middleware("http")
|
@app.middleware("http")
|
||||||
async def auth_guard(request: Request, call_next):
|
async def auth_guard(request: Request, call_next):
|
||||||
path = request.url.path
|
path = request.url.path
|
||||||
|
if NO_AUTH: # 本地免鉴权:全部放行,署名记为「本地」
|
||||||
|
request.state.user = "本地"
|
||||||
|
return await call_next(request)
|
||||||
# 放行登录、静态资源、根
|
# 放行登录、静态资源、根
|
||||||
if path.startswith("/api/") and path != "/api/login":
|
if path.startswith("/api/") and path != "/api/login":
|
||||||
user = _session_user(request)
|
user = _session_user(request)
|
||||||
|
|||||||
Reference in New Issue
Block a user