Struct cedar_policy::Context
source · pub struct Context(/* private fields */);
Expand description
the Context object for an authorization request
Implementations§
source§impl Context
impl Context
sourcepub fn empty() -> Self
pub fn empty() -> Self
Create an empty Context
use cedar_policy::Context;
let c = Context::empty();
// let request: Request = Request::new(Some(principal), Some(action), Some(resource), c);
sourcepub fn from_pairs(
pairs: impl IntoIterator<Item = (String, RestrictedExpression)>,
) -> Self
pub fn from_pairs( pairs: impl IntoIterator<Item = (String, RestrictedExpression)>, ) -> Self
Create a Context
from a map of key to “restricted expression”,
or a Vec of (key, restricted expression)
pairs, or any other iterator
of (key, restricted expression)
pairs.
use cedar_policy::{Context, RestrictedExpression};
use std::collections::HashMap;
use std::str::FromStr;
let data : serde_json::Value = serde_json::json!({
"sub": "1234",
"groups": {
"1234": {
"group_id": "abcd",
"group_name": "test-group"
}
}
});
let mut groups: HashMap<String, RestrictedExpression> = HashMap::new();
groups.insert("key".to_string(), RestrictedExpression::from_str(&data.to_string()).unwrap());
groups.insert("age".to_string(), RestrictedExpression::from_str("18").unwrap());
let context = Context::from_pairs(groups);
let request: Request = Request::new(Some(p), Some(a), Some(r), context);
sourcepub fn from_json_str(
json: &str,
schema: Option<(&Schema, &EntityUid)>,
) -> Result<Self, ContextJsonError>
pub fn from_json_str( json: &str, schema: Option<(&Schema, &EntityUid)>, ) -> Result<Self, ContextJsonError>
Create a Context
from a string containing JSON (which must be a JSON
object, not any other JSON type, or you will get an error here).
JSON here must use the __entity
and __extn
escapes for entity
references, extension values, etc.
If a schema
is provided, this will inform the parsing: for instance, it
will allow __entity
and __extn
escapes to be implicit, and it will error
if attributes have the wrong types (e.g., string instead of integer).
Since different Actions have different schemas for Context
, you also
must specify the Action
for schema-based parsing.
use cedar_policy::{Context, RestrictedExpression};
use std::collections::HashMap;
use std::str::FromStr;
let data =r#"{
"sub": "1234",
"groups": {
"1234": {
"group_id": "abcd",
"group_name": "test-group"
}
}
}"#;
let context = Context::from_json_str(data, None).unwrap();
let request: Request = Request::new(Some(p), Some(a), Some(r), context);
sourcepub fn from_json_value(
json: Value,
schema: Option<(&Schema, &EntityUid)>,
) -> Result<Self, ContextJsonError>
pub fn from_json_value( json: Value, schema: Option<(&Schema, &EntityUid)>, ) -> Result<Self, ContextJsonError>
Create a Context
from a serde_json::Value
(which must be a JSON object,
not any other JSON type, or you will get an error here).
JSON here must use the __entity
and __extn
escapes for entity
references, extension values, etc.
If a schema
is provided, this will inform the parsing: for instance, it
will allow __entity
and __extn
escapes to be implicit, and it will error
if attributes have the wrong types (e.g., string instead of integer).
Since different Actions have different schemas for Context
, you also
must specify the Action
for schema-based parsing.
use cedar_policy::{Context, RestrictedExpression, Schema};
use std::collections::HashMap;
use std::str::FromStr;
let data = serde_json::json!(
{
"sub": "1234"
});
let schema_data =r#"
{
"": {
"entityTypes": {},
"actions": {
"view": {
"appliesTo": {
"principalTypes": [],
"resourceTypes": [],
"context": {
"type": "Record",
"attributes": {
"sub": { "type": "Long" }
}
}
}
}
}
}
}"#;
let a_eid = EntityId::from_str("view").unwrap();
let a_name: EntityTypeName = EntityTypeName::from_str("Action").unwrap();
let action = EntityUid::from_type_name_and_id(a_name, a_eid);
let schema = Schema::from_str(schema_data).unwrap();
let context = Context::from_json_value(data, Some((&schema, &action))).unwrap();
let request: Request = Request::new(Some(principal), Some(action), Some(resource), context);
sourcepub fn from_json_file(
json: impl Read,
schema: Option<(&Schema, &EntityUid)>,
) -> Result<Self, ContextJsonError>
pub fn from_json_file( json: impl Read, schema: Option<(&Schema, &EntityUid)>, ) -> Result<Self, ContextJsonError>
Create a Context
from a JSON file. The JSON file must contain a JSON
object, not any other JSON type, or you will get an error here.
JSON here must use the __entity
and __extn
escapes for entity
references, extension values, etc.
If a schema
is provided, this will inform the parsing: for instance, it
will allow __entity
and __extn
escapes to be implicit, and it will error
if attributes have the wrong types (e.g., string instead of integer).
Since different Actions have different schemas for Context
, you also
must specify the Action
for schema-based parsing.
let mut json = File::open("json_file.txt").expect("failed");
let context = Context::from_json_file(&json, None).unwrap();
let request: Request = Request::new(Some(p), Some(a), Some(r), context);
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Context
impl RefUnwindSafe for Context
impl Send for Context
impl Sync for Context
impl Unpin for Context
impl UnwindSafe for Context
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more