JwtService

Struct JwtService 

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

JWT service struct providing token generation and validation functionality.

Implementations§

Source§

impl JwtService

Source

pub fn new( config: JwtConfig, encoding_key: EncodingKey, decoding_key: DecodingKey, validation: Validation, ) -> JwtService

Creates a new JwtService instance.

§Arguments
  • JwtConfig - The JWT configuration.
  • EncodingKey - The encoding key for signing tokens.
  • DecodingKey - The decoding key for verifying tokens.
  • Validation - The validation settings for token verification.
§Returns
  • JwtService - The created service instance.
Source

pub fn get_config(&self) -> &JwtConfig

Returns a reference to the JWT configuration.

§Returns
  • &JwtConfig - The configuration.
Source

pub fn get_encoding_key(&self) -> &EncodingKey

Returns a reference to the encoding key.

§Returns
  • &EncodingKey - The encoding key.
Source

pub fn get_decoding_key(&self) -> &DecodingKey

Returns a reference to the decoding key.

§Returns
  • &DecodingKey - The decoding key.
Source

pub fn get_validation(&self) -> &Validation

Returns a reference to the validation settings.

§Returns
  • &Validation - The validation settings.
Source§

impl JwtService

Implementation block for JwtService core functionality.

Source

pub fn generate_token<S>(&self, subject: S) -> Result<JwtToken, String>
where S: AsRef<str>,

Generates a JWT token for the given subject.

§Arguments
  • AsRef<str> - The subject (user identifier) to include in the token.
§Returns
  • Result<JwtToken, String> - The generated token on success, or an error message on failure.
Source

pub fn validate_token<T>( &self, token: T, ) -> Result<JwtExtraJwtClaims, JwtValidationError>
where T: AsRef<str>,

Validates a JWT token and returns its claims.

§Arguments
  • AsRef<str> - The JWT token to validate.
§Returns
  • Result<JwtExtraJwtClaims, JwtValidationError> - The claims on success, or a validation error on failure.
Source

pub fn get_subject_from_token<T>(&self, token: T) -> Result<String, String>
where T: AsRef<str>,

Extracts the subject from a JWT token.

§Arguments
  • AsRef<str> - The JWT token to extract the subject from.
§Returns
  • Result<String, String> - The subject on success, or an error message on failure.
Source

pub fn is_token_expired<T>(&self, token: T) -> Result<bool, String>
where T: AsRef<str>,

Checks if a JWT token is expired.

§Arguments
  • AsRef<str> - The JWT token to check.
§Returns
  • Result<bool, String> - True if expired, false otherwise, or an error message on failure.
Source§

impl JwtService

Implementation block for JwtService with custom claims support.

Source

pub fn generate_token_with_claims<U, S>( &self, subject: S, claims: U, ) -> Result<JwtToken, String>
where U: Clone + Default + Serialize + for<'de> Deserialize<'de>, S: AsRef<str>,

Generates a JWT token with custom claims.

§Arguments
  • AsRef<str> - The subject (user identifier) to include in the token.
  • Clone + Default + Serialize + for<'de> Deserialize<'de> - The custom claims to include in the token payload.
§Returns
  • Result<JwtToken, String> - The generated token on success, or an error message on failure.
Source

pub fn validate_token_with_claims<U, T>( &self, token: T, ) -> Result<CustomExtraJwtClaims<U>, JwtValidationError>
where U: Clone + Default + Serialize + for<'de> Deserialize<'de>, T: AsRef<str>,

Validates a JWT token and returns its custom claims.

§Arguments
  • AsRef<str> - The JWT token to validate.
§Returns
  • Result<CustomExtraJwtClaims<U>, JwtValidationError> - The custom claims on success, or a validation error on failure.
Source

pub fn generate_token_with_extra_claims<S>( &self, subject: S, extra: HashMap<String, Value>, ) -> Result<JwtToken, String>
where S: AsRef<str>,

Generates a JWT token with extra custom fields.

§Arguments
  • AsRef<str> - The subject (user identifier) to include in the token.
  • HashMap<String, Value> - Additional key-value pairs to include in the token payload.
§Returns
  • Result<JwtToken, String> - The generated token on success, or an error message on failure.
Source

pub fn validate_token_with_extra_claims<T>( &self, token: T, ) -> Result<ExtraJwtClaims, JwtValidationError>
where T: AsRef<str>,

Validates a JWT token and returns its claims with extra fields.

§Arguments
  • AsRef<str> - The JWT token to validate.
§Returns
  • Result<ExtraJwtClaims, JwtValidationError> - The claims with extra fields on success, or a validation error on failure.
Source

pub fn get_from_token<T, K>( &self, token: T, field_key: K, ) -> Result<Option<Value>, JwtValidationError>
where T: AsRef<str>, K: AsRef<str>,

Extracts a specific field value from a JWT token’s extra claims.

§Arguments
  • AsRef<str> - The JWT AsRef<str token to extract the field from.
  • AsRef<str> - The key of the field to retrieve.
§Returns
  • Result<Option<Value>, JwtValidationError> - The field value if found, or None, or a validation error.

Trait Implementations§

Source§

impl Clone for JwtService

Source§

fn clone(&self) -> JwtService

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for JwtService

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl From<JwtConfig> for JwtService

Conversion from JwtConfig to JwtService.

§Arguments

  • JwtConfig - The JWT configuration to convert.

§Returns

  • JwtService - The initialized JWT service.
Source§

fn from(config: JwtConfig) -> JwtService

Converts to this type from the input type.

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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
Source§

impl<T> AnySend for T
where T: Any + Send,

Source§

impl<T> AnySendClone for T
where T: Any + Send + Clone,

Source§

impl<T> AnySendSync for T
where T: Any + Send + Sync,

Source§

impl<T> AnySendSyncClone for T
where T: Any + Send + Sync + Clone,

Source§

impl<T> AnySync for T
where T: Any + Sync,

Source§

impl<T> AnySyncClone for T
where T: Any + Sync + Clone,

Source§

impl<T> BroadcastMapTrait for T
where T: Clone + Debug,

Source§

impl<T> BroadcastTrait for T
where T: Clone + Debug,

Source§

impl<T> ErasedDestructor for T
where T: 'static,