pub struct Basic<P, F> { /* private fields */ }Expand description
HTTP Basic authentication plugin produced by Auth::basic.
On every request this plugin reads Authorization: Basic <base64> (scheme
matched case-insensitively), base64-decodes it, splits the username:password
pair on the first :, and calls your verify closure. Some(principal)
authenticates the call by inserting the principal for a later
Principal<P> extractor; None, a header that is not valid base64, or one
without a : leaves the call unauthenticated. As with all schemes in this
crate it never rejects the request itself.
Construct it with Auth::basic rather than naming this type directly. Note
that Basic credentials are only base64-encoded, not encrypted, so serve such
routes over TLS.
§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 { name: String }
let app = Churust::server()
.install(Auth::basic(|u: String, p: String| async move {
(p == "hunter2").then(|| User { name: u })
}))
.routing(|r| {
r.get("/me", |Principal(u): Principal<User>| async move { u.name });
})
.build();
// base64("ada:hunter2") == "YWRhOmh1bnRlcjI="
let res = TestClient::new(app)
.get("/me")
.header("authorization", "Basic YWRhOmh1bnRlcjI=")
.send()
.await;
assert_eq!(res.text(), "ada");Trait Implementations§
Auto Trait Implementations§
impl<P, F> Freeze for Basic<P, F>
impl<P, F> RefUnwindSafe for Basic<P, F>where
F: RefUnwindSafe,
impl<P, F> Send for Basic<P, F>
impl<P, F> Sync for Basic<P, F>
impl<P, F> Unpin for Basic<P, F>
impl<P, F> UnsafeUnpin for Basic<P, F>
impl<P, F> UnwindSafe for Basic<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