From 16616fcee46a1b57d369598af0c2bab2e985732b Mon Sep 17 00:00:00 2001 From: "Spencer C. Imbleau" Date: Fri, 20 Oct 2023 21:34:32 -0400 Subject: [PATCH] build: simple example prints valid time --- Cargo.toml | 4 ++++ examples/simple.rs | 33 +++++++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b65c26c..5f141fe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,5 +11,9 @@ bevy-async-task = "1.2" async-trait = "0.1" bevy = { version = "0.11", default-features = false } +[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies] +getrandom = { version = "0.2" } + [target.'cfg(target_arch = "wasm32")'.dev-dependencies] wasm-bindgen-test = "0.3" +getrandom = { version = "0.2", features = ["js"] } diff --git a/examples/simple.rs b/examples/simple.rs index 1f1ff78..f9baa91 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -21,8 +21,8 @@ impl AuthProvider for MyAuthProvider { Ok(Keystore { username, password, - access_token: "123".to_string(), - refresh_token: "456".to_string(), + access_token: random_token(), + refresh_token: random_token(), access_expires: bevy_key_rotation::Instant::now() + Duration::from_secs(10), refresh_expires: bevy_key_rotation::Instant::now() @@ -36,7 +36,7 @@ impl AuthProvider for MyAuthProvider { Ok(Keystore { username: keystore.username, password: keystore.password, - access_token: "789".to_string(), + access_token: random_token(), refresh_token: keystore.refresh_token, access_expires: keystore.access_expires + Duration::from_secs(5), refresh_expires: keystore.refresh_expires, @@ -49,17 +49,25 @@ fn status_check( mut update_every: Local>, keystore: Res, ) { - // Print status every 2s - let update_every = update_every - .get_or_insert(Timer::from_seconds(2.0, TimerMode::Repeating)); + // Print status every few seconds... + const PRINT_EVERY_SECONDS: f32 = 1.0; + let update_every = update_every.get_or_insert(Timer::from_seconds( + PRINT_EVERY_SECONDS, + TimerMode::Repeating, + )); update_every.tick(time.delta()); if !update_every.finished() { return; } - update_every.reset(); // Log current access token - log::info!("{:?}", *keystore); + log::info!( + token = keystore.access_token, + refresh_token = keystore.refresh_token, + "token valid for: {:.0?}, refresh token valid for: {:.0?}", + keystore.access_token_valid_for(), + keystore.refresh_token_valid_for(), + ); } pub fn main() { @@ -80,3 +88,12 @@ pub fn main() { .add_systems(Update, status_check) .run(); } + +fn random_token() -> String { + let mut token = vec![0; 6]; + getrandom::getrandom(&mut token).unwrap(); + for byte in token.as_mut_slice() { + *byte = (*byte % 26) + b'A'; // Convert to A-Z character + } + String::from_utf8(token).unwrap() +}