PolicyAuthorizer

Struct PolicyAuthorizer 

Source
pub struct PolicyAuthorizer<'a> { /* private fields */ }
Expand description

Evaluates authorization rules (SRP - extracted from Policy)

This struct is responsible only for evaluating whether a given peer/action/resource combination is allowed by a set of rules.

It does NOT handle:

  • Policy construction
  • Policy validation
  • Policy serialization
  • Rule management

§Example

use core_policy::{PolicyRule, Action, Resource};
use core_policy::authorizer::PolicyAuthorizer;

let rules = vec![
    PolicyRule::new("alice".to_string(), Action::Read, Resource::All),
];

let authorizer = PolicyAuthorizer::new(&rules);
assert!(authorizer.is_allowed("alice", &Action::Read, &Resource::File("/docs/file.txt".into())));
assert!(!authorizer.is_allowed("bob", &Action::Read, &Resource::File("/docs/file.txt".into())));

Implementations§

Source§

impl<'a> PolicyAuthorizer<'a>

Source

pub const fn new(rules: &'a [PolicyRule]) -> Self

Create a new authorizer with the given rules

Source

pub fn is_allowed( &self, peer_id: &str, action: &Action, resource: &Resource, ) -> bool

Check if a peer is allowed to perform an action on a resource (RBAC)

This performs basic Role-Based Access Control checking without time or context validation.

§Arguments
  • peer_id - The peer attempting the action
  • action - The action to perform
  • resource - The resource to access
§Returns

true if at least one rule allows the access, false otherwise

Source

pub fn is_allowed_with_context( &self, peer_id: &str, action: &Action, resource: &Resource, current_time: u64, context: &BTreeMap<String, String>, ) -> bool

Check if a peer is allowed with full ABAC validation

This performs Attribute-Based Access Control checking including:

  • Basic RBAC (peer/action/resource)
  • Time-based validation (expiration)
  • Context attributes validation
§Arguments
  • peer_id - The peer attempting the action
  • action - The action to perform
  • resource - The resource to access
  • current_time - Current Unix timestamp for expiration checks
  • context - Context attributes for ABAC
§Returns

true if at least one rule allows the access with valid time and context, false otherwise

Source

pub fn matching_rules( &'a self, peer_id: &'a str, action: &'a Action, resource: &'a Resource, ) -> impl Iterator<Item = &'a PolicyRule> + 'a

Get all rules that allow a specific peer/action/resource combination

Useful for auditing and debugging authorization decisions.

§Returns

An iterator over all matching rules

Source

pub fn rule_count(&self) -> usize

Get the number of rules being evaluated

Trait Implementations§

Source§

impl<'a> Authorizer for PolicyAuthorizer<'a>

Source§

fn is_allowed( &self, peer_id: &str, action: &Action, resource: &Resource, ) -> bool

Check if access is allowed
Source§

impl<'a> Debug for PolicyAuthorizer<'a>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for PolicyAuthorizer<'a>

§

impl<'a> RefUnwindSafe for PolicyAuthorizer<'a>

§

impl<'a> Send for PolicyAuthorizer<'a>

§

impl<'a> Sync for PolicyAuthorizer<'a>

§

impl<'a> Unpin for PolicyAuthorizer<'a>

§

impl<'a> UnwindSafe for PolicyAuthorizer<'a>

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> 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, 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.