- 新增 plugin-sso 模块(零新增 crate):scim.rs(SCIM 2.0)、cas.rs(CAS 1.0/2.0)、 ldap.rs(LDAP/AD,手写 BER/ASN.1 Simple Bind)、sync.rs(钉钉/企微/飞书手动触发同步)、 reset.rs(自助密码重置)、geo.rs(登录 IP 归属地) - api.rs 兜底路由按 scim→cas→reset→sync 分发兄弟模块;h_login 集成 LDAP(未启用零行为变化) - 修 http::redirect 空 body 触发 NGX_ERROR(CAS 302 无 Location 的根因) - nginx.conf / nginx.test.conf 补 env SYNC_* / PASSWORD_RESET_*(worker 可见) - 新增第 7 套 e2e tests/maxkey-p1p2-e2e.sh(21 项),harness 接入 [7/7];七套全绿 rc=0 - 修脚本 $var 紧跟全角字符被并入变量名的 shell 坑(tests + scripts) - 文档同步:maxkey-migration-analysis(P1/P2 状态) / roadmap(v4) / CLAUDE.md / README.md
162 lines
6.8 KiB
Rust
162 lines
6.8 KiB
Rust
//! CAS 1.0 / 2.0 协议端点(对齐 MaxKey `maxkey-cas`)。
|
||
//!
|
||
//! 让既有的 CAS 客户端(老 Java 应用、Confluence、Jenkins 等)用标准 CAS 协议
|
||
//! 接入本网关做单点登录,无需改写客户端。本实现为**精简版**:
|
||
//!
|
||
//! * 身份来源复用本网关的 SSO 会话(管理台 session / 终端用户 sso session 都认);
|
||
//! * ST(Service Ticket)签发后存共享内存,单次有效、短 TTL;
|
||
//! * `/cas/serviceValidate` 走 CAS 2.0 XML,`/cas/validate` 走 CAS 1.0 纯文本,
|
||
//! `/cas/proxyValidate` 与 serviceValidate 同形(忽略代理链)。
|
||
//!
|
||
//! 鉴权全部 `Guard::Public`:login 自己读会话、validate 靠 ticket,都不依赖调用方会话。
|
||
|
||
use ngx::ffi::ngx_http_request_t;
|
||
|
||
use ipass_core::http;
|
||
use ipass_core::session;
|
||
use ipass_core::state;
|
||
use ipass_core::util;
|
||
use ngx::core::Status;
|
||
use ngx::http::HTTPStatus;
|
||
|
||
use crate::audit;
|
||
|
||
/// ST 有效期(秒)。CAS 票据本就单次、短时,30s 足够客户端拿到后立刻来校验。
|
||
const CAS_TICKET_TTL: i64 = 30;
|
||
/// ST 共享内存键前缀:`cas:st:<ticket>`
|
||
const KEY_CAS_ST: &[u8] = b"cas:st:";
|
||
|
||
/// 路由:CAS 端点路径固定,精确匹配。
|
||
pub fn route(method: &str, uri: &str) -> Option<(crate::api::Guard, crate::api::Handler)> {
|
||
if method != "GET" {
|
||
return None;
|
||
}
|
||
let g = crate::api::Guard::Public;
|
||
match uri {
|
||
"/cas/login" => Some((g, h_cas_login)),
|
||
"/cas/logout" => Some((g, h_cas_logout)),
|
||
"/cas/validate" => Some((g, h_cas_validate)),
|
||
"/cas/serviceValidate" => Some((g, h_cas_service_validate)),
|
||
"/cas/proxyValidate" => Some((g, h_cas_proxy_validate)),
|
||
_ => None,
|
||
}
|
||
}
|
||
|
||
/// 生成 ST:`ST-<random>`。
|
||
fn new_ticket() -> String {
|
||
format!("ST-{}", util::gen_key())
|
||
}
|
||
|
||
/// 把 ticket 与服务绑定写入共享内存:payload = `username\nservice`。
|
||
fn issue_ticket(ticket: &str, username: &str, service: &str, now: i64) -> bool {
|
||
let payload = format!("{username}\n{service}");
|
||
state::put(KEY_CAS_ST, ticket.as_bytes(), payload.as_bytes(), now + CAS_TICKET_TTL)
|
||
}
|
||
|
||
/// 消费 ticket:校验存在、未过期、service 匹配,返回用户名(一次性)。
|
||
fn consume_ticket(ticket: &str, service: &str, now: i64) -> Option<String> {
|
||
let (payload, exp) = state::get(KEY_CAS_ST, ticket.as_bytes())?;
|
||
if exp > 0 && exp <= now {
|
||
state::remove(KEY_CAS_ST, ticket.as_bytes());
|
||
return None;
|
||
}
|
||
let (user, bound) = payload.split_once('\n')?;
|
||
if bound != service {
|
||
// service 不匹配:票据不可跨服务使用(防 fixation)
|
||
return None;
|
||
}
|
||
// 单次有效:消费即删
|
||
state::remove(KEY_CAS_ST, ticket.as_bytes());
|
||
Some(user.to_owned())
|
||
}
|
||
|
||
/// `/cas/login?service=URL`:已登录则签发 ST 并 302 回 service?ticket=ST;
|
||
/// 未登录则 302 到本网关登录页。无 service 时直接返回 ST(供程序化握手)。
|
||
fn h_cas_login(r: *mut ngx_http_request_t, _actor: crate::api::Actor) -> Status {
|
||
let p = http::params(r);
|
||
let service = p.get("service").map(String::as_str).unwrap_or("").to_owned();
|
||
let ip = http::remote_addr(r);
|
||
let ua = http::header(r, "user-agent").unwrap_or_default();
|
||
|
||
let cookie = http::header(r, "cookie");
|
||
let Some(user) = session::authenticate(cookie.as_deref()) else {
|
||
// 未登录:跳登录页(浏览器走完登录后会带着会话回来重放本请求)。
|
||
// 无 service 时无法回跳,直接 401 让调用方先建会话。
|
||
if service.is_empty() {
|
||
return http::fail(r, HTTPStatus::UNAUTHORIZED, "未登录或会话已过期,请先登录");
|
||
}
|
||
audit::log("cas.login.deny", "", "", false, &ip, &ua, "no session");
|
||
return http::redirect(r, "/login");
|
||
};
|
||
|
||
let ticket = new_ticket();
|
||
let now = util::now();
|
||
if !issue_ticket(&ticket, &user, &service, now) {
|
||
return http::fail(r, HTTPStatus::INTERNAL_SERVER_ERROR, "票据签发失败");
|
||
}
|
||
audit::log("cas.login", &user, &user, true, &ip, &ua, &service);
|
||
|
||
if service.is_empty() {
|
||
// 程序化握手:直接回 ST(JSON 信封,便于脚本取出)
|
||
return http::ok(r, serde_json::json!({ "ticket": ticket, "user": user }), "ok");
|
||
}
|
||
// 回跳 service?ticket= / &ticket=
|
||
let sep = if service.contains('?') { '&' } else { '?' };
|
||
let location = format!("{service}{sep}ticket={ticket}");
|
||
http::redirect(r, &location)
|
||
}
|
||
|
||
/// `/cas/logout`:销毁本会话,返回 200。
|
||
fn h_cas_logout(r: *mut ngx_http_request_t, _actor: crate::api::Actor) -> Status {
|
||
let cookie = http::header(r, "cookie");
|
||
session::destroy(cookie.as_deref());
|
||
http::ok(r, serde_json::json!({}), "已退出 CAS 单点登录")
|
||
}
|
||
|
||
/// `/cas/validate`:CAS 1.0 纯文本 `yes\n<user>` / `no\n`。
|
||
fn h_cas_validate(r: *mut ngx_http_request_t, _actor: crate::api::Actor) -> Status {
|
||
let p = http::params(r);
|
||
let service = p.get("service").map(String::as_str).unwrap_or("").to_owned();
|
||
let ticket = p.get("ticket").map(String::as_str).unwrap_or("").to_owned();
|
||
let now = util::now();
|
||
|
||
match consume_ticket(&ticket, &service, now) {
|
||
Some(user) => http::send_ct(r, HTTPStatus::OK, &format!("yes\n{user}\n"), "text/plain; charset=utf-8"),
|
||
None => http::send_ct(r, HTTPStatus::OK, "no\n", "text/plain; charset=utf-8"),
|
||
}
|
||
}
|
||
|
||
/// `/cas/serviceValidate`:CAS 2.0 XML。
|
||
fn h_cas_service_validate(r: *mut ngx_http_request_t, _actor: crate::api::Actor) -> Status {
|
||
let p = http::params(r);
|
||
let service = p.get("service").map(String::as_str).unwrap_or("").to_owned();
|
||
let ticket = p.get("ticket").map(String::as_str).unwrap_or("").to_owned();
|
||
let now = util::now();
|
||
|
||
match consume_ticket(&ticket, &service, now) {
|
||
Some(user) => {
|
||
let body = format!(
|
||
"<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>\n\
|
||
\x20 <cas:authenticationSuccess>\n\
|
||
\x20\x20 <cas:user>{user}</cas:user>\n\
|
||
\x20 </cas:authenticationSuccess>\n\
|
||
</cas:serviceResponse>\n"
|
||
);
|
||
http::send_ct(r, HTTPStatus::OK, &body, "application/xml; charset=utf-8")
|
||
}
|
||
None => {
|
||
let body = format!(
|
||
"<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>\n\
|
||
\x20 <cas:authenticationFailure code='INVALID_TICKET'>ticket 无效或已过期</cas:authenticationFailure>\n\
|
||
</cas:serviceResponse>\n"
|
||
);
|
||
http::send_ct(r, HTTPStatus::OK, &body, "application/xml; charset=utf-8")
|
||
}
|
||
}
|
||
}
|
||
|
||
/// `/cas/proxyValidate`:与 serviceValidate 同形(本项目不发放 PT,忽略代理链)。
|
||
fn h_cas_proxy_validate(r: *mut ngx_http_request_t, _actor: crate::api::Actor) -> Status {
|
||
h_cas_service_validate(r, _actor)
|
||
}
|