4 releases
0.1.14 | Oct 17, 2024 |
---|---|
0.1.13 | Oct 17, 2024 |
0.1.12 | Oct 16, 2024 |
0.1.1 | Oct 16, 2024 |
#278 in Authentication
324 downloads per month
Used in rusty_oauth
8KB
84 lines
JWT Cookie Encoder/Decoder in Rust
This Rust module provides functionality to encode and decode JSON Web Tokens (JWT) for user authentication. It uses the http-scrap
,jsonwebtoken
, serde
, and uuid
crates to handle JWTs securely.
Table of Contents
Overview
This module demonstrates how to encode and decode JWTs, using secret keys stored in environment variables. JWTs are commonly used for user authentication, storing details like user IDs, names, and expiration times.
Dependencies
Make sure to add the following dependencies to your Cargo.toml
:
[dependencies]
serde = { version = "1.0", features = ["derive"] }
jsonwebtoken = "8.0"
uuid = "1.2"
[encode]
//encode token
let user = Users {
name: String::from("John Doe"),
email: String::from("john.doe@example.com"),
exp: 3600, // Token expires in 1 hour
};
let jwt = Cookie::encode("JWT_SECRET_NAME".to_string(), user).unwrap();
println!("Generated JWT: {}", jwt);
[decode]
//decode
use http_scrap::Response;
let response = Response::new(&response); //use http-scrap
let cookie = response.cookie();
let decoded = Cookie::decode("JWT_SECRET_NAME".to_string(), cookie).unwrap();
println!("Decoded Claims: {:?}", decoded.claims);
Dependencies
~1.2–9.5MB
~112K SLoC