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 get(&self, key: &str) -> Option<EvalResult>
pub fn get(&self, key: &str) -> Option<EvalResult>
Retrieves a value from the Context by its key.
§Arguments
key- The key to look up in the context
§Returns
Some(EvalResult)- If the key exists in the context, returns its valueNone- If the key doesn’t exist or if the context is not a Value type
§Examples
let context = Context::from_json_str(r#"{"rayId": "abc123"}"#, None).unwrap();
if let Some(value) = context.get("rayId") {
// value here is an EvalResult, convertible from the internal Value type
println!("Found value: {:?}", value);
}
assert_eq!(context.get("nonexistent"), None);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();Sourcepub fn merge(
self,
other_context: impl IntoIterator<Item = (String, RestrictedExpression)>,
) -> Result<Self, ContextCreationError>
pub fn merge( self, other_context: impl IntoIterator<Item = (String, RestrictedExpression)>, ) -> Result<Self, ContextCreationError>
Merge this Context with another context (or iterator over
(String, RestrictedExpression) pairs), returning an error if the two
contain overlapping keys
Sourcepub fn validate(
&self,
schema: &Schema,
action: &EntityUid,
) -> Result<(), RequestValidationError>
pub fn validate( &self, schema: &Schema, action: &EntityUid, ) -> Result<(), RequestValidationError>
Validates this context against the provided schema
Returns Ok(()) if the context is valid according to the schema, or an error otherwise
This validation is already handled by Request::new, so there is no need to separately call
if you are validating the whole request
Trait Implementations§
Source§impl IntoIterator for Context
impl IntoIterator for Context
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§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