Files
sso/docs/iap-demo/backend.py
T
adminandClaude Code 1d984181b5 docs(iap): IAP (Identity-Aware Proxy) 接入指南 + 演示文件
- docs/iap.md 写清 3 段配置 + 5 个 X-Auth-* 头 + Bearer/Cookie 双源 + 6 个踩坑
- docs/iap-demo/ 留 nginx.conf.template 与回显 backend.py,可复用

实测: ipass-sso-v0.7.0 .so 加载自签 RSA key,挂到 30089 端口
      /iap-demo/ 走 sso_iap on; 注入 X-Auth-User/Sub/Email/Groups/Token

Co-Authored-By: Claude Code <[email protected]>
2026-10-09 21:40:09 +08:00

59 lines
2.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""
IAP Demo Backend — 回显所有收到的请求头,让 IAP 注入的 X-Auth-* 头肉眼可见。
"""
import json
import sys
from flask import Flask, request
app = Flask(__name__)
def page(title, body):
return f"""<!doctype html><meta charset=utf-8><title>{title}</title>
<style>body{{font-family:ui-monospace,Menlo,monospace;padding:20px;max-width:1100px}}
pre{{background:#0f172a;color:#e2e8f0;padding:14px;border-radius:8px;overflow:auto}}
h2{{margin-top:24px}}
.tag{{display:inline-block;padding:2px 8px;border-radius:4px;background:#dcfce7;color:#166534;font-size:12px}}
.auth{{background:#fef3c7;color:#92400e}}
.iam{{background:#dbeafe;color:#1e3a8a}}
table{{border-collapse:collapse;width:100%}}
td{{padding:4px 10px;border-bottom:1px solid #1e293b;font-family:ui-monospace,monospace}}
td.k{{color:#94a3b8;width:280px}}
</style><body><h1>IAP Demo Backend <span class=tag>:18200</span></h1>{body}"""
@app.route("/", defaults={"p": ""})
@app.route("/<path:p>")
def echo(p):
# 重点:把所有 X-Auth-* 头挑出来 + 全部 headers
auth_headers = {k: v for k, v in request.headers.items() if k.lower().startswith("x-auth-")}
all_headers = dict(request.headers)
# 身份总结
summary = {
"X-Auth-User": auth_headers.get("X-Auth-User", "(missing)"),
"X-Auth-Sub": auth_headers.get("X-Auth-Sub", "(missing)"),
"X-Auth-Email": auth_headers.get("X-Auth-Email", "(missing)"),
"X-Auth-Groups": auth_headers.get("X-Auth-Groups", "(missing)"),
"X-Auth-Token": (auth_headers.get("X-Auth-Token", "(missing)")[:48] + "…") if auth_headers.get("X-Auth-Token") else "(missing)",
}
# 关键判定
identity_injected = bool(auth_headers.get("X-Auth-User"))
badge = '<span class=tag>✓ IAP 已注入身份</span>' if identity_injected else '<span class=tag style="background:#fee2e2;color:#991b1b">✗ IAP 未注入(未授权或未配置)</span>'
body = f"""
<p>{badge} &nbsp; 请求路径: <code>/{p}</code></p>
<h2>1) 身份总结(X-Auth-* 头)</h2>
<table>{''.join(f'<tr><td class=k>{k}</td><td>{v}</td></tr>' for k, v in summary.items())}</table>
<h2>2) 完整请求头(Flask request.headers)</h2>
<pre>{json.dumps(all_headers, indent=2, ensure_ascii=False)}</pre>
"""
return page("IAP echo", body)
if __name__ == "__main__":
print(f"[iap-backend] listening on http://127.0.0.1:18200", file=sys.stderr)
app.run(host="127.0.0.1", port=18200, debug=False)