Skip to main content

JwtAuthPlugin

Struct JwtAuthPlugin 

Source
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": the sub claim, 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: Algorithm

HMAC algorithm the token must be signed with in inline mode.

§header_name: String

Lowercased name of the request header carrying the token.

§use_consumers: bool

When 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

Source

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, default false): resolve the token’s key claim against the gateway’s consumers: 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_consumers must be provided.
  • algorithm (string, default "HS256"): one of HS256, HS384, HS512 used 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: authorization
Source

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.

Source

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.

Source

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

Source§

fn plugin_type(&self) -> &str

Unique identifier for the plugin type (e.g., “proxy-rewrite”, “upstream”).
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,

Executes the plugin logic against the request/response context. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> MaybeSend for T