Skip to content

Commit 85b06dc

Browse files
author
Victor Moreno
committed
add authorization example to ro cedar rust hello world
Signed-off-by: Victor Moreno <morevct@amazon.com>
1 parent 5a29a38 commit 85b06dc

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

cedar-rust-hello-world/src/main.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ fn main() {
4444

4545
//print a policy in JSON format
4646
to_json();
47+
48+
//Authorization example
49+
let decision = authorization();
50+
println!("{:?}", decision);
4751
}
4852
/// parse a policy
4953
fn parse_policy() {
@@ -471,3 +475,31 @@ fn create_p_a_r() -> (EntityUid, EntityUid, EntityUid) {
471475
let r = EntityUid::from_type_name_and_id(r_name, r_eid);
472476
(p, a, r)
473477
}
478+
479+
/// Demonstrates a basic Cedar authorization flow
480+
/// Returns a Response indicating whether the access is allowed or denied
481+
fn authorization() -> Response {
482+
let (principal, action, resource) = create_p_a_r();
483+
let context_json_val = serde_json::json!({});
484+
let context = Context::from_json_value(context_json_val, None).unwrap();
485+
486+
// Construct the authorization request combining principal, action, resource, and context
487+
let request = Request::new(principal, action, resource, context, None)
488+
.expect("request validation error");
489+
490+
// Define the policy that determines access rules
491+
// This policy permits user "alice" to perform "update" action on "VacationPhoto94.jpg"
492+
let policies_str = r#"permit(
493+
principal == User::"alice",
494+
action == Action::"view",
495+
resource == Album::"trip"
496+
);"#;
497+
498+
// Evaluate the authorization request against the policy and entities
499+
let policy_set = PolicySet::from_str(policies_str).expect("policy parse error");
500+
let entities_json = r#"[]"#;
501+
let entities = Entities::from_json_str(entities_json, None).expect("entity parse error");
502+
let authorizer = Authorizer::new();
503+
authorizer.is_authorized(&request, &policy_set, &entities)
504+
}
505+

0 commit comments

Comments
 (0)