wip: 2026-08-11 工作树快照(docs/memory/h5.py/scripts 等 447 项未评审改动,安全提交到 feat 分支)
This commit is contained in:
+28
-13
@@ -249,8 +249,13 @@ async def get_oauth_authorize_url(
|
||||
Returns:
|
||||
Dict: 统一响应格式,包含 authorize_url 字段
|
||||
"""
|
||||
# 后端第二道防线:非企微环境拒绝授权
|
||||
_require_wework_ua(request)
|
||||
# 检测请求来源:企微 WebView vs 外部浏览器
|
||||
ua = request.headers.get("user-agent", "")
|
||||
is_wework = bool(_WEWORK_UA_RE.search(ua))
|
||||
|
||||
# 生产环境且非企微时,仍需要 UA 校验记录日志(但不再拒绝,改为走扫码登录)
|
||||
if is_production() and not is_wework:
|
||||
logger.info(f"[OAuth] 外部浏览器访问 authorize,UA={ua[:80]},将返回扫码登录URL")
|
||||
|
||||
corp_id = settings.wecom_corp_id
|
||||
|
||||
@@ -266,17 +271,27 @@ async def get_oauth_authorize_url(
|
||||
default_origin = settings.cors_origins_list[0] if settings.cors_origins_list else "https://localhost"
|
||||
encoded_redirect = quote(f"{default_origin}/itdesk/", safe="")
|
||||
|
||||
# 构造企微OAuth2静默授权URL(snsapi_base:用户无感知)
|
||||
# 企业微信 OAuth2 地址(注意是 open.work.weixin.qq.com)
|
||||
authorize_url = (
|
||||
f"https://open.work.weixin.qq.com/connect/oauth2/authorize"
|
||||
f"?appid={corp_id}"
|
||||
f"&redirect_uri={encoded_redirect}"
|
||||
f"&response_type=code"
|
||||
f"&scope=snsapi_base"
|
||||
f"&state=STATE"
|
||||
f"#wechat_redirect"
|
||||
)
|
||||
if is_wework or not is_production():
|
||||
# 企微内(或非生产环境):静默授权(snsapi_base,用户无感知)
|
||||
authorize_url = (
|
||||
f"https://open.work.weixin.qq.com/connect/oauth2/authorize"
|
||||
f"?appid={corp_id}"
|
||||
f"&redirect_uri={encoded_redirect}"
|
||||
f"&response_type=code"
|
||||
f"&scope=snsapi_base"
|
||||
f"&state=STATE"
|
||||
f"#wechat_redirect"
|
||||
)
|
||||
else:
|
||||
# 外部浏览器 + 生产环境:扫码登录(qrConnect)
|
||||
agent_id = getattr(settings, "wecom_agent_id", "1000002")
|
||||
authorize_url = (
|
||||
f"https://open.work.weixin.qq.com/wwopen/sso/qrConnect"
|
||||
f"?appid={corp_id}"
|
||||
f"&agentid={agent_id}"
|
||||
f"&redirect_uri={encoded_redirect}"
|
||||
f"&state=STATE"
|
||||
)
|
||||
|
||||
return success_response(data={"authorize_url": authorize_url})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user