pub struct JwtLayer { /* private fields */ }auth only.Expand description
Middleware that verifies Authorization: Bearer <token> JWTs.
JwtLayer::new verifies HMAC-SHA256 (HS256) tokens against a shared
secret. With the auth-asymmetric feature, JwtLayer::rs256 and
JwtLayer::es256 verify tokens signed by a separate auth server’s
private key, given only its public key.
Rejects tokens with a past exp claim. All other validation (format,
algorithm, signature) is performed by verify_jwt / verify_jwt_rs256
/ verify_jwt_es256.
If a handler also needs the decoded claims, call the matching verify_*
function again inside the handler — verification is cheap.
§Example
use rust_web_server::app::App;
use rust_web_server::auth::JwtLayer;
use rust_web_server::core::New;
let app = App::new().wrap(JwtLayer::new(b"my-signing-secret"));Implementations§
Source§impl JwtLayer
impl JwtLayer
Sourcepub fn new(secret: impl Into<Vec<u8>>) -> JwtLayer
pub fn new(secret: impl Into<Vec<u8>>) -> JwtLayer
Create a layer that verifies HS256 JWTs signed with secret.
Sourcepub fn secrets<I, S>(secrets: I) -> JwtLayer
pub fn secrets<I, S>(secrets: I) -> JwtLayer
Create a layer that verifies HS256 JWTs signed with any of secrets
(18.20.0): rotate by adding the new secret, keeping the old one until
the tokens it signed have expired, then removing it.
Sourcepub fn secret_with_kid(
self,
kid: impl Into<String>,
secret: impl Into<Vec<u8>>,
) -> JwtLayer
pub fn secret_with_kid( self, kid: impl Into<String>, secret: impl Into<Vec<u8>>, ) -> JwtLayer
Also accept HS256 tokens signed with secret; a token whose header
says "kid": kid is checked against this secret only.
Sourcepub fn rs256(public_key_pem: &str) -> Result<JwtLayer, String>
Available on crate feature auth-asymmetric only.
pub fn rs256(public_key_pem: &str) -> Result<JwtLayer, String>
auth-asymmetric only.Create a layer that verifies RS256 JWTs against an RSA public key.
public_key_pem is a -----BEGIN PUBLIC KEY----- block
(SubjectPublicKeyInfo — openssl rsa -in key.pem -pubout,
openssl req -pubkey) or a -----BEGIN RSA PUBLIC KEY----- (PKCS#1)
block; 2048–8192-bit keys.
§Errors
Returns Err if public_key_pem is not a valid PEM-encoded RSA
public key.
Sourcepub fn es256(public_key_pem: &str) -> Result<JwtLayer, String>
Available on crate feature auth-asymmetric only.
pub fn es256(public_key_pem: &str) -> Result<JwtLayer, String>
auth-asymmetric only.Create a layer that verifies ES256 JWTs against a P-256 public key.
public_key_pem must be in SubjectPublicKeyInfo PEM format (the
-----BEGIN PUBLIC KEY----- block produced by, e.g.,
openssl ec -in key.pem -pubout).
§Errors
Returns Err if public_key_pem is not a valid PEM-encoded P-256
public key.
Trait Implementations§
Source§impl Middleware for JwtLayer
impl Middleware for JwtLayer
Source§fn accepts_streamed_body(
&self,
_request: &Request,
_connection: &ConnectionInfo,
) -> bool
fn accepts_streamed_body( &self, _request: &Request, _connection: &ConnectionInfo, ) -> bool
Headers only: a streamed body passes through untouched.
fn handle( &self, request: &Request, connection: &ConnectionInfo, next: &dyn Application, ) -> Result<Response, String>
Source§fn runs_inline(&self, _request: &Request, _connection: &ConnectionInfo) -> bool
fn runs_inline(&self, _request: &Request, _connection: &ConnectionInfo) -> bool
true when this layer’s own work for request never blocks (in-memory
state, atomics, short mutexes) — see Application::runs_inline. A
WithMiddleware stack runs inline only if every layer and the inner
application agree. Default false.Source§fn is_passthrough(
&self,
_request: &Request,
_connection: &ConnectionInfo,
) -> bool
fn is_passthrough( &self, _request: &Request, _connection: &ConnectionInfo, ) -> bool
true when handle would do nothing for request but call next
(e.g. a rate limit that isn’t configured). A WithMiddleware stack
offers the inner application’s execute_async only if every layer
passes the request through. Default false.Source§fn answers_streamed_body(
&self,
_request: &Request,
_connection: &ConnectionInfo,
) -> bool
fn answers_streamed_body( &self, _request: &Request, _connection: &ConnectionInfo, ) -> bool
true when this layer answers request itself — it doesn’t call
next — and reads its body as a stream (a proxy forwarding it as it
arrives). The body is then streamed whatever the layers after it and
the application would say. Requires accepts_streamed_body. Since
18.3.0.Source§fn layer_name(&self) -> &'static str
fn layer_name(&self) -> &'static str
Application::middleware_names): by default the type’s name
without its module path or generic parameters (CacheLayer). Since
18.28.0.Auto Trait Implementations§
impl Freeze for JwtLayer
impl RefUnwindSafe for JwtLayer
impl Send for JwtLayer
impl Sync for JwtLayer
impl Unpin for JwtLayer
impl UnsafeUnpin for JwtLayer
impl UnwindSafe for JwtLayer
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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> ⓘ
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> ⓘ
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<T> ServiceExt for T
impl<T> ServiceExt for T
Source§fn map_response_body<F>(self, f: F) -> MapResponseBody<Self, F>where
Self: Sized,
fn map_response_body<F>(self, f: F) -> MapResponseBody<Self, F>where
Self: Sized,
map-response-body only.Source§fn trace_for_http(self) -> Trace<Self, SharedClassifier<ServerErrorsAsFailures>>where
Self: Sized,
fn trace_for_http(self) -> Trace<Self, SharedClassifier<ServerErrorsAsFailures>>where
Self: Sized,
trace only.Source§fn trace_for_grpc(self) -> Trace<Self, SharedClassifier<GrpcErrorsAsFailures>>where
Self: Sized,
fn trace_for_grpc(self) -> Trace<Self, SharedClassifier<GrpcErrorsAsFailures>>where
Self: Sized,
trace only.