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 from_pairs(
pairs: impl IntoIterator<Item = (String, RestrictedExpression)>
) -> Result<Self, ContextCreationError>
pub fn from_pairs( pairs: impl IntoIterator<Item = (String, RestrictedExpression)> ) -> Result<Self, ContextCreationError>
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.
let context = Context::from_pairs([
("key".to_string(), RestrictedExpression::from_str(r#""value""#).unwrap()),
("age".to_string(), RestrictedExpression::from_str("18").unwrap()),
]).unwrap();
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.
let json_data = r#"{
"sub": "1234",
"groups": {
"1234": {
"group_id": "abcd",
"group_name": "test-group"
}
}
}"#;
let context = Context::from_json_str(json_data, None).unwrap();
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.
let schema_json = serde_json::json!(
{
"": {
"entityTypes": {
"User": {},
"Album": {},
},
"actions": {
"view": {
"appliesTo": {
"principalTypes": ["User"],
"resourceTypes": ["Album"],
"context": {
"type": "Record",
"attributes": {
"sub": { "type": "Long" }
}
}
}
}
}
}
});
let schema = Schema::from_json_value(schema_json).unwrap();
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 data = serde_json::json!({
"sub": 1234
});
let context = Context::from_json_value(data, Some((&schema, &action))).unwrap();
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.json").unwrap();
let context = Context::from_json_file(&json, None).unwrap();