Add README
This commit is contained in:
parent
6039fa7495
commit
0743532eff
22
README.md
Normal file
22
README.md
Normal 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!");
|
||||||
|
```
|
@ -65,19 +65,18 @@ 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};
|
||||||
|
|
||||||
fn time() -> u64 {
|
fn time() -> u64 {
|
||||||
SystemTime::now()
|
SystemTime::now()
|
||||||
.duration_since(UNIX_EPOCH)
|
.duration_since(UNIX_EPOCH)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.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)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user