pub struct Jwt<C> { /* private fields */ }Expand description
JWT bearer-token authentication plugin produced by Auth::jwt_hs256.
This plugin reads Authorization: Bearer <jwt> (scheme matched
case-insensitively), verifies the token’s signature and standard claims
against the configured key and validation, and on success inserts the decoded
claims of type C as the principal. Handlers then read those claims via
Principal<C>. A missing token, a bad signature, or a failed validation
(e.g. an expired exp) leaves the call unauthenticated rather than erroring.
Unlike Bearer there is no verify closure: trust comes from the
cryptographic signature, and the claims are deserialized directly into C.
Construct it with Auth::jwt_hs256.
§Type parameter
C— the claims type, which must beserde::de::DeserializeOwned + Clone + Send + Sync + 'static. By defaultjsonwebtokenvalidatesexp, so include anexpfield inC.
§Examples
use churust_core::{Churust, TestClient};
use churust_auth::{Auth, Principal};
use jsonwebtoken::{encode, EncodingKey, Header};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Clone)]
struct Claims { sub: String, exp: usize }
let secret = b"shhh";
let token = encode(
&Header::default(),
&Claims { sub: "u1".into(), exp: 9_999_999_999 },
&EncodingKey::from_secret(secret),
).unwrap();
let app = Churust::server()
.install(Auth::jwt_hs256::<Claims>(secret))
.routing(|r| {
r.get("/who", |Principal(c): Principal<Claims>| async move { c.sub });
})
.build();
let res = TestClient::new(app)
.get("/who")
.header("authorization", &format!("Bearer {token}"))
.send()
.await;
assert_eq!(res.text(), "u1");Trait Implementations§
Auto Trait Implementations§
impl<C> Freeze for Jwt<C>
impl<C> RefUnwindSafe for Jwt<C>
impl<C> Send for Jwt<C>
impl<C> Sync for Jwt<C>
impl<C> Unpin for Jwt<C>
impl<C> UnsafeUnpin for Jwt<C>
impl<C> UnwindSafe for Jwt<C>
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