auth0-jwt

Crates.ioauth0-jwt
lib.rsauth0-jwt
version0.1.0
sourcesrc
created_at2022-12-19 18:34:09.32064
updated_at2022-12-19 18:34:09.32064
descriptionLibrary to validate JWTs from Auth0
homepagehttps://github.com/rawnly/auth0-jwt
repositoryhttps://github.com/rawnly/auth0-jwt
max_upload_size
id741456
size11,224
Federico (rawnly)

documentation

README

Auth0 JWT

Auth0 utility to check if the given JWT is valid.

Usage

use auth0_jwt::get_claims;

#[tokio::async]
async fn main() {
  let token = env!("JWT_TOKEN");
  let claims = get_claims(&token).unwrap();

  println!("Claims {}", claims);
}

Features

  • claims - Exports the Claims struct which is useful for deserialization.
  • axum - Implements the FromRequestParts<T> trait for Claims and provides a Token(String) extractor for convenience.

Axum Example

use auth0_jwt::claims::Claims;
use axum::{response::IntoResponse, routing::get, Json, Router};
use dotenv::dotenv;
use serde::{Deserialize, Serialize};
use std::net::SocketAddr;

#[tokio::main]
async fn main() {
    dotenv().ok();

    // build our application with a route
    let app = Router::new().route("/", get(handler));

    // run it
    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
    println!("listening on {}", addr);
    axum::Server::bind(&addr)
        .serve(app.into_make_service())
        .await
        .unwrap();
}

#[derive(Serialize)]
struct ResponseBody {
    message: &'static str,
}

#[derive(Deserialize, Serialize)]
struct ClaimsContent {
    pub exp: usize,
    pub iat: usize
}

async fn handler(Claims(claims): Claims<ClaimsContent>) -> impl IntoResponse {
    println!("{:?}", claims.exp);

    Json(ResponseBody {
        message: "hello world",
    })
}
Commit count: 8

cargo fmt