pub struct AuthzLayer { /* private fields */ }Expand description
Middleware that lets a request through when its caller
(Identity::current, set by an authenticator wrapped before — outside —
this layer) has
a relation to an object: 401 without a caller, 403 when the answer
is no, the object can’t be named, or the check fails (logged).
use rust_web_server::app::App;
use rust_web_server::authz::{AuthzLayer, Authorizer, MemoryStore, Schema};
use rust_web_server::core::New;
let schema = Schema::new().relation("doc", "viewer", "user").unwrap();
let authz = Authorizer::new(schema, MemoryStore::new()).unwrap();
// The first layer wrapped is the outermost: wrap the authenticator first —
// e.g. `.wrap(JwtLayer::new(secret))` (the `auth` feature) — so it has set
// the caller by the time this layer checks.
let app = App::new().wrap(AuthzLayer::new(authz, "viewer", "doc:{path:2}").unwrap()); // /docs/42 → doc:42Implementations§
Source§impl AuthzLayer
impl AuthzLayer
Sourcepub fn new(
authz: Authorizer,
relation: &str,
template: &str,
) -> Result<AuthzLayer, String>
pub fn new( authz: Authorizer, relation: &str, template: &str, ) -> Result<AuthzLayer, String>
Require relation on the object template names (see
ObjectTemplate); the relation must exist on the template’s type.
Trait Implementations§
Source§impl Clone for AuthzLayer
impl Clone for AuthzLayer
Source§impl Debug for AuthzLayer
impl Debug for AuthzLayer
Source§impl Middleware for AuthzLayer
impl Middleware for AuthzLayer
fn handle( &self, request: &Request, connection: &ConnectionInfo, next: &dyn Application, ) -> Result<Response, String>
Source§fn accepts_streamed_body(
&self,
_request: &Request,
_connection: &ConnectionInfo,
) -> bool
fn accepts_streamed_body( &self, _request: &Request, _connection: &ConnectionInfo, ) -> bool
true when this layer never reads request.body, so the inner
application may receive it as a stream (Application::streams_body).
A layer that inspects the body (CSRF tokens in a form, request
signatures, rewriting) must keep the default false: it would see an
empty body.Source§fn runs_inline(&self, _request: &Request, _connection: &ConnectionInfo) -> bool
fn runs_inline(&self, _request: &Request, _connection: &ConnectionInfo) -> bool
true when this layer’s own work for request never blocks (in-memory
state, atomics, short mutexes) — see Application::runs_inline. A
WithMiddleware stack runs inline only if every layer and the inner
application agree. Default false.Source§fn is_passthrough(
&self,
_request: &Request,
_connection: &ConnectionInfo,
) -> bool
fn is_passthrough( &self, _request: &Request, _connection: &ConnectionInfo, ) -> bool
true when handle would do nothing for request but call next
(e.g. a rate limit that isn’t configured). A WithMiddleware stack
offers the inner application’s execute_async only if every layer
passes the request through. Default false.Source§fn answers_streamed_body(
&self,
_request: &Request,
_connection: &ConnectionInfo,
) -> bool
fn answers_streamed_body( &self, _request: &Request, _connection: &ConnectionInfo, ) -> bool
true when this layer answers request itself — it doesn’t call
next — and reads its body as a stream (a proxy forwarding it as it
arrives). The body is then streamed whatever the layers after it and
the application would say. Requires accepts_streamed_body. Since
18.3.0.Auto Trait Implementations§
impl !RefUnwindSafe for AuthzLayer
impl !UnwindSafe for AuthzLayer
impl Freeze for AuthzLayer
impl Send for AuthzLayer
impl Sync for AuthzLayer
impl Unpin for AuthzLayer
impl UnsafeUnpin for AuthzLayer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more