pub struct JwtAuthPlugin {
secret: Option<String>,
algorithm: Algorithm,
header_name: String,
use_consumers: bool,
resources: Arc<PluginResources>,
}Expand description
Authenticates requests by verifying a JWT signature and expiry (exp).
The token is read from the configured header (with an optional
Bearer prefix stripped). On success the plugin writes into
context.message:
"jwt_claims": the full decoded claims object"user_id": thesubclaim, when present (convenience copy)
In consumer mode the matched consumer’s identity is also attached
(consumer.* message keys + X-Consumer-* headers).
On failure the request is rejected with a 401 JSON response and a
JWT_INVALID error routed through the error port.
Fields§
§secret: Option<String>Shared HMAC secret used to verify token signatures (inline mode).
None when only consumer mode is configured.
algorithm: AlgorithmHMAC algorithm the token must be signed with in inline mode.
header_name: StringLowercased name of the request header carrying the token.
use_consumers: boolWhen true, tokens are also resolved against the consumer store via
their key claim and verified with the consumer’s own secret.
resources: Arc<PluginResources>Implementations§
Source§impl JwtAuthPlugin
impl JwtAuthPlugin
Sourcepub fn from_config(
config: &HashMap<String, Value>,
resources: &Arc<PluginResources>,
) -> Result<Self, String>
pub fn from_config( config: &HashMap<String, Value>, resources: &Arc<PluginResources>, ) -> Result<Self, String>
Builds the plugin from node config.
Accepted keys:
secret(string, optional): HMAC secret for inline signature verification. When set, every token is verified with it.use_consumers(bool, defaultfalse): resolve the token’skeyclaim against the gateway’sconsumers:section (jwt-auth: {key, secret, algorithm}) and verify with the matched consumer’s secret/algorithm, attaching the consumer on success.- At least one of
secret/use_consumersmust be provided. algorithm(string, default"HS256"): one ofHS256,HS384,HS512used for inline verification; any other value is a config error — an auth plugin must not silently verify with a different algorithm than the one requested.header_name(string, default"authorization"): header to read the token from (compared case-insensitively via lowercasing).
type: jwt-auth
config:
use_consumers: true
header_name: authorizationSourcefn reject(
ctx: Context,
message: &str,
) -> Result<PluginOutput, PluginExecutionError>
fn reject( ctx: Context, message: &str, ) -> Result<PluginOutput, PluginExecutionError>
Builds the 401 rejection with a JSON error body and returns a
PluginExecutionError (code JWT_INVALID) carrying the context so the
graph engine routes through the error port.
Sourcefn verify(
token: &str,
secret: &str,
algorithm: Algorithm,
ctx: &mut Context,
) -> Result<HashMap<String, Value>, String>
fn verify( token: &str, secret: &str, algorithm: Algorithm, ctx: &mut Context, ) -> Result<HashMap<String, Value>, String>
Verifies token with secret/algorithm and, on success, writes the
claims into context.message. Returns the decoded claims on success.
Sourcefn peek_key_claim(token: &str) -> Option<String>
fn peek_key_claim(token: &str) -> Option<String>
Reads the key claim from an unverified token payload.
The payload segment is base64url-decoded and parsed as JSON purely to discover which consumer to look up; the signature is verified only afterwards with that consumer’s secret, so no trust is placed in this value.
Trait Implementations§
Source§impl Plugin for JwtAuthPlugin
impl Plugin for JwtAuthPlugin
Source§fn plugin_type(&self) -> &str
fn plugin_type(&self) -> &str
Source§fn execute<'life0, 'life1, 'async_trait>(
&'life0 self,
ctx: Context,
_named_inputs: &'life1 HashMap<String, Value>,
) -> Pin<Box<dyn Future<Output = Result<PluginOutput, PluginExecutionError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn execute<'life0, 'life1, 'async_trait>(
&'life0 self,
ctx: Context,
_named_inputs: &'life1 HashMap<String, Value>,
) -> Pin<Box<dyn Future<Output = Result<PluginOutput, PluginExecutionError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Auto Trait Implementations§
impl Freeze for JwtAuthPlugin
impl !RefUnwindSafe for JwtAuthPlugin
impl Send for JwtAuthPlugin
impl Sync for JwtAuthPlugin
impl Unpin for JwtAuthPlugin
impl UnsafeUnpin for JwtAuthPlugin
impl !UnwindSafe for JwtAuthPlugin
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§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 more