0
0
mirror of https://github.com/tursodatabase/libsql.git synced 2024-11-25 03:46:16 +00:00
libsql/libsql-server/tests/common/auth.rs
2024-03-11 15:00:26 +00:00

17 lines
701 B
Rust

use base64::Engine;
use jsonwebtoken::EncodingKey;
use ring::signature::{Ed25519KeyPair, KeyPair};
pub fn key_pair() -> (EncodingKey, String) {
let doc = Ed25519KeyPair::generate_pkcs8(&ring::rand::SystemRandom::new()).unwrap();
let encoding_key = EncodingKey::from_ed_der(doc.as_ref());
let pair = Ed25519KeyPair::from_pkcs8(doc.as_ref()).unwrap();
let jwt_key = base64::prelude::BASE64_URL_SAFE_NO_PAD.encode(pair.public_key().as_ref());
(encoding_key, jwt_key)
}
pub fn encode<T: serde::Serialize>(claims: &T, key: &EncodingKey) -> String {
let header = jsonwebtoken::Header::new(jsonwebtoken::Algorithm::EdDSA);
jsonwebtoken::encode(&header, &claims, key).unwrap()
}