pub struct AuthGuard<F: Fn(JwtAuthState) -> AnyHttpError + Send + Sync + 'static> { /* private fields */ }
Expand description
JWT Guard middleware that checks the validation of the request before entry of the handler
Implementations§
Source§impl<F> AuthGuard<F>
impl<F> AuthGuard<F>
Sourcepub fn new(f: F) -> Self
pub fn new(f: F) -> Self
Construct the guard with a handler on how to respond to the clients when validation failed
Examples found in repository?
examples/hello.rs (line 106)
95async fn main() -> anyhow::Result<()> {
96 let config = Config::from_config_file("./config.toml").expect("config file not found");
97 let jwt = authorization::gen_jwt_auth::<JwtClaim>(
98 config.secret_key.clone(),
99 vec![Box::new(HeaderFinder::new())],
100 );
101
102 webserver_rs::serve_routes! {
103 config => [
104 // http://localhost:8080/hello
105 router!([get, post] => @hello)
106 .hoop(authorization::AuthGuard::new(|_e| html_err!("unauthorized"))),
107 // http://localhost:8080/user/login
108 router!([get] => /user/@login),
109 // http://localhost:8080/a/b/show
110 router!([get, post] => a/b/@ab::show),
111 // http://localhost:8080/b/c/show/*
112 router!([get, post, put] => /b/c/@ab::show/<**path>),
113 // http://localhost:8080/test_json
114 router!([get, post] => @text_json).hoop(build_cros("*")),
115 // http://localhost:8080/ab/shop/show
116 router!([get, post] => ...@ab::shop::show).hoop(build_cros("*")),
117 ] & [jwt, /*Middlewares*/]
118 };
119 Ok(())
120}
Trait Implementations§
Source§impl<F> Handler for AuthGuard<F>
impl<F> Handler for AuthGuard<F>
Source§fn handle<'life0, 'life1, 'life2, 'life3, 'life4, 'async_trait>(
&'life0 self,
req: &'life1 mut Request,
depot: &'life2 mut Depot,
res: &'life3 mut Response,
ctrl: &'life4 mut FlowCtrl,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
'life4: 'async_trait,
fn handle<'life0, 'life1, 'life2, 'life3, 'life4, 'async_trait>(
&'life0 self,
req: &'life1 mut Request,
depot: &'life2 mut Depot,
res: &'life3 mut Response,
ctrl: &'life4 mut FlowCtrl,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
'life4: 'async_trait,
Handle http request.
Auto Trait Implementations§
impl<F> Freeze for AuthGuard<F>where
F: Freeze,
impl<F> RefUnwindSafe for AuthGuard<F>where
F: RefUnwindSafe,
impl<F> Send for AuthGuard<F>
impl<F> Sync for AuthGuard<F>
impl<F> Unpin for AuthGuard<F>where
F: Unpin,
impl<F> UnwindSafe for AuthGuard<F>where
F: UnwindSafe,
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more