Add README

This commit is contained in:
Pierre HUBERT 2024-04-20 13:17:44 +02:00
parent 6039fa7495
commit 0743532eff
2 changed files with 25 additions and 4 deletions

22
README.md Normal file
View File

@ -0,0 +1,22 @@
# Basic JWT
This crate provide basic functions to:
* Sign JWT
* Parse and validate JWT
Basic usage:
```rust
let claims = ...; // note : claims must be serializable
// Generate a key pair. Public and private key are both serializable
let (pub_key, priv_key) = generate_ec384_keypair().unwrap();
// Create a JWT for the given claims (note: standard claims: sub, iss, ...) are not
// automatically added if they are missing
let jwt = sign_jwt(&priv_key, &claims).expect("Failed to sign JWT!");
// Validate signed JWT
let claims_out = validate_jwt::<Claims>(&pub_key, &jwt).expect("Failed to validate JWT!");
```

View File

@ -65,8 +65,8 @@ pub fn validate_jwt<E: DeserializeOwned>(key: &TokenPubKey, token: &str) -> anyh
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use std::time::{SystemTime, UNIX_EPOCH};
use crate::{generate_ec384_keypair, sign_jwt, validate_jwt}; use crate::{generate_ec384_keypair, sign_jwt, validate_jwt};
use std::time::{SystemTime, UNIX_EPOCH};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -77,7 +77,6 @@ mod test {
.as_secs() .as_secs()
} }
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)] #[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
pub struct Claims { pub struct Claims {
sub: String, sub: String,
@ -98,7 +97,7 @@ mod test {
let (pub_key, priv_key) = generate_ec384_keypair().unwrap(); let (pub_key, priv_key) = generate_ec384_keypair().unwrap();
let claims = Claims::default(); let claims = Claims::default();
let jwt = sign_jwt(&priv_key, &claims).expect("Failed to sign JWT!"); let jwt = sign_jwt(&priv_key, &claims).expect("Failed to sign JWT!");
let claims_out = validate_jwt(&pub_key, &jwt).expect("Failed to validate JWT!"); let claims_out = validate_jwt::<Claims>(&pub_key, &jwt).expect("Failed to validate JWT!");
assert_eq!(claims, claims_out) assert_eq!(claims, claims_out)
} }