TODO
- Add more examples
- Improve coverage
Features
- Manages & Orchestrates JWT for user login, logout & renew
- Async ready
- Easy start
- No un-safe code
- Runs on stable rust
- Library approach (Requires no runtime)
- Supports plugable components
- Invalidates old refresh upon new refresh token renewal
- Invalidates old authentication upon new authentication token renewal
- Handles Thundering herd problem upon authentication token expiry
Quickstart
Prerequisite:
[dependencies]
jwtvault = "*"
$ curl https://raw.githubusercontent.com/sgrust01/jwtvault/master/generate_certificates.sh > ./generate_certificates.sh
$ chmod 700 generate_certificates.sh && ./generate_certificates.sh
use jwtvault::prelude::*;
use std::collections::HashMap;
use std::collections::hash_map::DefaultHasher;
fn main() {
let mut users = HashMap::new();
let user_john = "john_doe";
let password_for_john = "john";
let user_jane = "jane_doe";
let password_for_jane = "jane";
users.insert(user_john.to_string(), password_for_john.to_string());
users.insert(user_jane.to_string(), password_for_jane.to_string());
let loader = CertificateManger::default();
let mut vault = DefaultVault::new(loader, users);
let token = block_on(vault.login(
user_john,
password_for_john,
None,
None,
));
let token = token.ok().unwrap();
let server_refresh_token = block_on(resolve_session_from_client_authentication_token(
&mut vault,
user_john,
token.authentication(),
));
let server_refresh_token = server_refresh_token.ok().unwrap();
let private_info_about_john = server_refresh_token.server().unwrap();
let key = digest::<_, DefaultHasher>(user_john);
let data_on_server_side = private_info_about_john.get(&key).unwrap();
assert!(server_refresh_token.client().is_none());
println!("[Private] John Info: {}",
String::from_utf8_lossy(data_on_server_side.as_slice()).to_string());
let new_token = block_on(vault.renew(
user_john,
token.refresh(),
None,
));
let new_token = new_token.ok().unwrap();
let result = block_on(resolve_session_from_client_authentication_token(
&mut vault,
user_john,
new_token.as_str(),
));
let _ = result.ok().unwrap();
}
Workflows
-
To begin use login
with user and password
-
Upon successful login is provides user will be provided with JWT pair (authentication/refresh)
-
Authentication token is then provided to access any resources
-
Refresh token is used to renew an authentication token upon expiry
-
Use resolve_session_from_client_authentication_token
with user and authentication_token to restore user session
-
Use renew
with user and refresh_token to generate new authentication token
-
Use logout
with user and authentication_token will remove all tokens associated with the user