genies_auth_admin 1.8.1

统一认证管理服务 - 用户、角色、权限、组织架构管理
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! 应用权限代理 Handler
//!
//! 根据 app_id 查出目标微服务 base_url,
//! 使用 reqwest::Client 将请求转发到目标服务的 /auth/* 端点。
//! 代理直接透传目标微服务的原始 HTTP 响应(状态码 + body),不做 RespVO 包装。

use salvo::prelude::*;
use salvo::oapi::extract::*;
use salvo::writing::Text;

use crate::application::app_service::ApplicationAppService;
use crate::application::service::SyncAppService;

/// 返回代理子路由列表,需由调用方挂载到 `{id}` 节点下
pub fn proxy_sub_routes() -> Vec<Router> {
    vec![
        Router::with_path("schemas").get(proxy_list_schemas),
        Router::with_path("policies")
            .get(proxy_list_policies)
            .post(proxy_add_policy),
        Router::with_path("policies/{policy_id}").delete(proxy_remove_policy),
        Router::with_path("roles")
            .get(proxy_list_roles)
            .post(proxy_add_role),
        Router::with_path("roles/{role_id}").delete(proxy_remove_role),
        Router::with_path("groups")
            .get(proxy_list_groups)
            .post(proxy_add_group),
        Router::with_path("groups/{group_id}").delete(proxy_remove_group),
        Router::with_path("reload").post(proxy_reload),
        Router::with_path("sync-user-roles").post(proxy_sync_user_roles),
    ]
}

/// 内部辅助:根据 app_id 获取 base_url
async fn resolve_base_url(app_id: i64) -> Result<String, String> {
    let app = ApplicationAppService::get_app(app_id).await?;
    let status = app.status.unwrap_or(0);
    if status != 1 {
        return Err("应用已禁用".to_string());
    }
    app.base_url.ok_or_else(|| "应用未配置访问地址".to_string())
}

/// 内部辅助:从请求中提取 Authorization 头
fn extract_auth_header(req: &Request) -> Option<String> {
    req.headers().get("Authorization").and_then(|v| v.to_str().ok()).map(|s| s.to_string())
}

/// 内部辅助:将目标服务的原始响应直接透传到 Salvo Response
async fn forward_response(resp: reqwest::Response, res: &mut Response) {
    let status = resp.status();
    let salvo_status = StatusCode::from_u16(status.as_u16())
        .unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);
    res.status_code(salvo_status);
    match resp.text().await {
        Ok(body) => {
            res.render(Text::Json(body));
        }
        Err(e) => {
            res.status_code(StatusCode::BAD_GATEWAY);
            let err = serde_json::json!({"code": "-1", "msg": format!("读取目标响应失败: {}", e)});
            res.render(Text::Json(err.to_string()));
        }
    }
}

/// 内部辅助:向 Salvo Response 写入 JSON 错误
fn write_error(res: &mut Response, msg: &str) {
    res.status_code(StatusCode::BAD_GATEWAY);
    let err = serde_json::json!({"code": "-1", "msg": msg});
    res.render(Text::Json(err.to_string()));
}

/// GET /apps/{id}/schemas — 代理查询 API Schema
#[endpoint(tags("app-proxy"), summary = "代理查询应用 Schema")]
pub async fn proxy_list_schemas(
    id: PathParam<i64>,
    req: &mut Request,
    res: &mut Response,
) {
    let auth_header = extract_auth_header(req);
    let base_url = match resolve_base_url(id.into_inner()).await {
        Ok(u) => u,
        Err(msg) => return write_error(res, &msg),
    };

    let query_string = req.uri().query().unwrap_or("");
    let url = if query_string.is_empty() {
        format!("{}/auth/schemas", base_url.trim_end_matches('/'))
    } else {
        format!("{}/auth/schemas?{}", base_url.trim_end_matches('/'), query_string)
    };

    let mut builder = reqwest::Client::new().get(&url);
    if let Some(auth) = &auth_header {
        builder = builder.header("Authorization", auth.as_str());
    }
    match builder.send().await {
        Ok(resp) => forward_response(resp, res).await,
        Err(e) => write_error(res, &format!("目标服务不可达: {}", e)),
    }
}

/// GET /apps/{id}/policies — 代理查询策略
#[endpoint(tags("app-proxy"), summary = "代理查询应用策略")]
pub async fn proxy_list_policies(
    id: PathParam<i64>,
    req: &mut Request,
    res: &mut Response,
) {
    let auth_header = extract_auth_header(req);
    let base_url = match resolve_base_url(id.into_inner()).await {
        Ok(u) => u,
        Err(msg) => return write_error(res, &msg),
    };

    let query_string = req.uri().query().unwrap_or("");
    let url = if query_string.is_empty() {
        format!("{}/auth/policies", base_url.trim_end_matches('/'))
    } else {
        format!("{}/auth/policies?{}", base_url.trim_end_matches('/'), query_string)
    };

    let mut builder = reqwest::Client::new().get(&url);
    if let Some(auth) = &auth_header {
        builder = builder.header("Authorization", auth.as_str());
    }
    match builder.send().await {
        Ok(resp) => forward_response(resp, res).await,
        Err(e) => write_error(res, &format!("目标服务不可达: {}", e)),
    }
}

/// POST /apps/{id}/policies — 代理添加策略
#[endpoint(tags("app-proxy"), summary = "代理添加应用策略")]
pub async fn proxy_add_policy(
    id: PathParam<i64>,
    req: &mut Request,
    res: &mut Response,
    body: JsonBody<serde_json::Value>,
) {
    let auth_header = extract_auth_header(req);
    let base_url = match resolve_base_url(id.into_inner()).await {
        Ok(u) => u,
        Err(msg) => return write_error(res, &msg),
    };

    let url = format!("{}/auth/policies", base_url.trim_end_matches('/'));

    let mut builder = reqwest::Client::new().post(&url).json(&body.into_inner());
    if let Some(auth) = &auth_header {
        builder = builder.header("Authorization", auth.as_str());
    }
    match builder.send().await {
        Ok(resp) => forward_response(resp, res).await,
        Err(e) => write_error(res, &format!("目标服务不可达: {}", e)),
    }
}

/// DELETE /apps/{id}/policies/{policy_id} — 代理删除策略
#[endpoint(tags("app-proxy"), summary = "代理删除应用策略")]
pub async fn proxy_remove_policy(
    id: PathParam<i64>,
    policy_id: PathParam<i64>,
    req: &mut Request,
    res: &mut Response,
) {
    let auth_header = extract_auth_header(req);
    let base_url = match resolve_base_url(id.into_inner()).await {
        Ok(u) => u,
        Err(msg) => return write_error(res, &msg),
    };

    let url = format!("{}/auth/policies/{}", base_url.trim_end_matches('/'), policy_id.into_inner());

    let mut builder = reqwest::Client::new().delete(&url);
    if let Some(auth) = &auth_header {
        builder = builder.header("Authorization", auth.as_str());
    }
    match builder.send().await {
        Ok(resp) => forward_response(resp, res).await,
        Err(e) => write_error(res, &format!("目标服务不可达: {}", e)),
    }
}

/// GET /apps/{id}/roles — 代理查询角色
#[endpoint(tags("app-proxy"), summary = "代理查询应用角色分配")]
pub async fn proxy_list_roles(
    id: PathParam<i64>,
    req: &mut Request,
    res: &mut Response,
) {
    let auth_header = extract_auth_header(req);
    let base_url = match resolve_base_url(id.into_inner()).await {
        Ok(u) => u,
        Err(msg) => return write_error(res, &msg),
    };

    let query_string = req.uri().query().unwrap_or("");
    let url = if query_string.is_empty() {
        format!("{}/auth/roles", base_url.trim_end_matches('/'))
    } else {
        format!("{}/auth/roles?{}", base_url.trim_end_matches('/'), query_string)
    };

    let mut builder = reqwest::Client::new().get(&url);
    if let Some(auth) = &auth_header {
        builder = builder.header("Authorization", auth.as_str());
    }
    match builder.send().await {
        Ok(resp) => forward_response(resp, res).await,
        Err(e) => write_error(res, &format!("目标服务不可达: {}", e)),
    }
}

/// POST /apps/{id}/roles — 代理添加角色
#[endpoint(tags("app-proxy"), summary = "代理添加应用角色分配")]
pub async fn proxy_add_role(
    id: PathParam<i64>,
    req: &mut Request,
    res: &mut Response,
    body: JsonBody<serde_json::Value>,
) {
    let auth_header = extract_auth_header(req);
    let base_url = match resolve_base_url(id.into_inner()).await {
        Ok(u) => u,
        Err(msg) => return write_error(res, &msg),
    };

    let url = format!("{}/auth/roles", base_url.trim_end_matches('/'));

    let mut builder = reqwest::Client::new().post(&url).json(&body.into_inner());
    if let Some(auth) = &auth_header {
        builder = builder.header("Authorization", auth.as_str());
    }
    match builder.send().await {
        Ok(resp) => forward_response(resp, res).await,
        Err(e) => write_error(res, &format!("目标服务不可达: {}", e)),
    }
}

/// GET /apps/{id}/groups — 代理查询分组
#[endpoint(tags("app-proxy"), summary = "代理查询应用对象分组")]
pub async fn proxy_list_groups(
    id: PathParam<i64>,
    req: &mut Request,
    res: &mut Response,
) {
    let auth_header = extract_auth_header(req);
    let base_url = match resolve_base_url(id.into_inner()).await {
        Ok(u) => u,
        Err(msg) => return write_error(res, &msg),
    };

    let query_string = req.uri().query().unwrap_or("");
    let url = if query_string.is_empty() {
        format!("{}/auth/groups", base_url.trim_end_matches('/'))
    } else {
        format!("{}/auth/groups?{}", base_url.trim_end_matches('/'), query_string)
    };

    let mut builder = reqwest::Client::new().get(&url);
    if let Some(auth) = &auth_header {
        builder = builder.header("Authorization", auth.as_str());
    }
    match builder.send().await {
        Ok(resp) => forward_response(resp, res).await,
        Err(e) => write_error(res, &format!("目标服务不可达: {}", e)),
    }
}

/// POST /apps/{id}/groups — 代理添加分组
#[endpoint(tags("app-proxy"), summary = "代理添加应用对象分组")]
pub async fn proxy_add_group(
    id: PathParam<i64>,
    req: &mut Request,
    res: &mut Response,
    body: JsonBody<serde_json::Value>,
) {
    let auth_header = extract_auth_header(req);
    let base_url = match resolve_base_url(id.into_inner()).await {
        Ok(u) => u,
        Err(msg) => return write_error(res, &msg),
    };

    let url = format!("{}/auth/groups", base_url.trim_end_matches('/'));

    let mut builder = reqwest::Client::new().post(&url).json(&body.into_inner());
    if let Some(auth) = &auth_header {
        builder = builder.header("Authorization", auth.as_str());
    }
    match builder.send().await {
        Ok(resp) => forward_response(resp, res).await,
        Err(e) => write_error(res, &format!("目标服务不可达: {}", e)),
    }
}

/// DELETE /apps/{id}/roles/{role_id} — 代理删除角色
#[endpoint(tags("app-proxy"), summary = "代理删除应用角色分配")]
pub async fn proxy_remove_role(
    id: PathParam<i64>,
    role_id: PathParam<i64>,
    req: &mut Request,
    res: &mut Response,
) {
    let auth_header = extract_auth_header(req);
    let base_url = match resolve_base_url(id.into_inner()).await {
        Ok(u) => u,
        Err(msg) => return write_error(res, &msg),
    };

    let url = format!("{}/auth/roles/{}", base_url.trim_end_matches('/'), role_id.into_inner());

    let mut builder = reqwest::Client::new().delete(&url);
    if let Some(auth) = &auth_header {
        builder = builder.header("Authorization", auth.as_str());
    }
    match builder.send().await {
        Ok(resp) => forward_response(resp, res).await,
        Err(e) => write_error(res, &format!("目标服务不可达: {}", e)),
    }
}

/// DELETE /apps/{id}/groups/{group_id} — 代理删除分组
#[endpoint(tags("app-proxy"), summary = "代理删除应用对象分组")]
pub async fn proxy_remove_group(
    id: PathParam<i64>,
    group_id: PathParam<i64>,
    req: &mut Request,
    res: &mut Response,
) {
    let auth_header = extract_auth_header(req);
    let base_url = match resolve_base_url(id.into_inner()).await {
        Ok(u) => u,
        Err(msg) => return write_error(res, &msg),
    };

    let url = format!("{}/auth/groups/{}", base_url.trim_end_matches('/'), group_id.into_inner());

    let mut builder = reqwest::Client::new().delete(&url);
    if let Some(auth) = &auth_header {
        builder = builder.header("Authorization", auth.as_str());
    }
    match builder.send().await {
        Ok(resp) => forward_response(resp, res).await,
        Err(e) => write_error(res, &format!("目标服务不可达: {}", e)),
    }
}

/// POST /apps/{id}/sync-user-roles — 将用户-角色映射推送到目标微服务
#[endpoint(tags("app-proxy"), summary = "推送用户-角色映射到目标应用")]
pub async fn proxy_sync_user_roles(
    id: PathParam<i64>,
    req: &mut Request,
    res: &mut Response,
) {
    let auth_header = extract_auth_header(req);
    let base_url = match resolve_base_url(id.into_inner()).await {
        Ok(u) => u,
        Err(msg) => return write_error(res, &msg),
    };

    // 查询所有活跃的用户-角色映射
    let mappings = match SyncAppService::list_active_user_roles().await {
        Ok(data) => data,
        Err(msg) => return write_error(res, &format!("查询用户-角色映射失败: {}", msg)),
    };

    let url = format!("{}/auth/sync/receive-user-roles", base_url.trim_end_matches('/'));

    let mut builder = reqwest::Client::new().post(&url).json(&mappings);
    if let Some(auth) = &auth_header {
        builder = builder.header("Authorization", auth.as_str());
    }
    match builder.send().await {
        Ok(resp) => forward_response(resp, res).await,
        Err(e) => write_error(res, &format!("目标服务不可达: {}", e)),
    }
}

/// POST /apps/{id}/reload — 代理重载 Enforcer
#[endpoint(tags("app-proxy"), summary = "代理重载应用 Enforcer")]
pub async fn proxy_reload(
    id: PathParam<i64>,
    req: &mut Request,
    res: &mut Response,
) {
    let auth_header = extract_auth_header(req);
    let base_url = match resolve_base_url(id.into_inner()).await {
        Ok(u) => u,
        Err(msg) => return write_error(res, &msg),
    };

    let url = format!("{}/auth/reload", base_url.trim_end_matches('/'));

    let mut builder = reqwest::Client::new().post(&url);
    if let Some(auth) = &auth_header {
        builder = builder.header("Authorization", auth.as_str());
    }
    match builder.send().await {
        Ok(resp) => forward_response(resp, res).await,
        Err(e) => write_error(res, &format!("目标服务不可达: {}", e)),
    }
}