Refined docs and stuff

Docs link to each other and are generally better
This commit is contained in:
2026-05-20 12:50:00 +02:00
parent 0db9b76cad
commit 721e43c380
14 changed files with 256 additions and 159 deletions
+7 -6
View File
@@ -6,8 +6,8 @@ use crate::models::Claims;
/// Error response for JWT token operations.
///
/// Returned when token encoding or decoding fails. Used in error responses
/// for invalid or expired tokens.
/// Returned when token encoding or decoding fails via `encode_token` or `decode_token`.
/// Used in error responses for invalid or expired [`Claims`] tokens.
///
/// # Fields
/// - `status`: HTTP status text (e.g., "error")
@@ -22,7 +22,7 @@ pub struct Error {
///
/// This function creates a new JWT with the provided user ID as the subject,
/// sets the issued-at and expiration times (60 minutes from now), and signs it
/// using the given encoding key.
/// using the given encoding key. The resulting token is a serialized [`Claims`].
///
/// # Arguments
/// - `header`: The JWT header, specifying the algorithm (e.g., HS256).
@@ -30,7 +30,7 @@ pub struct Error {
/// - `key`: The `EncodingKey` used to sign the JWT.
///
/// # Returns
/// A `String` representing the encoded JWT.
/// A `String` representing the encoded JWT containing [`Claims`].
///
/// # Panics
/// Panics if the token encoding fails for any reason (e.g., invalid key).
@@ -50,14 +50,15 @@ pub fn encode_token(header: &Header, id: String, key: &EncodingKey) -> String {
///
/// This function attempts to decode a JWT string, validate its signature and claims
/// using the provided decoding key. It specifically ignores expiration (`validate_exp`)
/// and "not before" (`validate_nbf`) claims during validation.
/// and "not before" (`validate_nbf`) claims during validation. Returns the extracted [`Claims`]
/// on success.
///
/// # Arguments
/// - `token`: The JWT string to decode.
/// - `key`: The `DecodingKey` used to verify the JWT's signature.
///
/// # Returns
/// - `Ok(Claims)`: If the token is successfully decoded and verified, returns the extracted `Claims`.
/// - `Ok(Claims)`: If the token is successfully decoded and verified, returns the extracted [`Claims`].
/// - `Err((StatusCode, Json<Error>))`: If the token is invalid, expired, or cannot be decoded,
/// returns an `UNAUTHORIZED` status code along with a JSON error message.
pub fn decode_token(token: String, key: &DecodingKey) -> Result<Claims, (StatusCode, Json<Error>)> {