camel_cli/template/
authorization_policy.rs1use super::{TemplateFile, cargo_toml, gitignore, plugin_toml, readme_md, to_pascal_case};
2
3pub fn authorization_policy_files(plugin_name: &str) -> Vec<TemplateFile> {
4 vec![
5 TemplateFile {
6 path: "Cargo.toml".to_string(),
7 content: cargo_toml(plugin_name),
8 },
9 TemplateFile {
10 path: "src/lib.rs".to_string(),
11 content: lib_rs(plugin_name),
12 },
13 TemplateFile {
14 path: "Camel.plugin.toml".to_string(),
15 content: plugin_toml(plugin_name, "authorization-policy"),
16 },
17 TemplateFile {
18 path: "wit/camel-plugin.wit".to_string(),
19 content: camel_wit::PLUGIN_WIT.to_string(),
20 },
21 TemplateFile {
22 path: "README.md".to_string(),
23 content: readme_md(plugin_name, "authorization-policy"),
24 },
25 TemplateFile {
26 path: ".gitignore".to_string(),
27 content: gitignore().to_string(),
28 },
29 ]
30}
31
32fn lib_rs(plugin_name: &str) -> String {
33 let plugin_type = to_pascal_case(plugin_name);
34 format!(
35 "use bindings::camel::plugin::types::{{WasmError, WasmExchange}};\nuse bindings::Guest;\n\nmod bindings {{\n wit_bindgen::generate!({{\n world: \"authorization-policy\",\n path: \"../wit\",\n }});\n}}\n\nstruct {plugin_type};\n\nimpl Guest for {plugin_type} {{\n fn init(config: Vec<(String, String)>) -> Result<(), String> {{\n let _ = config;\n Ok(())\n }}\n\n fn evaluate(exchange: WasmExchange) -> Result<Option<String>, WasmError> {{\n let roles_prop = bindings::camel::plugin::host::get_property(\"camel.auth.roles\");\n match roles_prop {{\n Some(roles_json) if roles_json.contains(\"admin\") => Ok(None),\n _ => Ok(Some(\"admin role required\".into())),\n }}\n }}\n}}\n\nbindings::export!({plugin_type} with_types_in bindings);\n"
36 )
37}