pub struct Bearer<P, F> { /* private fields */ }Expand description
Bearer-token authentication plugin produced by Auth::bearer.
On every request this plugin reads the Authorization: Bearer <token>
header (the scheme word is matched case-insensitively), trims the token, and
calls your verify closure. If the closure returns Some(principal), that
principal is inserted into the call so a later Principal<P> extractor can
pick it up. A missing header, a non-bearer scheme, or a None result simply
leaves the call unauthenticated — it is never rejected here; rejection is the
job of Principal<P>.
You rarely name this type directly; construct it with Auth::bearer and
hand it to AppBuilder::install.
§Type parameters
P— the principal type the closure resolves to.F— the verifier closure type (inferred from the argument).
§Examples
use churust_core::{Churust, TestClient};
use churust_auth::{Auth, Principal};
#[derive(Clone)]
struct User;
let app = Churust::server()
.install(Auth::bearer(|t: String| async move { (t == "ok").then_some(User) }))
.routing(|r| {
r.get("/", |_p: Principal<User>| async { "in" });
})
.build();
let res = TestClient::new(app)
.get("/")
.header("authorization", "Bearer ok")
.send()
.await;
assert_eq!(res.status().as_u16(), 200);Trait Implementations§
Auto Trait Implementations§
impl<P, F> Freeze for Bearer<P, F>
impl<P, F> RefUnwindSafe for Bearer<P, F>where
F: RefUnwindSafe,
impl<P, F> Send for Bearer<P, F>
impl<P, F> Sync for Bearer<P, F>
impl<P, F> Unpin for Bearer<P, F>
impl<P, F> UnsafeUnpin for Bearer<P, F>
impl<P, F> UnwindSafe for Bearer<P, F>where
F: RefUnwindSafe,
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