Skip to content

Commit

Permalink
Improve substitution (#1387)
Browse files Browse the repository at this point in the history
Signed-off-by: Lőrinc Bódy <[email protected]>
  • Loading branch information
B-Lorentz authored Jan 2, 2025
1 parent d1f1185 commit bbf7813
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
# Don't check in the Emacs temp files
*~

# Don't check in common editors configs
.vscode
.zed

# Don't check in test framework files
.attach_pid*

Expand Down
1 change: 1 addition & 0 deletions cedar-policy/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Cedar Language Version: TBD
- Added protobuf and JSON generation code to `cedar-policy-cli`.
- Added a new get helper method to Context that allows easy extraction of generic values from the context by key. This method simplifies the common use case of retrieving values from Context objects.
- Implemented [RFC 62 (extended `has` operator)](https://github.com/cedar-policy/rfcs/blob/main/text/0062-extended-has.md) (#1327, resolving #1329)
- Added a helper method to `PartialResponse` to accept substitutions from an iterator.

### Changed

Expand Down
17 changes: 15 additions & 2 deletions cedar-policy/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1079,12 +1079,25 @@ impl PartialResponse {
self.0.get(id.as_ref()).map(Policy::from_ast)
}

/// Attempt to re-authorize this response given a mapping from unknowns to values
/// Attempt to re-authorize this response given a mapping from unknowns to values.
#[allow(clippy::needless_pass_by_value)]
#[deprecated = "use reauthorize_with_bindings"]
pub fn reauthorize(
&self,
mapping: HashMap<SmolStr, RestrictedExpression>,
auth: &Authorizer,
es: &Entities,
) -> Result<Self, ReauthorizationError> {
self.reauthorize_with_bindings(mapping.iter().map(|(k, v)| (k.as_str(), v)), auth, es)
}

/// Attempt to re-authorize this response given a mapping from unknowns to values, provided as an iterator.
/// Exhausts the iterator, returning any evaluation errors in the restricted expressions, regardless whether there is a matching unknown.
pub fn reauthorize_with_bindings<'m>(
&self,
mapping: impl IntoIterator<Item = (&'m str, &'m RestrictedExpression)>,
auth: &Authorizer,
es: &Entities,
) -> Result<Self, ReauthorizationError> {
let exts = Extensions::all_available();
let evaluator = RestrictedEvaluator::new(exts);
Expand All @@ -1093,7 +1106,7 @@ impl PartialResponse {
.map(|(name, expr)| {
evaluator
.interpret(BorrowedRestrictedExpr::new_unchecked(expr.0.as_ref()))
.map(|v| (name, v))
.map(|v| (name.into(), v))
})
.collect::<Result<HashMap<_, _>, EvaluationError>>()?;
let r = self.0.reauthorize(&mapping, &auth.0, &es.0)?;
Expand Down

0 comments on commit bbf7813

Please sign in to comment.