Skip to main content

enforce_http_request_authorization

Function enforce_http_request_authorization 

Source
pub fn enforce_http_request_authorization(
    hook: &AuthorizationHook,
    request: &Request,
    subject: impl Into<String>,
    attributes: HashMap<String, String>,
) -> Result<(), ServerError>
Available on crate feature enterprise only.
Expand description

Enforces authorization for an HTTP request.

§Examples

use http_handle::enterprise::{enforce_http_request_authorization, 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", "/health", "GET"),
);
let request = Request {
    method: "GET".to_string(),
    path: "/health".to_string(),
    version: "HTTP/1.1".to_string(),
    headers: HashMap::new(),
};

let result = enforce_http_request_authorization(
    &auth,
    &request,
    "service-a",
    HashMap::new(),
);
assert!(result.is_ok());

§Errors

Returns Err(ServerError::Forbidden) when any authorization engine denies.

§Panics

This function does not panic.