Skip to content

Commit

Permalink
refactor: fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
simbleau committed Mar 28, 2024
1 parent 0770633 commit 84bab91
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 60 deletions.
32 changes: 8 additions & 24 deletions examples/error_handling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use bevy::{
prelude::*,
};
use bevy_key_rotation::{
AuthProvider, KeyRotationPlugin, KeyRotationSettings, Keystore,
KeystoreState, StartKeyRotationExt, TokenRotationError,
AuthProvider, KeyRotationPlugin, KeyRotationSettings, Keystore, KeystoreState,
StartKeyRotationExt, TokenRotationError,
};
use std::sync::Arc;

Expand All @@ -29,22 +29,15 @@ impl AuthProvider for MyAuthProvider {
+ bevy_key_rotation::Duration::from_secs(60),
})
}
async fn refresh(
&self,
_keystore: Keystore,
) -> Result<Keystore, TokenRotationError> {
async fn refresh(&self, _keystore: Keystore) -> Result<Keystore, TokenRotationError> {
#[derive(thiserror::Error, Default, Debug)]
#[error("This fails on purpose!")]
struct MyError;
Err(TokenRotationError::new(MyError))
}
}

fn status_check(
time: Res<Time>,
mut update_every: Local<Option<Timer>>,
keystore: Res<Keystore>,
) {
fn status_check(time: Res<Time>, mut update_every: Local<Option<Timer>>, keystore: Res<Keystore>) {
// Print status every few seconds...
const PRINT_EVERY_SECONDS: f32 = 1.0;
let update_every = update_every.get_or_insert(Timer::from_seconds(
Expand All @@ -56,9 +49,7 @@ fn status_check(
return;
}

if keystore.access_token_valid_for()
< bevy_key_rotation::Duration::from_secs(5)
{
if keystore.access_token_valid_for() < bevy_key_rotation::Duration::from_secs(5) {
log::warn!("The keystore is about to be non-conformant!");
// You could attempt to re-authenticate from scratch:
// commands.start_key_rotation(username, password);
Expand All @@ -81,18 +72,13 @@ pub fn main() {
.add_plugins(KeyRotationPlugin {
rotation_settings: KeyRotationSettings {
rotation_timeout: bevy_key_rotation::Duration::MAX, // no timeout
rotation_check_interval: bevy_key_rotation::Duration::from_secs(
5,
),
rotation_check_interval: bevy_key_rotation::Duration::from_secs(5),
rotate_before: bevy_key_rotation::Duration::from_secs(15),
},
auth_provider: Arc::new(MyAuthProvider),
})
.add_systems(Startup, |mut commands: Commands| {
commands.start_key_rotation(
"username".to_string(),
"password".to_string(),
);
commands.start_key_rotation("username".to_string(), "password".to_string());
})
.add_systems(
Update,
Expand All @@ -104,9 +90,7 @@ pub fn main() {
to: KeystoreState::NonConformant,
},
|| {
error!(
"Keystore is now non-conformant! Keys cannot be rotated."
);
error!("Keystore is now non-conformant! Keys cannot be rotated.");
},
)
.run();
Expand Down
27 changes: 7 additions & 20 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use bevy::{
prelude::*,
};
use bevy_key_rotation::{
AuthProvider, KeyRotationPlugin, KeyRotationSettings, Keystore,
StartKeyRotationExt, TokenRotationError,
AuthProvider, KeyRotationPlugin, KeyRotationSettings, Keystore, StartKeyRotationExt,
TokenRotationError,
};
use std::sync::Arc;

Expand All @@ -29,27 +29,19 @@ impl AuthProvider for MyAuthProvider {
+ bevy_key_rotation::Duration::from_secs(20),
})
}
async fn refresh(
&self,
keystore: Keystore,
) -> Result<Keystore, TokenRotationError> {
async fn refresh(&self, keystore: Keystore) -> Result<Keystore, TokenRotationError> {
Ok(Keystore {
username: keystore.username,
password: keystore.password,
access_token: random_token(),
refresh_token: keystore.refresh_token,
access_expires: keystore.access_expires
+ bevy_key_rotation::Duration::from_secs(5),
access_expires: keystore.access_expires + bevy_key_rotation::Duration::from_secs(5),
refresh_expires: keystore.refresh_expires,
})
}
}

fn status_check(
time: Res<Time>,
mut update_every: Local<Option<Timer>>,
keystore: Res<Keystore>,
) {
fn status_check(time: Res<Time>, mut update_every: Local<Option<Timer>>, keystore: Res<Keystore>) {
// Print status every few seconds...
const PRINT_EVERY_SECONDS: f32 = 1.0;
let update_every = update_every.get_or_insert(Timer::from_seconds(
Expand Down Expand Up @@ -77,18 +69,13 @@ pub fn main() {
.add_plugins(KeyRotationPlugin {
rotation_settings: KeyRotationSettings {
rotation_timeout: bevy_key_rotation::Duration::MAX, // no timeout
rotation_check_interval: bevy_key_rotation::Duration::from_secs(
1,
),
rotation_check_interval: bevy_key_rotation::Duration::from_secs(1),
rotate_before: bevy_key_rotation::Duration::from_secs(5),
},
auth_provider: Arc::new(MyAuthProvider),
})
.add_systems(Startup, |mut commands: Commands| {
commands.start_key_rotation(
"username".to_string(),
"password".to_string(),
);
commands.start_key_rotation("username".to_string(), "password".to_string());
})
.add_systems(Update, status_check)
.run();
Expand Down
5 changes: 1 addition & 4 deletions src/data_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ pub trait AuthProvider {
username: String,
password: String,
) -> Result<Keystore, TokenRotationError>;
async fn refresh(
&self,
keystore: Keystore,
) -> Result<Keystore, TokenRotationError>;
async fn refresh(&self, keystore: Keystore) -> Result<Keystore, TokenRotationError>;
}

/// A resource around the auth provider used (mostly internally) to perform
Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ pub use web_time::{Duration, Instant};

pub use commands::{StartKeyRotationExt, StopKeyRotationExt};
pub use data_types::{
AuthProvider, KeyRotationEvent, KeyRotationSettings, Keygen, Keystore,
KeystoreState,
AuthProvider, KeyRotationEvent, KeyRotationSettings, Keygen, Keystore, KeystoreState,
};
pub use error::TokenRotationError;
pub use plugin::KeyRotationPlugin;
14 changes: 4 additions & 10 deletions src/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@ use crate::{
Duration, KeystoreState,
};
use bevy::prelude::*;
use bevy_async_task::{
AsyncTask, AsyncTaskRunner, AsyncTaskStatus, TimeoutError,
};
use bevy_async_task::{AsyncTask, AsyncTaskRunner, AsyncTaskStatus, TimeoutError};

pub(crate) fn rotate_tokens(
keygen: Res<Keygen>,
settings: Res<KeyRotationSettings>,
mut keystore: ResMut<Keystore>,
mut tr_rotate: AsyncTaskRunner<
Result<Result<Keystore, TokenRotationError>, TimeoutError>,
>,
mut tr_rotate: AsyncTaskRunner<Result<Result<Keystore, TokenRotationError>, TimeoutError>>,
mut event_writer: EventWriter<KeyRotationEvent>,
mut rotation_timer: Local<Option<Timer>>,
time: Res<Time>,
Expand Down Expand Up @@ -51,10 +47,8 @@ pub(crate) fn rotate_tokens(
rotation_timer.reset();

// Check if rotation is necessary
let rtoken_expiring =
keystore.refresh_token_valid_for() < settings.rotate_before;
let atoken_expiring =
keystore.access_token_valid_for() < settings.rotate_before;
let rtoken_expiring = keystore.refresh_token_valid_for() < settings.rotate_before;
let atoken_expiring = keystore.access_token_valid_for() < settings.rotate_before;

if rtoken_expiring {
info!("rotating refresh token...");
Expand Down

0 comments on commit 84bab91

Please sign in to comment.