Skip to content

Commit 84bab91

Browse files
committed
refactor: fmt
1 parent 0770633 commit 84bab91

File tree

5 files changed

+21
-60
lines changed

5 files changed

+21
-60
lines changed

examples/error_handling.rs

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use bevy::{
33
prelude::*,
44
};
55
use bevy_key_rotation::{
6-
AuthProvider, KeyRotationPlugin, KeyRotationSettings, Keystore,
7-
KeystoreState, StartKeyRotationExt, TokenRotationError,
6+
AuthProvider, KeyRotationPlugin, KeyRotationSettings, Keystore, KeystoreState,
7+
StartKeyRotationExt, TokenRotationError,
88
};
99
use std::sync::Arc;
1010

@@ -29,22 +29,15 @@ impl AuthProvider for MyAuthProvider {
2929
+ bevy_key_rotation::Duration::from_secs(60),
3030
})
3131
}
32-
async fn refresh(
33-
&self,
34-
_keystore: Keystore,
35-
) -> Result<Keystore, TokenRotationError> {
32+
async fn refresh(&self, _keystore: Keystore) -> Result<Keystore, TokenRotationError> {
3633
#[derive(thiserror::Error, Default, Debug)]
3734
#[error("This fails on purpose!")]
3835
struct MyError;
3936
Err(TokenRotationError::new(MyError))
4037
}
4138
}
4239

43-
fn status_check(
44-
time: Res<Time>,
45-
mut update_every: Local<Option<Timer>>,
46-
keystore: Res<Keystore>,
47-
) {
40+
fn status_check(time: Res<Time>, mut update_every: Local<Option<Timer>>, keystore: Res<Keystore>) {
4841
// Print status every few seconds...
4942
const PRINT_EVERY_SECONDS: f32 = 1.0;
5043
let update_every = update_every.get_or_insert(Timer::from_seconds(
@@ -56,9 +49,7 @@ fn status_check(
5649
return;
5750
}
5851

59-
if keystore.access_token_valid_for()
60-
< bevy_key_rotation::Duration::from_secs(5)
61-
{
52+
if keystore.access_token_valid_for() < bevy_key_rotation::Duration::from_secs(5) {
6253
log::warn!("The keystore is about to be non-conformant!");
6354
// You could attempt to re-authenticate from scratch:
6455
// commands.start_key_rotation(username, password);
@@ -81,18 +72,13 @@ pub fn main() {
8172
.add_plugins(KeyRotationPlugin {
8273
rotation_settings: KeyRotationSettings {
8374
rotation_timeout: bevy_key_rotation::Duration::MAX, // no timeout
84-
rotation_check_interval: bevy_key_rotation::Duration::from_secs(
85-
5,
86-
),
75+
rotation_check_interval: bevy_key_rotation::Duration::from_secs(5),
8776
rotate_before: bevy_key_rotation::Duration::from_secs(15),
8877
},
8978
auth_provider: Arc::new(MyAuthProvider),
9079
})
9180
.add_systems(Startup, |mut commands: Commands| {
92-
commands.start_key_rotation(
93-
"username".to_string(),
94-
"password".to_string(),
95-
);
81+
commands.start_key_rotation("username".to_string(), "password".to_string());
9682
})
9783
.add_systems(
9884
Update,
@@ -104,9 +90,7 @@ pub fn main() {
10490
to: KeystoreState::NonConformant,
10591
},
10692
|| {
107-
error!(
108-
"Keystore is now non-conformant! Keys cannot be rotated."
109-
);
93+
error!("Keystore is now non-conformant! Keys cannot be rotated.");
11094
},
11195
)
11296
.run();

examples/simple.rs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use bevy::{
33
prelude::*,
44
};
55
use bevy_key_rotation::{
6-
AuthProvider, KeyRotationPlugin, KeyRotationSettings, Keystore,
7-
StartKeyRotationExt, TokenRotationError,
6+
AuthProvider, KeyRotationPlugin, KeyRotationSettings, Keystore, StartKeyRotationExt,
7+
TokenRotationError,
88
};
99
use std::sync::Arc;
1010

@@ -29,27 +29,19 @@ impl AuthProvider for MyAuthProvider {
2929
+ bevy_key_rotation::Duration::from_secs(20),
3030
})
3131
}
32-
async fn refresh(
33-
&self,
34-
keystore: Keystore,
35-
) -> Result<Keystore, TokenRotationError> {
32+
async fn refresh(&self, keystore: Keystore) -> Result<Keystore, TokenRotationError> {
3633
Ok(Keystore {
3734
username: keystore.username,
3835
password: keystore.password,
3936
access_token: random_token(),
4037
refresh_token: keystore.refresh_token,
41-
access_expires: keystore.access_expires
42-
+ bevy_key_rotation::Duration::from_secs(5),
38+
access_expires: keystore.access_expires + bevy_key_rotation::Duration::from_secs(5),
4339
refresh_expires: keystore.refresh_expires,
4440
})
4541
}
4642
}
4743

48-
fn status_check(
49-
time: Res<Time>,
50-
mut update_every: Local<Option<Timer>>,
51-
keystore: Res<Keystore>,
52-
) {
44+
fn status_check(time: Res<Time>, mut update_every: Local<Option<Timer>>, keystore: Res<Keystore>) {
5345
// Print status every few seconds...
5446
const PRINT_EVERY_SECONDS: f32 = 1.0;
5547
let update_every = update_every.get_or_insert(Timer::from_seconds(
@@ -77,18 +69,13 @@ pub fn main() {
7769
.add_plugins(KeyRotationPlugin {
7870
rotation_settings: KeyRotationSettings {
7971
rotation_timeout: bevy_key_rotation::Duration::MAX, // no timeout
80-
rotation_check_interval: bevy_key_rotation::Duration::from_secs(
81-
1,
82-
),
72+
rotation_check_interval: bevy_key_rotation::Duration::from_secs(1),
8373
rotate_before: bevy_key_rotation::Duration::from_secs(5),
8474
},
8575
auth_provider: Arc::new(MyAuthProvider),
8676
})
8777
.add_systems(Startup, |mut commands: Commands| {
88-
commands.start_key_rotation(
89-
"username".to_string(),
90-
"password".to_string(),
91-
);
78+
commands.start_key_rotation("username".to_string(), "password".to_string());
9279
})
9380
.add_systems(Update, status_check)
9481
.run();

src/data_types.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ pub trait AuthProvider {
1111
username: String,
1212
password: String,
1313
) -> Result<Keystore, TokenRotationError>;
14-
async fn refresh(
15-
&self,
16-
keystore: Keystore,
17-
) -> Result<Keystore, TokenRotationError>;
14+
async fn refresh(&self, keystore: Keystore) -> Result<Keystore, TokenRotationError>;
1815
}
1916

2017
/// A resource around the auth provider used (mostly internally) to perform

src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ pub use web_time::{Duration, Instant};
1010

1111
pub use commands::{StartKeyRotationExt, StopKeyRotationExt};
1212
pub use data_types::{
13-
AuthProvider, KeyRotationEvent, KeyRotationSettings, Keygen, Keystore,
14-
KeystoreState,
13+
AuthProvider, KeyRotationEvent, KeyRotationSettings, Keygen, Keystore, KeystoreState,
1514
};
1615
pub use error::TokenRotationError;
1716
pub use plugin::KeyRotationPlugin;

src/systems.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,13 @@ use crate::{
44
Duration, KeystoreState,
55
};
66
use bevy::prelude::*;
7-
use bevy_async_task::{
8-
AsyncTask, AsyncTaskRunner, AsyncTaskStatus, TimeoutError,
9-
};
7+
use bevy_async_task::{AsyncTask, AsyncTaskRunner, AsyncTaskStatus, TimeoutError};
108

119
pub(crate) fn rotate_tokens(
1210
keygen: Res<Keygen>,
1311
settings: Res<KeyRotationSettings>,
1412
mut keystore: ResMut<Keystore>,
15-
mut tr_rotate: AsyncTaskRunner<
16-
Result<Result<Keystore, TokenRotationError>, TimeoutError>,
17-
>,
13+
mut tr_rotate: AsyncTaskRunner<Result<Result<Keystore, TokenRotationError>, TimeoutError>>,
1814
mut event_writer: EventWriter<KeyRotationEvent>,
1915
mut rotation_timer: Local<Option<Timer>>,
2016
time: Res<Time>,
@@ -51,10 +47,8 @@ pub(crate) fn rotate_tokens(
5147
rotation_timer.reset();
5248

5349
// Check if rotation is necessary
54-
let rtoken_expiring =
55-
keystore.refresh_token_valid_for() < settings.rotate_before;
56-
let atoken_expiring =
57-
keystore.access_token_valid_for() < settings.rotate_before;
50+
let rtoken_expiring = keystore.refresh_token_valid_for() < settings.rotate_before;
51+
let atoken_expiring = keystore.access_token_valid_for() < settings.rotate_before;
5852

5953
if rtoken_expiring {
6054
info!("rotating refresh token...");

0 commit comments

Comments
 (0)