Skip to content

Commit bf7cedb

Browse files
committed
feat: return the matched allow policy in authorize()
not just the policy id. this makes the API easier to understand (in many cases, the first policy is matched, so the return value is `0`, which is falsy)
1 parent 22bbbcc commit bf7cedb

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

biscuit_test.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def test_complete_lifecycle():
267267

268268
policy = authorizer.authorize()
269269

270-
assert policy == 0
270+
assert policy == {'code': 'allow if user("1234")', 'policy_id': 0}
271271

272272
rule = Rule("u($id) <- user($id), $id == {id}", { 'id': "1234"})
273273
facts = authorizer.query(rule)
@@ -300,7 +300,7 @@ def test_snapshot():
300300

301301
policy = parsed.authorize()
302302

303-
assert policy == 0
303+
assert policy == {'code': 'allow if user("1234")', 'policy_id': 0}
304304

305305
rule = Rule("u($id) <- user($id), $id == {id}", { 'id': "1234"})
306306
facts = parsed.query(rule)
@@ -315,7 +315,7 @@ def test_snapshot():
315315

316316
raw_policy = parsed_from_raw.authorize()
317317

318-
assert raw_policy == 0
318+
assert raw_policy == {'code': 'allow if user("1234")', 'policy_id': 0}
319319

320320
rule = Rule("u($id) <- user($id), $id == {id}", { 'id': "1234"})
321321
raw_facts = parsed_from_raw.query(rule)
@@ -471,5 +471,4 @@ def test(left, right):
471471
'other': lambda x : x == 2,
472472
})
473473
policy = authorizer.build_unauthenticated().authorize()
474-
assert policy == 0
475-
474+
assert policy == {'code': 'allow if 1.extern::test(1)', 'policy_id': 0}

src/lib.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ struct AuthorizationErrorData {
4747
}
4848

4949
#[derive(IntoPyObject)]
50-
struct MatchedPolicyData {
50+
pub struct MatchedPolicyData {
5151
policy_id: usize,
5252
code: String,
5353
}
@@ -807,15 +807,16 @@ impl PyAuthorizer {
807807
///
808808
/// :return: the index of the matched allow rule
809809
/// :rtype: int
810-
pub fn authorize(&mut self) -> PyResult<usize> {
811-
self.0.authorize().map_err(|error| match error {
810+
pub fn authorize(&mut self) -> PyResult<MatchedPolicyData> {
811+
let all_policies = self.0.dump().3;
812+
let policy_id = self.0.authorize().map_err(|error| match error {
812813
error::Token::FailedLogic(error::Logic::Unauthorized {
813814
policy: MatchedPolicy::Deny(pid),
814815
checks,
815816
}) => AuthorizationError::new_err(AuthorizationErrorData {
816817
matched_policy: Some(MatchedPolicyData {
817818
policy_id: pid,
818-
code: self.0.dump().3.get(pid).unwrap().to_string(),
819+
code: all_policies.get(pid).unwrap().to_string(),
819820
}),
820821
checks: checks
821822
.into_iter()
@@ -838,6 +839,11 @@ impl PyAuthorizer {
838839
.collect(),
839840
}),
840841
_ => AuthorizationError::new_err(error.to_string()),
842+
})?;
843+
844+
Ok(MatchedPolicyData {
845+
policy_id,
846+
code: all_policies.get(policy_id).unwrap().to_string(),
841847
})
842848
}
843849

0 commit comments

Comments
 (0)