sshca/tests/common/token.rs

42 lines
956 B
Rust

use std::time;
use argon2::Argon2;
use jsonwebtoken::{encode, EncodingKey};
use serde::Serialize;
use uuid::Uuid;
#[derive(Debug, Serialize)]
struct TestClaims {
sub: String,
iss: String,
aud: String,
iat: u64,
nbf: u64,
exp: u64,
}
pub fn make_token(hostname: &str, machine_id: Uuid) -> String {
let now = time::SystemTime::now()
.duration_since(time::UNIX_EPOCH)
.unwrap()
.as_secs();
let claims = TestClaims {
sub: hostname.into(),
iss: hostname.into(),
aud: "sshca.example.org".into(),
nbf: now - 60,
iat: now,
exp: now + 60,
};
let mut secret = [0u8; 32];
Argon2::default()
.hash_password_into(
machine_id.as_bytes(),
hostname.as_bytes(),
&mut secret,
)
.unwrap();
let key = EncodingKey::from_secret(&secret);
encode(&Default::default(), &claims, &key).unwrap()
}