Skip to main content

JwtPlugin

Struct JwtPlugin 

Source
pub struct JwtPlugin { /* private fields */ }
Expand description

A minimal JWT implementation using HMAC-SHA256 (HS256).

For production, consider a full JWT library. This implementation uses real cryptographic primitives (HMAC-SHA256 via the hmac and sha2 crates).

Implementations§

Source§

impl JwtPlugin

Source

pub fn new(secret: &str, expiry_secs: u64) -> Self

Source

pub fn issue(&self, user_id: &str) -> String

Issue a short-lived access JWT for a user ID.

Source

pub fn issue_with_kind( &self, user_id: &str, kind: &str, expiry_secs: u64, ) -> String

Issue a JWT with an explicit kind and expiry.

Source

pub fn issue_pair(&self, user_id: &str, refresh_expiry_secs: u64) -> TokenPair

Issue a token pair: a short-lived access token and a long-lived refresh token. The access token uses the plugin’s configured expiry; the refresh token uses the provided refresh_expiry_secs.

Source

pub fn refresh(&self, refresh_token: &str) -> Result<TokenPair, String>

Consume a refresh token and issue a new token pair.

Order of operations matters for security:

  1. Cryptographically verify the token FIRST. If we inserted into the replay cache before verification, an attacker could pollute the cache by posting random garbage, growing it unbounded. Worse, a real token presented alongside that garbage would get “burned” before we knew whether it was even valid.
  2. Then check the replay cache and atomically insert.

The window between verify() and insert() is a TOCTOU where two concurrent refreshes of the same token could both succeed. The Mutex around used_refresh_tokens is the serialization point — the check + insert happens under the same lock.

Source

pub fn verify(&self, token: &str) -> Result<Claims, String>

Verify and decode a JWT. Returns claims if valid and not expired. Uses constant-time comparison for the signature to prevent timing attacks.

Source

pub fn resolve_user(&self, token: &str) -> Option<String>

Resolve a JWT to a user ID. Returns None if invalid.

Trait Implementations§

Source§

impl Plugin for JwtPlugin

Source§

fn name(&self) -> &str

Unique name for this plugin.
Source§

fn on_init(&self, _ctx: &PluginContext)

Called once when the plugin is registered.
Source§

fn routes(&self) -> Vec<PluginRoute>

Custom API routes this plugin handles.
Source§

fn before_insert( &self, _entity: &str, _data: &mut Value, _auth: &AuthContext, ) -> Result<(), PluginError>

Called before an entity insert. Return Err to reject.
Source§

fn after_insert( &self, _entity: &str, _id: &str, _data: &Value, _auth: &AuthContext, )

Called after a successful insert.
Source§

fn before_update( &self, _entity: &str, _id: &str, _data: &mut Value, _auth: &AuthContext, ) -> Result<(), PluginError>

Called before an entity update. Return Err to reject.
Source§

fn after_update( &self, _entity: &str, _id: &str, _data: &Value, _auth: &AuthContext, )

Called after a successful update.
Source§

fn before_delete( &self, _entity: &str, _id: &str, _auth: &AuthContext, ) -> Result<(), PluginError>

Called before an entity delete. Return Err to reject.
Source§

fn after_delete(&self, _entity: &str, _id: &str, _auth: &AuthContext)

Called after a successful delete.
Source§

fn on_request( &self, _method: &str, _path: &str, _auth: &AuthContext, ) -> Result<(), PluginError>

Called on every incoming request (middleware).
Source§

fn on_request_with_meta( &self, method: &str, path: &str, auth: &AuthContext, _meta: &RequestMeta<'_>, ) -> Result<(), PluginError>

Richer variant of [on_request] that also receives per-request metadata (peer IP today; more fields may be added later). The default implementation delegates to on_request so existing plugins keep working without changes. Plugins that care about IP — notably rate limiting — override this hook.
Source§

fn on_session_create(&self, _user_id: &str, _token: &str)

Called when a new session is created.
Source§

fn entities(&self) -> Vec<ManifestEntity>

Additional manifest entities this plugin contributes.

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
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.

Source§

impl<T> Instrument for T

Source§

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

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

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

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

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

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

fn with_current_subscriber(self) -> WithDispatch<Self>

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