Skip to main content

AuthorizationHook

Struct AuthorizationHook 

Source
pub struct AuthorizationHook { /* private fields */ }
Available on crate feature enterprise only.
Expand description

Composite authorization hook that short-circuits on first deny.

§Examples

use http_handle::enterprise::AuthorizationHook;
let h = AuthorizationHook::new();
assert_eq!(format!("{h:?}").contains("AuthorizationHook"), true);

§Panics

This type does not panic.

Implementations§

Source§

impl AuthorizationHook

Source

pub fn new() -> Self

Creates an empty authorization hook chain.

§Examples
use http_handle::enterprise::AuthorizationHook;
let _h = AuthorizationHook::new();
assert_eq!(1, 1);
§Panics

This function does not panic.

Source

pub fn with_engine(self, engine: impl AuthorizationEngine + 'static) -> Self

Adds an authorization engine to the chain.

§Examples
use http_handle::enterprise::{AuthorizationContext, AuthorizationDecision, AuthorizationEngine, AuthorizationHook};
struct Allow;
impl AuthorizationEngine for Allow {
    fn evaluate(&self, _context: &AuthorizationContext) -> AuthorizationDecision { AuthorizationDecision::Allow }
}
let _h = AuthorizationHook::new().with_engine(Allow);
assert_eq!(1, 1);
§Panics

This function does not panic.

Source

pub fn evaluate(&self, context: &AuthorizationContext) -> AuthorizationDecision

Evaluates all engines in-order.

§Examples
use http_handle::enterprise::{AuthorizationContext, AuthorizationDecision, AuthorizationEngine, AuthorizationHook};
struct Allow;
impl AuthorizationEngine for Allow {
    fn evaluate(&self, _context: &AuthorizationContext) -> AuthorizationDecision { AuthorizationDecision::Allow }
}
let h = AuthorizationHook::new().with_engine(Allow);
let d = h.evaluate(&AuthorizationContext::default());
assert!(matches!(d, AuthorizationDecision::Allow));
§Panics

This function does not panic.

Source

pub fn evaluate_http_request( &self, request: &Request, subject: impl Into<String>, attributes: HashMap<String, String>, ) -> AuthorizationDecision

Evaluates authorization from an HTTP request.

Use this helper to map request method and path into an authorization context without repeating context construction in each handler.

§Examples
use http_handle::enterprise::{AuthorizationDecision, AuthorizationHook, RbacAdapter};
use http_handle::request::Request;
use std::collections::HashMap;

let auth = AuthorizationHook::new().with_engine(
    RbacAdapter::default()
        .grant_role("service-a", "reader")
        .grant_permission("reader", "/metrics", "GET"),
);
let request = Request {
    method: "GET".to_string(),
    path: "/metrics".to_string(),
    version: "HTTP/1.1".to_string(),
    headers: HashMap::new(),
};

let decision = auth.evaluate_http_request(
    &request,
    "service-a",
    HashMap::new(),
);
assert!(matches!(decision, AuthorizationDecision::Allow));
§Panics

This function does not panic.

Trait Implementations§

Source§

impl Debug for AuthorizationHook

Source§

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

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

impl Default for AuthorizationHook

Source§

fn default() -> AuthorizationHook

Returns the “default value” for a type. Read more

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more