Skip to content

Commit

Permalink
refactor: fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
simbleau committed Oct 20, 2023
1 parent 3810e48 commit a8ca523
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 15 deletions.
27 changes: 20 additions & 7 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use bevy::{
prelude::*,
};
use bevy_key_rotation::{
AuthProvider, KeyRotationPlugin, KeyRotationSettings, Keystore, TokenRotationError,
AuthProvider, KeyRotationPlugin, KeyRotationSettings, Keystore,
TokenRotationError,
};
use std::{sync::Arc, time::Duration};

Expand All @@ -22,11 +23,16 @@ impl AuthProvider for MyAuthProvider {
password,
access_token: "123".to_string(),
refresh_token: "456".to_string(),
access_expires: bevy_key_rotation::Instant::now() + Duration::from_secs(30),
refresh_expires: bevy_key_rotation::Instant::now() + Duration::from_secs(60),
access_expires: bevy_key_rotation::Instant::now()
+ Duration::from_secs(30),
refresh_expires: bevy_key_rotation::Instant::now()
+ Duration::from_secs(60),
})
}
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,
Expand All @@ -38,9 +44,14 @@ impl AuthProvider for MyAuthProvider {
}
}

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 2s
let update_every = update_every.get_or_insert(Timer::from_seconds(2.0, TimerMode::Repeating));
let update_every = update_every
.get_or_insert(Timer::from_seconds(2.0, TimerMode::Repeating));
update_every.tick(time.delta());
if !update_every.just_finished() {
return;
Expand All @@ -61,7 +72,9 @@ pub fn main() {
access_valid_time: bevy_key_rotation::Duration::from_secs(5),
refresh_valid_time: bevy_key_rotation::Duration::from_secs(10),
rotation_timeout: bevy_key_rotation::Duration::from_secs(1),
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(3),
},
auth_provider: Arc::new(MyAuthProvider),
Expand Down
4 changes: 4 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
max_width = 80
comment_width = 80
wrap_comments = true
imports_granularity = "Crate"
9 changes: 6 additions & 3 deletions src/data_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ 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 wrapper around the auth provider used internally to perform auth.
Expand Down Expand Up @@ -74,10 +77,10 @@ impl Default for KeyRotationSettings {
// - Refresh token valid for 30 days
// - Rotation attempt timeout is 10 seconds
// - Re-attempt rotation, if necessary, every 60 seconds
// - Begin to attempt key rotation 5 minutes before expiration (55 min in)
// - Begin to attempt key rotation 5 minutes before expiration
Self {
access_valid_time: Duration::from_secs(60 * 60), // 1 hour
refresh_valid_time: Duration::from_secs(60 * 60 * 24 * 30), // 30 days
refresh_valid_time: Duration::from_secs(60 * 60 * 24 * 30), /* 30 days */
rotation_timeout: Duration::from_secs(10),
rotation_check_interval: Duration::from_secs(60),
rotate_before: Duration::from_secs(60 * 5), // 5 min
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ mod systems;
// Re-export instant
pub use instant::{Duration, Instant};

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

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 rotation_timer: Local<Option<Timer>>,
time: Res<Time>,
) {
Expand Down Expand Up @@ -48,9 +52,11 @@ pub(crate) fn rotate_tokens(
// Check if rotation is necessary
let now = Instant::now();
let rtoken_expiring =
keystore.refresh_expires.saturating_duration_since(now) < settings.rotate_before;
keystore.refresh_expires.saturating_duration_since(now)
< settings.rotate_before;
let atoken_expiring =
keystore.access_expires.saturating_duration_since(now) < settings.rotate_before;
keystore.access_expires.saturating_duration_since(now)
< settings.rotate_before;

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

0 comments on commit a8ca523

Please sign in to comment.