//! OAuth 状态与存储(SSO 插件个性,**不进内核**)。 //! //! 内核只提供通用共享内存原语 `state::put/get/remove/clear_prefix` 与通用文件 //! 接口 `store::read_plugin/update_plugin`,本模块用它们实现 OAuth 专属读写: //! //! * `ocl:` 接入方注册表镜像(`get/put/remove_oauth_client`) //! * `oac:` 授权码(`put/take_oauth_code`,一次性) //! * `sso:` 终端用户会话(`get/put/remove_oauth_session`) //! * `data/oauth_clients.json` 注册表真源(`read/update_oauth_clients`) //! //! 这些原本是内核 `state` / `store` 里的 OAuth 段;收窄内核时按「个性代码 //! 插件自己实现」整体迁到此处。语义(前缀、TTL、一次性消费)与迁移前完全一致。 use serde::{Deserialize, Serialize}; use crate::oauth_keys::{ KEY_OAUTH_CLIENT_PREFIX, KEY_OAUTH_CODE_PREFIX, KEY_OAUTH_SESS_PREFIX, }; use ipass_core::state; use crate::storex; // =========================================================================== // 数据模型(原内核 `models::OAuthClient`;属插件个性,迁到本插件) // =========================================================================== #[derive(Debug, Clone, Serialize, Deserialize, Default)] pub struct OAuthClient { pub client_id: String, pub client_secret: String, pub name: String, pub redirect_uris: Vec, pub grant_types: Vec, pub scopes: Vec, pub confidential: bool, pub created_at: i64, } pub type OAuthClientTable = Vec; impl OAuthClient { /// 校验 scope 是否全部落在白名单内(openid 始终允许,OIDC 必备) pub fn allows_scope(&self, scope: &str) -> bool { if scope == "openid" { return true; } self.scopes.iter().any(|s| s == scope) } /// 校验重定向地址是否在注册清单内(精确匹配,不做前缀/通配) pub fn allows_redirect(&self, uri: &str) -> bool { self.redirect_uris.iter().any(|u| u == uri) } /// 是否允许该授权类型 pub fn allows_grant(&self, grant: &str) -> bool { self.grant_types.iter().any(|g| g == grant) } } // =========================================================================== // 注册表(文件真源:data/oauth_clients.json) // =========================================================================== /// 读取注册表文件(内核只提供带锁原子读,不认识结构)。 pub fn read_oauth_clients() -> OAuthClientTable { storex::read_plugin::("oauth_clients.json") } /// 加锁「读-改-写」注册表文件,闭包返回新值;`Err` 不落盘。 pub fn update_oauth_clients(f: F) -> Result where F: FnOnce(&mut OAuthClientTable) -> Result, { let mut table = read_oauth_clients(); let out = f(&mut table)?; storex::write_plugin("oauth_clients.json", &table)?; Ok(out) } // =========================================================================== // 注册表共享内存镜像(`ocl:`) // =========================================================================== /// 按 client_id 取客户端 JSON(低频:授权/令牌端点各一次)。 pub fn get_oauth_client(client_id: &str) -> Option { state::get(KEY_OAUTH_CLIENT_PREFIX, client_id.as_bytes()).map(|(p, _)| p) } /// 写入/覆盖客户端 JSON。 pub fn put_oauth_client(client_id: &str, payload: &str) -> bool { state::put(KEY_OAUTH_CLIENT_PREFIX, client_id.as_bytes(), payload.as_bytes(), 0) } /// 删除客户端(重载/删除时)。 pub fn remove_oauth_client(client_id: &str) -> bool { state::remove(KEY_OAUTH_CLIENT_PREFIX, client_id.as_bytes()) } // =========================================================================== // 授权码(`oac:`,一次性) // =========================================================================== /// 写入授权码上下文(含 sub / client_id / redirect_uri / scope / nonce / PKCE 挑战)。 pub fn put_oauth_code(code: &str, ctx: &str, expires_at: i64) -> bool { state::put(KEY_OAUTH_CODE_PREFIX, code.as_bytes(), ctx.as_bytes(), expires_at) } /// 取出并消费授权码(一次性):过期或不存在返回 None。 pub fn take_oauth_code(code: &str, now: i64) -> Option { let raw = state::get_live(KEY_OAUTH_CODE_PREFIX, code.as_bytes(), now)?; state::remove(KEY_OAUTH_CODE_PREFIX, code.as_bytes()); Some(raw) } // =========================================================================== // 终端用户会话(`sso:`,对应 SSO 登录态) // =========================================================================== /// 写入 SSO 会话。 pub fn put_oauth_session(token: &str, payload: &str, expires_at: i64) -> bool { state::put(KEY_OAUTH_SESS_PREFIX, token.as_bytes(), payload.as_bytes(), expires_at) } /// 读取(未过期才返回)。 pub fn get_oauth_session(token: &str, now: i64) -> Option { state::get_live(KEY_OAUTH_SESS_PREFIX, token.as_bytes(), now) } /// 删除会话。 pub fn remove_oauth_session(token: &str) -> bool { state::remove(KEY_OAUTH_SESS_PREFIX, token.as_bytes()) }