| Crates.io | crabtoken |
| lib.rs | crabtoken |
| version | 0.0.1 |
| created_at | 2025-01-09 05:04:13.806872+00 |
| updated_at | 2025-01-09 05:04:13.806872+00 |
| description | An alternative to JWT/Jsonwebtoken or Paseto |
| homepage | |
| repository | https://github.com/Stipulations/CrabToken |
| max_upload_size | |
| id | 1509592 |
| size | 11,040 |
An in house alternative to JWT and Paseto
serdeExpirable trait for expiration handlingContributions are always welcome!
See something that should be done better or that i was stupid enought to miss, please make a fork and request a PR and once approved ill pull to main.
use crabtoken::{create_token, decode_token, verify_token, Expirable};
use chrono::Utc;
use serde::{Deserialize, Serialize};
use std::{thread, time};
#[derive(Serialize, Deserialize, Debug)]
pub struct CustomPayload {
pub user_id: String,
pub exp: i64,
pub data: String,
}
impl Expirable for CustomPayload {
fn exp(&self) -> i64 {
self.exp
}
}
fn main() {
let secret = "Just a cat eating tacos";
let payload = CustomPayload {
user_id: "user123".to_string(),
exp: Utc::now().timestamp() + 10,
data: "Custom data!".to_string(),
};
let token = create_token(&payload, secret).unwrap();
println!("Generated Token: {}", token);
match verify_token::<CustomPayload>(secret, &token) {
Ok(verified_payload) => println!("{:?}", verified_payload),
Err(e) => println!("{}", e),
}
match decode_token::<CustomPayload>(&token) {
Ok(decoded_payload) => {
println!("Decoded Payload:");
println!("User ID: {}", decoded_payload.user_id);
println!("Expiration: {}", decoded_payload.exp);
println!("Data: {}", decoded_payload.data);
}
Err(e) => println!("Error decoding token: {}", e),
}
println!("Sleeping for 12 seconds to let the token expire...");
thread::sleep(time::Duration::from_secs(12));
match verify_token::<CustomPayload>(secret, &token) {
Ok(verified_payload) => println!("{:?}", verified_payload),
Err(e) => println!("{}", e),
}
}
This project is used by the following companies:
not a soul cuz why would they?
me maybe one day? perhaps after i get my SaaS started but thats eta 2050