Files
core/test/rust/build/api/identity.rs
T
2026-11-27 06:03:56 +00:00

108 lines
4.1 KiB
Rust

//! 身份沿调用图与任务传递,入口权限由编译器约束,Provider 名义类型由配置绑定核对。
use super::*;
#[test]
fn identity_reads_reject_untrusted_entries_and_pure_functions() {
for (name, app, api, message) in [
(
"anonymous",
"flow current() (out:Text)\n| step | kind | do |\n| out | calc | dever.auth.id() |",
"public get current = app.current",
"public HTTP",
),
(
"command",
"flow current() (out:Text)\n| step | kind | do |\n| out | calc | dever.auth.id() |",
"cmd current = app.current",
"CMD cannot",
),
(
"pure",
"current() (out:Text) {out=dever.auth.id()}",
"authenticated get current = app.current",
"pure",
),
(
"argument",
"flow current() (out:Text)\n| step | kind | do |\n| out | calc | dever.auth.id(\"forged\") |",
"authenticated get current = app.current",
"no arguments",
),
(
"nominal",
"flow current() (out:Int?)\n| step | kind | do |\n| out | calc | dever.auth.userId() |",
"authenticated get current = app.current",
"nominal Model ID",
),
(
"transitive",
"flow current() (out:Text)\n| step | kind | do |\n| out | flow | app.readIdentity() |\nflow readIdentity() (out:Text)\n| step | kind | do |\n| out | calc | dever.auth.id() |",
"public get current = app.current",
"public HTTP",
),
(
"scheduled",
"flow current() ()\n| step | kind | do |\n| - | calc | dever.auth.id() |",
"job current() retry(1) timeout(1000) = app.current\nschedule every = \"* * * * *\" = app.current",
"System Job",
),
] {
let root = project(&format!("identity-{name}"), app, api);
let project = Project::load(&root).unwrap();
assert!(
project.diagnostics.is_empty(),
"{name}: {:?}",
project.diagnostics
);
let error = program::check(&project.declarations, &project.modules)
.err()
.expect("invalid identity use");
assert!(error.message.contains(message), "{name}: {error:?}");
}
}
#[test]
fn identity_provider_types_are_checked_before_starting_http() {
let root = project(
"identity-provider",
r#"
type Identity {id:Text,user_id:data.user.id?,tenant_id:data.tenant.id?}
flow verify(claims:dever.auth.Claims) (identity:Identity)
| step | kind | do |
| identity | calc | Identity {id=claims.subject,user_id=null,tenant_id=null} |
flow current() (out:data.tenant.id?)
| step | kind | do |
| out | calc | dever.auth.userId() |
"#,
"authenticated get current = app.current",
);
let domain = root.join("module/demo/service/data");
fs::create_dir_all(&domain).unwrap();
fs::write(domain.join("user.dever"), "global type User {name:Text}").unwrap();
fs::write(
domain.join("tenant.dever"),
"global type Tenant {name:Text}",
)
.unwrap();
let (loaded, program) = analyze(&root);
let artifact = build::build(&loaded, &program, "main", Profile::Dev, None).unwrap();
let (path, _, mut config) = config(&root);
config["database"] = serde_json::json!({"default":{"type":"sqlite","path":"missing.db"}});
config["sites"]["front"]["auth"] = serde_json::json!("users");
config["auth"] = serde_json::json!({"providers":{"users":{"verify":"demo.service.verify","jwtSecret":"01234567890123456789012345678901","ttlSeconds":3600}}});
fs::write(&path, config.to_string()).unwrap();
let result = Command::new(&artifact.binary)
.arg("--config")
.arg(&path)
.arg("api")
.output()
.unwrap();
assert!(!result.status.success());
assert!(
String::from_utf8_lossy(&result.stderr).contains("Provider identity types do not match"),
"{}",
String::from_utf8_lossy(&result.stderr)
);
assert!(!root.join("missing.db").exists());
}