帮助开发与运维设计兼顾本地顺畅和远程安全的认证与 TLS 接入方案。
复制安装指令,让 AI 自动完成配置 · 推荐新手
请帮我安装 askskill 上的 "auth-tls-patterns" 技能: 1. 下载 https://raw.githubusercontent.com/microsoft/amplifier-bundle-skills/main/skills/auth-tls-patterns/SKILL.md 2. 保存为 ~/.claude/skills/auth-tls-patterns/SKILL.md 3. 装好后重载技能,告诉我可以用了
请为一个内部 Web 服务设计认证方案:本地 localhost 访问无需登录,远程访问必须使用自动生成的 Bearer Token,并说明中间件判断逻辑、环境变量配置和安全注意事项。
一套清晰的认证设计,包含本地绕过、远程令牌校验、配置建议与风险提示。
我正在部署一个对外提供 API 的服务,请给出自动 TLS 证书配置方案,包括开发环境与生产环境的差异、证书签发方式、续期策略,以及如何避免本地开发被 HTTPS 流程阻碍。
可执行的 TLS 配置思路,覆盖证书自动化、本地开发体验和生产安全要求。
请整理一份适用于微服务的认证与 TLS 最佳实践,重点说明:哪些接口可在 localhost 放行、何时启用 token 自动生成、如何默认启用 HTTPS,以及常见误配置风险。
一份结构化最佳实践清单,便于团队统一实现认证与加密传输策略。
Problem: Your tool serves a web UI. Locally it should just work — no passwords, no login screens. Remotely it needs real authentication and HTTPS. You don't want to configure either manually.
Approach: Socket-level localhost bypass (unforgeable), cascading auth strategies with auto-generation, and a TLS setup cascade that picks the best available method automatically.
Pattern proven in production across multiple Python CLI tools and web services.
The single most important auth decision: localhost connections skip all auth checks. But you MUST use the socket-level client IP, not HTTP headers:
_LOCALHOST_ADDRS = {"127.0.0.1", "::1"}
async def dispatch(self, request: Request, call_next) -> Response:
# client.host is the socket-level IP — cannot be forged by the client
client_host = request.client.host if request.client else ""
if client_host in _LOCALHOST_ADDRS:
return await call_next(request)
This is unforgeable — unlike X-Forwarded-For or the Host header, request.client.host comes from the TCP connection's source address. A remote attacker cannot set it to 127.0.0.1.
A simpler approach checks at the CLI level:
auth_required = resolved_host != "127.0.0.1" and not no_auth
Resolve auth mode through a fallback chain:
def _resolve_auth() -> tuple[str, str]:
"""Fallback chain for non-localhost:
1. PAM available → ("pam", "")
2. MY_TOOL_PASSWORD env → ("password", <env value>)
3. ~/.config/my-tool/password file → ("password", <file value>)
4. Auto-generate → ("password", <generated>)
"""
Auto-generation writes a random password to a file with restricted permissions:
def generate_and_save_password() -> str:
pw = secrets.token_urlsafe(20)
path = get_password_path()
_config_dir() # ensures dir exists with mode 0700
path.write_text(pw + "\n")
path.chmod(0o600)
return pw
A simpler bearer token approach:
def ensure_token() -> str:
"""Return the existing auth token or generate and persist a new one."""
if TOKEN_FILE.exists():
token = TOKEN_FILE.read_text().strip()
if token:
return token
token = secrets.token_urlsafe(32)
TOKEN_FILE.parent.mkdir(parents=True, exist_ok=True)
TOKEN_FILE.write_text(token + "\n")
TOKEN_FILE.chmod(0o600)
return token
The middleware checks Bearer tokens on protected paths:
class AuthMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request, call_next):
if not request.app.state.auth_required:
return await call_next(request)
path = request.url.path
if not _is_protected(path):
return await call_next(request)
auth_header = request.headers.get("authorization", "")
if auth_header.startswith("Bearer "):
token = auth_header[7:]
if token == request.app.state.auth_token:
return await call_next(request)
return JSONResponse(status_code=401, content={"detail": "Unauthorized"})
Implement a three-tier TLS cascade, trying the best option first:
Tailscale available + cert domains? → Real Let's Encrypt cert, auto-renewed
mkcert installed? → Locally-trusted cert, no browser warnings
Neither? → Self-signed via Python cryptography library
Self-signed generation is pure Python — no openssl binary needed:
def generate_self_signed(cert_path, key_path, hostnames=None, days_valid=3650):
from cryptography import x509
from cryptography.hazmat.primitives import hashes, serialization
…
帮助你安全编排 Docker 容器任务,并搭建可复现的开发运行环境
帮助你调研、规划并并行执行大规模代码变更,让多个代理分别提交 PR。
以资深工程师视角审视架构、遗留重构与工具选型,给出务实建议。
用多模型视觉能力分析图片内容、提取文字并回答图像相关问题。
帮助开发者设计安全持久的配置与状态文件管理模式,兼顾默认值合并和崩溃恢复。
帮助开发者构建含生命周期管理、WebSocket与SSE的 HTTP 服务模式
帮助开发者在认证、输入处理、密钥和敏感功能开发中进行系统安全审查
为本地服务快速生成可公开访问的 HTTPS、TCP 或 UDP 隧道地址。
通过 Auth0 完成用户认证,并代表用户安全调用受保护 API 的 MCP 工具
帮助用户扫描 SSL/TLS 配置、签发证书并监控到期风险。
帮助用户为 Power Pages 站点配置登录认证、权限控制与身份提供商接入。
帮助开发者将无鉴权的远程 MCP 服务快速部署到 Cloudflare Workers 并供客户端调用。