ForcePilot/backend/package/yuxi/channels/adapters/msteams/secret_input.py

54 lines
1.3 KiB
Python
Raw Normal View History

"""Microsoft Teams 密钥输入规范化。
处理用户输入的密钥字符串去除多余空格换行
检测是否已配置合法密钥
"""
from __future__ import annotations
import logging
logger = logging.getLogger(__name__)
def normalize_secret_input(raw: str) -> str:
"""规范化用户输入的密钥字符串。
- 去除首尾空白和换行
- 去除连续的空白行
- 去除不可见字符保留 ASCII 可打印字符
"""
if not raw:
return ""
cleaned = raw.strip()
lines = cleaned.splitlines()
cleaned = " ".join(line.strip() for line in lines if line.strip())
cleaned = "".join(c for c in cleaned if 32 <= ord(c) <= 126 or c in "\t")
return cleaned
def has_configured_secret(value: str, min_length: int = 8) -> bool:
"""检测密钥是否已配置且合法。
Returns True 如果密钥长度 >= min_length 且不是占位符
"""
if not value or len(value) < min_length:
return False
placeholder_lower = value.lower()
placeholders = {
"your_app_password_here",
"replace_with_your_secret",
"changeme",
"password",
"secret",
}
if placeholder_lower in placeholders:
logger.warning("MSTeams: secret appears to be a placeholder value")
return False
return True