cedar_policy

Struct Context

Source
pub struct Context(/* private fields */);
Expand description

the Context object for an authorization request

Implementations§

Source§

impl Context

Source

pub fn empty() -> Self

Create an empty Context

let context = Context::empty();
Source

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();
Source

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();
Source

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();
Source

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();
Source

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

Trait Implementations§

Source§

impl Clone for Context

Source§

fn clone(&self) -> Context

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Context

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Context

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl IntoIterator for Context

Source§

type Item = (String, RestrictedExpression)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl RefCast for Context

Source§

type From = Context

Source§

fn ref_cast(_from: &Self::From) -> &Self

Source§

fn ref_cast_mut(_from: &mut Self::From) -> &mut Self

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToSmolStr for T
where T: Display + ?Sized,

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.