Struct AuthGuard

Source
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>
where F: Fn(JwtAuthState) -> AnyHttpError + Send + Sync + 'static,

Source

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>
where F: Fn(JwtAuthState) -> AnyHttpError + Send + Sync + 'static,

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,

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

Source§

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

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

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

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

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
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

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

impl<T> ErasedDestructor for T
where T: 'static,