ts-token 0.8.0

JSON web token library for my projects
Documentation
//! A decoded JSON web token <https://www.rfc-editor.org/rfc/rfc7519>

use base64ct::{Base64UrlUnpadded, Encoding};
use serde::{Deserialize, Serialize};

/// A decoded JSON web token <https://www.rfc-editor.org/rfc/rfc7519>
#[derive(Debug, Clone)]
pub struct JsonWebToken {
    /// The token's header
    pub header: Header,
    /// The claims on the token.
    pub claims: Claims,
}

/// The token header values <https://www.iana.org/assignments/jose/jose.xhtml#web-signature-encryption-header-parameters>.
#[derive(Deserialize, Clone, Debug, Serialize)]
pub struct Header {
    /// The algorithm used to sign this JSON web token.
    pub alg: String,
    /// The type of this token, should be `JWT`.
    pub typ: String,
    /// The ID of the key used to sign this token.
    pub kid: String,
    /// The URL of the JSON web key set that contains the key used to sign this token.
    pub jku: String,
}

/// The token claims <https://www.iana.org/assignments/jwt/jwt.xhtml>.
#[derive(Deserialize, Clone, Debug, Serialize)]
pub struct Claims {
    /// The token ID.
    pub jti: String,
    /// The token issuer URL.
    pub iss: String,
    /// The expiration time, seconds since `1970-01-01T00:00:00Z`
    pub exp: u64,
    /// The time in seconds since `1970-01-01T00:00:00Z` when the token was issued.
    pub iat: u64,
    /// The subject of the token.
    pub sub: String,
    /// The client ID of the service that requested the token.
    pub aud: String,
    /// The space separated scopes this token is valid for.
    pub scopes: String,
}

impl JsonWebToken {
    /// Returns the JWKS URL and key ID used to sign the token.
    pub fn get_key_details(jws: String) -> Option<(String, String)> {
        let header = jws.split('.').next()?;
        let header = Base64UrlUnpadded::decode_vec(header).ok()?;
        let header: Header = serde_json::from_slice(&header).ok()?;
        Some((header.jku, header.kid))
    }
}